Skip to content
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

StrlenFunctionReturnTypeExtension: Cleanup instanceof ConstantString #3780

Merged
merged 3 commits into from
Mar 1, 2025
Merged
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
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1551,12 +1551,6 @@ parameters:
count: 2
path: src/Type/Php/LtrimFunctionReturnTypeExtension.php

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

-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
identifier: phpstanApi.instanceofType
@@ -1641,12 +1635,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\.$#'
identifier: phpstanApi.instanceofType
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\.$#'
identifier: phpstanApi.instanceofType
12 changes: 4 additions & 8 deletions src/Type/Php/MbStrlenFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -92,24 +92,20 @@ public function getTypeFromFunctionCall(
}

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

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

foreach ($encodings as $encoding) {
if (!$this->isSupportedEncoding($encoding)) {
continue;
}

$length = @mb_strlen($stringScalar->getValue(), $encoding);
$length = @mb_strlen($stringScalar, $encoding);
if ($length === false) {
throw new ShouldNotHappenException(sprintf('Got false on a supported encoding %s and value %s', $encoding, var_export($stringScalar->getValue(), true)));
throw new ShouldNotHappenException(sprintf('Got false on a supported encoding %s and value %s', $encoding, var_export($stringScalar, true)));
}
$lengths[] = $length;
}
11 changes: 3 additions & 8 deletions src/Type/Php/StrlenFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerRangeType;
@@ -42,16 +41,12 @@ public function getTypeFromFunctionCall(
}

$argType = $scope->getType($args[0]->value);
$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;
}