Skip to content

Commit

Permalink
mb_* fatal errors on php8 with invalid encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
clxmstaab committed Feb 1, 2022
1 parent af38f03 commit 0018251
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/Type/Php/MbFunctionsReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\NeverType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
Expand Down Expand Up @@ -44,7 +46,7 @@ class MbFunctionsReturnTypeExtension implements DynamicFunctionReturnTypeExtensi
'mb_ord' => 2,
];

public function __construct()
public function __construct(private PhpVersion $phpVersion)
{
$supportedEncodings = [];
if (function_exists('mb_list_encodings')) {
Expand Down Expand Up @@ -86,9 +88,14 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
}

if (count($results) === 1) {
$invalidEncodingReturn = new ConstantBooleanType(false);
if ($this->phpVersion->getVersionId() >= 80000) {
$invalidEncodingReturn = new NeverType();
}

return $results[0]
? TypeCombinator::remove($returnType, new ConstantBooleanType(false))
: new ConstantBooleanType(false);
: $invalidEncodingReturn;
}

return $returnType;
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/LegacyNodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5314,7 +5314,7 @@ public function dataFunctions(): array
'$mbStrlenWithValidEncodingAlias',
],
[
'false',
PHP_VERSION_ID < 80000 ? 'false' : '*NEVER*',
'$mbStrlenWithInvalidEncoding',
],
[
Expand Down Expand Up @@ -5390,7 +5390,7 @@ public function dataFunctions(): array
'$mbEncodingAliasesWithValidEncoding',
],
[
'false',
PHP_VERSION_ID < 80000 ? 'false' : '*NEVER*',
'$mbEncodingAliasesWithInvalidEncoding',
],
[
Expand All @@ -5410,7 +5410,7 @@ public function dataFunctions(): array
'$mbChrWithValidEncoding',
],
[
'false',
PHP_VERSION_ID < 80000 ? 'false' : '*NEVER*',
'$mbChrWithInvalidEncoding',
],
[
Expand All @@ -5430,7 +5430,7 @@ public function dataFunctions(): array
'$mbOrdWithValidEncoding',
],
[
'false',
PHP_VERSION_ID < 80000 ? 'false' : '*NEVER*',
'$mbOrdWithInvalidEncoding',
],
[
Expand Down

0 comments on commit 0018251

Please sign in to comment.