Skip to content

Commit

Permalink
StrlenFunctionReturnTypeExtension: Cleanup `instanceof ConstantString…
Browse files Browse the repository at this point in the history
…Type`
  • Loading branch information
staabm committed Jan 5, 2025
1 parent 2132cc0 commit 2048fc8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1488,11 +1488,6 @@ parameters:
count: 1
path: src/Type/Php/StrRepeatFunctionReturnTypeExtension.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Constant\\\\ConstantStringType is error\\-prone and deprecated\\. Use Type\\:\\:getConstantStrings\\(\\) instead\\.$#"
count: 1
path: src/Type/Php/StrlenFunctionReturnTypeExtension.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\ObjectType is error\\-prone and deprecated\\. Use Type\\:\\:isObject\\(\\) or Type\\:\\:getObjectClassNames\\(\\) instead\\.$#"
count: 2
Expand Down
19 changes: 6 additions & 13 deletions src/Type/Php/StrlenFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerRangeType;
Expand Down Expand Up @@ -44,25 +42,20 @@ public function getTypeFromFunctionCall(
}

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

if ($argType->isSuperTypeOf(new BooleanType())->yes()) {
$constantScalars = TypeCombinator::remove($argType, new BooleanType())->getConstantScalarTypes();
$constantScalars = TypeCombinator::remove($argType, new BooleanType())->getConstantScalarValues();
if (count($constantScalars) > 0) {
$constantScalars[] = new ConstantBooleanType(true);
$constantScalars[] = new ConstantBooleanType(false);
$constantScalars[] = true;
$constantScalars[] = false;
}
} else {
$constantScalars = $argType->getConstantScalarTypes();
$constantScalars = $argType->getConstantScalarValues();
}

$lengths = [];
foreach ($constantScalars as $constantScalar) {
$stringScalar = $constantScalar->toString();
if (!($stringScalar instanceof ConstantStringType)) {
$lengths = [];
break;
}
$length = strlen($stringScalar->getValue());
$stringScalar = (string) $constantScalar;
$length = strlen($stringScalar);
$lengths[] = $length;
}

Expand Down

0 comments on commit 2048fc8

Please sign in to comment.