Skip to content

Improve StrSplit returnType #3999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: 1.12.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build/baseline-8.0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ parameters:
path: ../src/Type/Php/StrSplitFunctionReturnTypeExtension.php

-
message: "#^Strict comparison using \\=\\=\\= between list<string> and false will always evaluate to false\\.$#"
message: "#^Strict comparison using \\=\\=\\= between non\\-empty\\-list<string> and false will always evaluate to false\\.$#"
count: 1
path: ../src/Type/Php/StrSplitFunctionReturnTypeExtension.php

Expand Down
101 changes: 66 additions & 35 deletions src/Type/Php/StrSplitFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;
Expand All @@ -9,14 +9,19 @@
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\NeverType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -51,14 +56,15 @@

if (count($functionCall->getArgs()) >= 2) {
$splitLengthType = $scope->getType($functionCall->getArgs()[1]->value);
if ($splitLengthType instanceof ConstantIntegerType) {
$splitLength = $splitLengthType->getValue();
if ($splitLength < 1) {
return new ConstantBooleanType(false);
}
}
} else {
$splitLength = 1;
$splitLengthType = new ConstantIntegerType(1);
}

if ($splitLengthType instanceof ConstantIntegerType) {
$splitLength = $splitLengthType->getValue();
if ($splitLength < 1) {
return new ConstantBooleanType(false);
}
}

$encoding = null;
Expand All @@ -67,47 +73,72 @@
$strings = $scope->getType($functionCall->getArgs()[2]->value)->getConstantStrings();
$values = array_unique(array_map(static fn (ConstantStringType $encoding): string => $encoding->getValue(), $strings));

if (count($values) !== 1) {
return null;
}

$encoding = $values[0];
if (!$this->isSupportedEncoding($encoding)) {
return new ConstantBooleanType(false);
if (count($values) === 1) {
$encoding = $values[0];
if (!$this->isSupportedEncoding($encoding)) {
return $this->phpVersion->throwsValueErrorForInternalFunctions() ? new NeverType() : new ConstantBooleanType(false);
}
}
} else {
$encoding = mb_internal_encoding();
}
}

if (!isset($splitLength)) {
return null;
}

$stringType = $scope->getType($functionCall->getArgs()[0]->value);

