Skip to content

Commit

Permalink
Merge branch '1.5.x' into 1.6.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 4, 2022
2 parents 2418b2d + a29dc57 commit 2d2f653
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
9 changes: 0 additions & 9 deletions src/Type/Constant/ConstantArrayType.php
Expand Up @@ -192,15 +192,6 @@ private function powerSet(array $in): array
return $return;
}

public function getKeyType(): Type
{
if (count($this->keyTypes) > 1) {
return new UnionType($this->keyTypes);
}

return parent::getKeyType();
}

/**
* @return array<int, ConstantIntegerType|ConstantStringType>
*/
Expand Down
15 changes: 12 additions & 3 deletions src/Type/Php/StrlenFunctionReturnTypeExtension.php
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use function count;
Expand All @@ -37,11 +38,19 @@ public function getTypeFromFunctionCall(

$argType = $scope->getType($args[0]->value);

$constantStrings = TypeUtils::getConstantStrings($argType);
$constantScalars = TypeUtils::getConstantScalars($argType);
$min = null;
$max = null;
foreach ($constantStrings as $constantString) {
$len = strlen($constantString->getValue());
foreach ($constantScalars as $constantScalar) {
if ((new IntegerType())->isSuperTypeOf($constantScalar)->yes()) {
$len = strlen((string) $constantScalar->getValue());
} elseif ((new StringType())->isSuperTypeOf($constantScalar)->yes()) {
$len = strlen((string) $constantScalar->getValue());
} elseif ((new BooleanType())->isSuperTypeOf($constantScalar)->yes()) {
$len = strlen((string) $constantScalar->getValue());
} else {
break;
}

if ($min === null) {
$min = $len;
Expand Down
7 changes: 6 additions & 1 deletion tests/PHPStan/Analyser/data/non-empty-string.php
Expand Up @@ -280,8 +280,9 @@ class MoreNonEmptyStringFunctions
/**
* @param non-empty-string $nonEmpty
* @param '1'|'2'|'5'|'10' $constUnion
* @param 1|2|5|10|123|'1234'|false $constUnionMixed
*/
public function doFoo(string $s, string $nonEmpty, int $i, bool $bool, $constUnion)
public function doFoo(string $s, string $nonEmpty, int $i, bool $bool, $constUnion, $constUnionMixed)
{
assertType('string', addslashes($s));
assertType('non-empty-string', addslashes($nonEmpty));
Expand Down Expand Up @@ -336,6 +337,10 @@ public function doFoo(string $s, string $nonEmpty, int $i, bool $bool, $constUni
assertType('int<0, max>', strlen($s));
assertType('int<1, max>', strlen($nonEmpty));
assertType('int<1, 2>', strlen($constUnion));
assertType('int<0, 4>', strlen($constUnionMixed));
assertType('3', strlen(123));
assertType('1', strlen(true));
assertType('0', strlen(false));

assertType('non-empty-string', str_pad($nonEmpty, 0));
assertType('non-empty-string', str_pad($nonEmpty, 1));
Expand Down

0 comments on commit 2d2f653

Please sign in to comment.