-
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.
Support nette 4 for Strings::replace + support regexp array shapes fo…
…r Strings::replace callback
- Loading branch information
1 parent
f41257b
commit 83f2356
Showing
5 changed files
with
159 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Creating new PHPStan\\\\Reflection\\\\Native\\\\NativeParameterReflection is not covered by backward compatibility promise\\. The class might change in a minor PHPStan version\\.$#" | ||
count: 1 | ||
path: src/Type/Nette/StringsReplaceCallbackClosureTypeExtension.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
97 changes: 97 additions & 0 deletions
97
src/Type/Nette/StringsReplaceCallbackClosureTypeExtension.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,97 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Nette; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\MethodReflection; | ||
use PHPStan\Reflection\Native\NativeParameterReflection; | ||
use PHPStan\Reflection\ParameterReflection; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\ClosureType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Constant\ConstantIntegerType; | ||
use PHPStan\Type\Php\RegexArrayShapeMatcher; | ||
use PHPStan\Type\StaticMethodParameterClosureTypeExtension; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use function array_key_exists; | ||
use const PREG_OFFSET_CAPTURE; | ||
use const PREG_UNMATCHED_AS_NULL; | ||
|
||
final class StringsReplaceCallbackClosureTypeExtension implements StaticMethodParameterClosureTypeExtension | ||
{ | ||
|
||
/** @var RegexArrayShapeMatcher */ | ||
private $regexArrayShapeMatcher; | ||
|
||
public function __construct(RegexArrayShapeMatcher $regexArrayShapeMatcher) | ||
{ | ||
$this->regexArrayShapeMatcher = $regexArrayShapeMatcher; | ||
} | ||
|
||
public function isStaticMethodSupported(MethodReflection $methodReflection, ParameterReflection $parameter): bool | ||
{ | ||
return $methodReflection->getDeclaringClass()->getName() === Strings::class | ||
&& $methodReflection->getName() === 'replace' | ||
&& $parameter->getName() === 'replacement'; | ||
} | ||
|
||
public function getTypeFromStaticMethodCall(MethodReflection $methodReflection, StaticCall $methodCall, ParameterReflection $parameter, Scope $scope): ?Type | ||
{ | ||
$args = $methodCall->getArgs(); | ||
$patternArg = $args[1] ?? null; | ||
|
||
if ($patternArg === null) { | ||
return null; | ||
} | ||
|
||
$replacementType = $scope->getType($args[2]->value); | ||
|
||
if (!$replacementType->isCallable()->yes()) { | ||
return null; | ||
} | ||
|
||
$matchesType = $this->regexArrayShapeMatcher->matchExpr( | ||
$patternArg->value, | ||
$this->resolveFlagsType($args, $scope), | ||
TrinaryLogic::createYes(), | ||
$scope | ||
); | ||
|
||
if ($matchesType === null) { | ||
return null; | ||
} | ||
|
||
return new ClosureType( | ||
[ | ||
new NativeParameterReflection( | ||
$parameter->getName(), | ||
$parameter->isOptional(), | ||
$matchesType, | ||
$parameter->passedByReference(), | ||
$parameter->isVariadic(), | ||
$parameter->getDefaultValue() | ||
), | ||
], | ||
new StringType() | ||
); | ||
} | ||
|
||
/** | ||
* @param array<Arg> $args | ||
*/ | ||
private function resolveFlagsType(array $args, Scope $scope): ConstantIntegerType | ||
{ | ||
$captureOffsetType = array_key_exists(4, $args) ? $scope->getType($args[4]->value) : new ConstantBooleanType(false); | ||
$unmatchedAsNullType = array_key_exists(5, $args) ? $scope->getType($args[5]->value) : new ConstantBooleanType(false); | ||
|
||
$captureOffset = $captureOffsetType->isTrue()->yes(); | ||
$unmatchedAsNull = $unmatchedAsNullType->isTrue()->yes(); | ||
|
||
return new ConstantIntegerType(($captureOffset ? PREG_OFFSET_CAPTURE : 0) | ($unmatchedAsNull ? PREG_UNMATCHED_AS_NULL : 0)); | ||
} | ||
|
||
} |
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