$constantStrings = $stringType->getConstantStrings();
if (count($constantStrings) > 0) {
$results = [];
foreach ($constantStrings as $constantString) {
$items = $encoding === null
? str_split($constantString->getValue(), $splitLength)
: @mb_str_split($constantString->getValue(), $splitLength, $encoding);
if ($items === false) {
throw new ShouldNotHappenException();
if (
isset($splitLength)
&& ($functionReflection->getName() === 'str_split' || $encoding !== null)
) {
$constantStrings = $stringType->getConstantStrings();
if (count($constantStrings) > 0) {
$results = [];
foreach ($constantStrings as $constantString) {
$items = $encoding === null
? str_split($constantString->getValue(), $splitLength)
: @mb_str_split($constantString->getValue(), $splitLength, $encoding);
if ($items === false) {

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Strict comparison using === between list<non-empty-string> and false will always evaluate to false.

Check failure on line 99 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Strict comparison using === between list<string> and false will always evaluate to false.
throw new ShouldNotHappenException();
}

Check failure on line 101 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 101 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

$results[] = self::createConstantArrayFrom($items, $scope);
}

$results[] = self::createConstantArrayFrom($items, $scope);
return TypeCombinator::union(...$results);

Check failure on line 106 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 106 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 106 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Strict comparison using === between list<string> and false will always evaluate to false.

Check failure on line 106 in src/Type/Php/StrSplitFunctionReturnTypeExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Strict comparison using === between list<string> and false will always evaluate to false.
}

return TypeCombinator::union(...$results);
}

$returnType = AccessoryArrayListType::intersectWith(new ArrayType(new IntegerType(), new StringType()));
$isInputNonEmptyString = $stringType->isNonEmptyString()->yes();

$valueTypes = [new StringType()];
if ($isInputNonEmptyString || $this->phpVersion->strSplitReturnsEmptyArray()) {
$valueTypes[] = new AccessoryNonEmptyStringType();
}
if ($stringType->isLowercaseString()->yes()) {
$valueTypes[] = new AccessoryLowercaseStringType();
}
if ($stringType->isUppercaseString()->yes()) {
$valueTypes[] = new AccessoryUppercaseStringType();
}
$returnValueType = TypeCombinator::intersect(new StringType(), ...$valueTypes);

$returnType = AccessoryArrayListType::intersectWith(TypeCombinator::intersect(new ArrayType(new IntegerType(), $returnValueType)));
if (
// Non-empty-string will return an array with at least an element
$isInputNonEmptyString
// str_split('', 1) returns [''] on old PHP version and [] on new ones
|| ($functionReflection->getName() === 'str_split' && !$this->phpVersion->strSplitReturnsEmptyArray())
) {
$returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType());
}
if (
// Length parameter accepts int<1, max> or throws a ValueError/return false based on PHP Version.
!$this->phpVersion->throwsValueErrorForInternalFunctions()
&& !IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($splitLengthType)->yes()
) {
$returnType = TypeCombinator::union($returnType, new ConstantBooleanType(false));
}

return $encoding === null && !$this->phpVersion->strSplitReturnsEmptyArray()
? TypeCombinator::intersect($returnType, new NonEmptyArrayType())
: $returnType;
return $returnType;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ private static function findTestFiles(): iterable
} else {
yield __DIR__ . '/data/str-split-php74.php';
}
if (PHP_VERSION_ID >= 80000) {
if (PHP_VERSION_ID >= 80200) {
yield __DIR__ . '/data/mb-str-split-php82.php';
} elseif (PHP_VERSION_ID >= 80000) {
yield __DIR__ . '/data/mb-str-split-php80.php';
} elseif (PHP_VERSION_ID >= 74000) {
yield __DIR__ . '/data/mb-str-split-php74.php';
Expand Down
35 changes: 31 additions & 4 deletions tests/PHPStan/Analyser/data/mb-str-split-php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ public function legacyTest(): void
assertType('false', $mbStrSplitConstantStringWithFailureSplitLength);

$mbStrSplitConstantStringWithInvalidSplitLengthType = mb_str_split('abcdef', []);
assertType('list<string>', $mbStrSplitConstantStringWithInvalidSplitLengthType);
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithInvalidSplitLengthType);

$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', 1);
assertType("array{'a', 'b', 'c', 'd', 'e', 'f'}|array{'g', 'h', 'i', 'j', 'k', 'l'}", $mbStrSplitConstantStringWithVariableStringAndConstantSplitLength);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLength = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2);
assertType('list<string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLength);
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLength);

$mbStrSplitConstantStringWithOneSplitLengthAndValidEncoding = mb_str_split('abcdef', 1, 'UTF-8');
assertType("array{'a', 'b', 'c', 'd', 'e', 'f'}", $mbStrSplitConstantStringWithOneSplitLengthAndValidEncoding);
Expand Down Expand Up @@ -65,7 +65,7 @@ public function legacyTest(): void
assertType('false', $mbStrSplitConstantStringWithFailureSplitLengthAndVariableEncoding);

$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding = mb_str_split('abcdef', [], 'UTF-8');
assertType('list<string>', $mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding);
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding);

$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndInvalidEncoding = mb_str_split('abcdef', [], 'FAKE');
assertType('false', $mbStrSplitConstantStringWithInvalidSplitLengthTypeAndInvalidEncoding);
Expand All @@ -83,12 +83,39 @@ public function legacyTest(): void
assertType('list<string>', $mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndVariableEncoding);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2, 'UTF-8');
assertType('list<string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding);
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndInvalidEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2, 'FAKE');
assertType('false', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndInvalidEncoding);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndVariableEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2, doFoo());
assertType('list<string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndVariableEncoding);
}

/**
* @param non-empty-string $nonEmptyString
* @param non-falsy-string $nonFalsyString
* @param lowercase-string $lowercaseString
* @param uppercase-string $uppercaseString
*/
function doFoo(
string $string,
string $nonEmptyString,
string $nonFalsyString,
string $lowercaseString,
string $uppercaseString,
int $integer,
):void {
assertType('list<string>', mb_str_split($string));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonEmptyString));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonFalsyString));
assertType('list<lowercase-string>', mb_str_split($lowercaseString));
assertType('list<uppercase-string>', mb_str_split($uppercaseString));

