Skip to content

Commit

Permalink
fix multi-byte handling
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 8, 2024
1 parent 52511e2 commit 31edc40
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/Type/Php/SubstrDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function count;
use function in_array;
use function is_bool;
use function mb_substr;
use function substr;

class SubstrDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
Expand Down Expand Up @@ -63,16 +64,17 @@ public function getTypeFromFunctionCall(
$results = [];
foreach ($constantStrings as $constantString) {
if ($length !== null) {
$substr = substr(
$constantString->getValue(),
$offset->getValue(),
$length->getValue(),
);
if ($functionReflection->getName() === 'mb_substr') {
$substr = mb_substr($constantString->getValue(), $offset->getValue(), $length->getValue());
} else {
$substr = substr($constantString->getValue(), $offset->getValue(), $length->getValue());
}
} else {
$substr = substr(
$constantString->getValue(),
$offset->getValue(),
);
if ($functionReflection->getName() === 'mb_substr') {
$substr = mb_substr($constantString->getValue(), $offset->getValue());
} else {
$substr = substr($constantString->getValue(), $offset->getValue());
}
}

if (is_bool($substr)) {
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Analyser/data/non-empty-string-substr.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function doMbSubstr(string $s, $nonEmpty, $positiveInt, $postiveRange, $n

assertType('string', mb_substr($s, 0, $positiveInt));
assertType('non-empty-string', mb_substr($nonEmpty, 0, $positiveInt));

assertType('non-falsy-string', mb_substr("déjà_vu", 0, $positiveInt));
assertType("'déjà_vu'", mb_substr("déjà_vu", 0));
assertType("'déj'", mb_substr("déjà_vu", 0, 3));
}

}

0 comments on commit 31edc40

Please sign in to comment.