-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Understand the exact array shape coming from `Nette\Utils\Strings::ma…
…tchAll()` based on pattern
- Loading branch information
1 parent
5f97dfa
commit 14a68a9
Showing
4 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/Type/Nette/StringsMatchAllDynamicReturnTypeExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Nette; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\DynamicStaticMethodReturnTypeExtension; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class StringsMatchAllDynamicReturnTypeExtension implements DynamicStaticMethodReturnTypeExtension | ||
{ | ||
|
||
/** @var RegexArrayShapeMatcher */ | ||
private $regexArrayShapeMatcher; | ||
|
||
public function __construct(RegexArrayShapeMatcher $regexArrayShapeMatcher) | ||
{ | ||
$this->regexArrayShapeMatcher = $regexArrayShapeMatcher; | ||
} | ||
|
||
public function getClass(): string | ||
{ | ||
return Strings::class; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection): bool | ||
{ | ||
return $methodReflection->getName() === 'matchAll'; | ||
} | ||
|
||
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, Scope $scope): ?Type | ||
{ | ||
$args = $methodCall->getArgs(); | ||
$patternArg = $args[1] ?? null; | ||
if ($patternArg === null) { | ||
return null; | ||
} | ||
|
||
$flagsArg = $args[2] ?? null; | ||
$flagsType = null; | ||
if ($flagsArg !== null) { | ||
$flagsType = $scope->getType($flagsArg->value); | ||
} | ||
|
||
$arrayShape = $this->regexArrayShapeMatcher->matchAllExpr($patternArg->value, $flagsType, TrinaryLogic::createYes(), $scope); | ||
if ($arrayShape === null) { | ||
return null; | ||
} | ||
|
||
return TypeCombinator::union($arrayShape, new NullType()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14a68a9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes https://github.com/nette/utils/blob/master/src/Utils/Strings.php#L602
Ta metoda by měla vždy vracet pole, ale teď PHPStan reportuje, že může vracet
null
.14a68a9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kukulich Díky, zatemnění mysli :) Oprava vydána.