assertType('list<string>', mb_str_split($string, $integer));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonEmptyString, $integer));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonFalsyString, $integer));
assertType('list<lowercase-string>', mb_str_split($lowercaseString, $integer));
assertType('list<uppercase-string>', mb_str_split($uppercaseString, $integer));
}
}
121 changes: 121 additions & 0 deletions tests/PHPStan/Analyser/data/mb-str-split-php82.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace MbStrSplitPHP82;

use function PHPStan\Testing\assertType;

class MbStrSplit {
public function legacyTest(): void
{
/** @var string $string */
$string = doFoo();

$mbStrSplitConstantStringWithoutDefinedParameters = mb_str_split();
assertType('list<string>', $mbStrSplitConstantStringWithoutDefinedParameters);

$mbStrSplitConstantStringWithoutDefinedSplitLength = mb_str_split('abcdef');
assertType('array{\'a\', \'b\', \'c\', \'d\', \'e\', \'f\'}', $mbStrSplitConstantStringWithoutDefinedSplitLength);

$mbStrSplitStringWithoutDefinedSplitLength = mb_str_split($string);
assertType('list<non-empty-string>', $mbStrSplitStringWithoutDefinedSplitLength);

$mbStrSplitConstantStringWithOneSplitLength = mb_str_split('abcdef', 1);
assertType('array{\'a\', \'b\', \'c\', \'d\', \'e\', \'f\'}', $mbStrSplitConstantStringWithOneSplitLength);

$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLength = mb_str_split('abcdef', 999);
assertType('array{\'abcdef\'}', $mbStrSplitConstantStringWithGreaterSplitLengthThanStringLength);

$mbStrSplitConstantStringWithFailureSplitLength = mb_str_split('abcdef', 0);
assertType('false', $mbStrSplitConstantStringWithFailureSplitLength);

$mbStrSplitConstantStringWithInvalidSplitLengthType = mb_str_split('abcdef', []);
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithInvalidSplitLengthType);

$mbStrSplitConstantStringWithVariableStringAndConstantSplitLength = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', 1);
assertType("array{'a', 'b', 'c', 'd', 'e', 'f'}|array{'g', 'h', 'i', 'j', 'k', 'l'}", $mbStrSplitConstantStringWithVariableStringAndConstantSplitLength);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLength = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2);
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLength);

$mbStrSplitConstantStringWithOneSplitLengthAndValidEncoding = mb_str_split('abcdef', 1, 'UTF-8');
assertType("array{'a', 'b', 'c', 'd', 'e', 'f'}", $mbStrSplitConstantStringWithOneSplitLengthAndValidEncoding);

$mbStrSplitConstantStringWithOneSplitLengthAndInvalidEncoding = mb_str_split('abcdef', 1, 'FAKE');
assertType('false', $mbStrSplitConstantStringWithOneSplitLengthAndInvalidEncoding);

$mbStrSplitConstantStringWithOneSplitLengthAndVariableEncoding = mb_str_split('abcdef', 1, doFoo());
assertType('list<string>', $mbStrSplitConstantStringWithOneSplitLengthAndVariableEncoding);

$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndValidEncoding = mb_str_split('abcdef', 999, 'UTF-8');
assertType("array{'abcdef'}", $mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndValidEncoding);

$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndInvalidEncoding = mb_str_split('abcdef', 999, 'FAKE');
assertType('false', $mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndInvalidEncoding);

$mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndVariableEncoding = mb_str_split('abcdef', 999, doFoo());
assertType('list<string>', $mbStrSplitConstantStringWithGreaterSplitLengthThanStringLengthAndVariableEncoding);

$mbStrSplitConstantStringWithFailureSplitLengthAndValidEncoding = mb_str_split('abcdef', 0, 'UTF-8');
assertType('false', $mbStrSplitConstantStringWithFailureSplitLengthAndValidEncoding);

