Skip to content

Commit 087fdeb

Browse files
authored
StrlenFunctionReturnTypeExtension: Cleanup instanceof ConstantString
1 parent 3d0a658 commit 087fdeb

3 files changed

+7
-28
lines changed

phpstan-baseline.neon

-12
Original file line numberDiff line numberDiff line change
@@ -1545,12 +1545,6 @@ parameters:
15451545
count: 2
15461546
path: src/Type/Php/LtrimFunctionReturnTypeExtension.php
15471547

1548-
-
1549-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
1550-
identifier: phpstanApi.instanceofType
1551-
count: 1
1552-
path: src/Type/Php/MbStrlenFunctionReturnTypeExtension.php
1553-
15541548
-
15551549
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
15561550
identifier: phpstanApi.instanceofType
@@ -1629,12 +1623,6 @@ parameters:
16291623
count: 1
16301624
path: src/Type/Php/StrRepeatFunctionReturnTypeExtension.php
16311625

1632-
-
1633-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
1634-
identifier: phpstanApi.instanceofType
1635-
count: 1
1636-
path: src/Type/Php/StrlenFunctionReturnTypeExtension.php
1637-
16381626
-
16391627
message: '#^Doing instanceof PHPStan\\Type\\ObjectType is error\-prone and deprecated\. Use Type\:\:isObject\(\) or Type\:\:getObjectClassNames\(\) instead\.$#'
16401628
identifier: phpstanApi.instanceofType

src/Type/Php/MbStrlenFunctionReturnTypeExtension.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -92,24 +92,20 @@ public function getTypeFromFunctionCall(
9292
}
9393

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

9797
$lengths = [];
9898
foreach ($constantScalars as $constantScalar) {
99-
$stringScalar = $constantScalar->toString();
100-
if (!($stringScalar instanceof ConstantStringType)) {
101-
$lengths = [];
102-
break;
103-
}
99+
$stringScalar = (string) $constantScalar;
104100

105101
foreach ($encodings as $encoding) {
106102
if (!$this->isSupportedEncoding($encoding)) {
107103
continue;
108104
}
109105

110-
$length = @mb_strlen($stringScalar->getValue(), $encoding);
106+
$length = @mb_strlen($stringScalar, $encoding);
111107
if ($length === false) {
112-
throw new ShouldNotHappenException(sprintf('Got false on a supported encoding %s and value %s', $encoding, var_export($stringScalar->getValue(), true)));
108+
throw new ShouldNotHappenException(sprintf('Got false on a supported encoding %s and value %s', $encoding, var_export($stringScalar, true)));
113109
}
114110
$lengths[] = $length;
115111
}

src/Type/Php/StrlenFunctionReturnTypeExtension.php

+3-8
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use PHPStan\Analyser\Scope;
77
use PHPStan\Reflection\FunctionReflection;
88
use PHPStan\Type\Constant\ConstantIntegerType;
9-
use PHPStan\Type\Constant\ConstantStringType;
109
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
1110
use PHPStan\Type\FloatType;
1211
use PHPStan\Type\IntegerRangeType;
@@ -42,16 +41,12 @@ public function getTypeFromFunctionCall(
4241
}
4342

4443
$argType = $scope->getType($args[0]->value);
45-
$constantScalars = $argType->getConstantScalarTypes();
44+
$constantScalars = $argType->getConstantScalarValues();
4645

4746
$lengths = [];
4847
foreach ($constantScalars as $constantScalar) {
49-
$stringScalar = $constantScalar->toString();
50-
if (!($stringScalar instanceof ConstantStringType)) {
51-
$lengths = [];
52-
break;
53-
}
54-
$length = strlen($stringScalar->getValue());
48+
$stringScalar = (string) $constantScalar;
49+
$length = strlen($stringScalar);
5550
$lengths[] = $length;
5651
}
5752

0 commit comments

Comments
 (0)