$mbStrSplitConstantStringWithFailureSplitLengthAndInvalidEncoding = mb_str_split('abcdef', 0, 'FAKE');
assertType('false', $mbStrSplitConstantStringWithFailureSplitLengthAndInvalidEncoding);

$mbStrSplitConstantStringWithFailureSplitLengthAndVariableEncoding = mb_str_split('abcdef', 0, doFoo());
assertType('false', $mbStrSplitConstantStringWithFailureSplitLengthAndVariableEncoding);

$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding = mb_str_split('abcdef', [], 'UTF-8');
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithInvalidSplitLengthTypeAndValidEncoding);

$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndInvalidEncoding = mb_str_split('abcdef', [], 'FAKE');
assertType('false', $mbStrSplitConstantStringWithInvalidSplitLengthTypeAndInvalidEncoding);

$mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding = mb_str_split('abcdef', [], doFoo());
assertType('list<string>', $mbStrSplitConstantStringWithInvalidSplitLengthTypeAndVariableEncoding);

$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndValidEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', 1, 'UTF-8');
assertType("array{'a', 'b', 'c', 'd', 'e', 'f'}|array{'g', 'h', 'i', 'j', 'k', 'l'}", $mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndValidEncoding);

$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndInvalidEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', 1, 'FAKE');
assertType('false', $mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndInvalidEncoding);

$mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndVariableEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', 1, doFoo());
assertType('list<string>', $mbStrSplitConstantStringWithVariableStringAndConstantSplitLengthAndVariableEncoding);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2, 'UTF-8');
assertType('non-empty-list<lowercase-string&non-empty-string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndValidEncoding);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndInvalidEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2, 'FAKE');
assertType('false', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndInvalidEncoding);

$mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndVariableEncoding = mb_str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2, doFoo());
assertType('list<string>', $mbStrSplitConstantStringWithVariableStringAndVariableSplitLengthAndVariableEncoding);
}

/**
* @param non-empty-string $nonEmptyString
* @param non-falsy-string $nonFalsyString
* @param lowercase-string $lowercaseString
* @param uppercase-string $uppercaseString
*/
function doFoo(
string $string,
string $nonEmptyString,
string $nonFalsyString,
string $lowercaseString,
string $uppercaseString,
int $integer,
):void {
assertType('list<non-empty-string>', mb_str_split($string));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonEmptyString));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonFalsyString));
assertType('list<lowercase-string&non-empty-string>', mb_str_split($lowercaseString));
assertType('list<non-empty-string&uppercase-string>', mb_str_split($uppercaseString));

assertType('list<non-empty-string>', mb_str_split($string, $integer));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonEmptyString, $integer));
assertType('non-empty-list<non-empty-string>', mb_str_split($nonFalsyString, $integer));
assertType('list<lowercase-string&non-empty-string>', mb_str_split($lowercaseString, $integer));
assertType('list<non-empty-string&uppercase-string>', mb_str_split($uppercaseString, $integer));
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/data/str-split-php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public function legacyTest() {
assertType('false', $strSplitConstantStringWithFailureSplitLength);

$strSplitConstantStringWithInvalidSplitLengthType = str_split('abcdef', []);
assertType('non-empty-list<string>|false', $strSplitConstantStringWithInvalidSplitLengthType);
assertType('(non-empty-list<lowercase-string&non-empty-string>)|false', $strSplitConstantStringWithInvalidSplitLengthType);

$strSplitConstantStringWithVariableStringAndConstantSplitLength = str_split(doFoo() ? 'abcdef' : 'ghijkl', 1);
assertType("array{'a', 'b', 'c', 'd', 'e', 'f'}|array{'g', 'h', 'i', 'j', 'k', 'l'}", $strSplitConstantStringWithVariableStringAndConstantSplitLength);

$strSplitConstantStringWithVariableStringAndVariableSplitLength = str_split(doFoo() ? 'abcdef' : 'ghijkl', doFoo() ? 1 : 2);
assertType('non-empty-list<string>|false', $strSplitConstantStringWithVariableStringAndVariableSplitLength);
assertType('(non-empty-list<lowercase-string&non-empty-string>)|false', $strSplitConstantStringWithVariableStringAndVariableSplitLength);

}
}
Loading
Loading