-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from thecodingmachine/preg_replace
Adding Safe\preg_replace support
- Loading branch information
Showing
5 changed files
with
133 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
79 changes: 79 additions & 0 deletions
79
src/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtension.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,79 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
|
||
namespace TheCodingMachine\Safe\PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeUtils; | ||
|
||
class ReplaceSafeFunctionsDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
/** @var array<string, int> */ | ||
private $functions = [ | ||
'Safe\preg_replace' => 2, | ||
]; | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return array_key_exists($functionReflection->getName(), $this->functions); | ||
} | ||
|
||
public function getTypeFromFunctionCall( | ||
FunctionReflection $functionReflection, | ||
FuncCall $functionCall, | ||
Scope $scope | ||
): Type { | ||
$type = $this->getPreliminarilyResolvedTypeFromFunctionCall($functionReflection, $functionCall, $scope); | ||
|
||
$possibleTypes = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
|
||
if (TypeCombinator::containsNull($possibleTypes)) { | ||
$type = TypeCombinator::addNull($type); | ||
} | ||
|
||
return $type; | ||
} | ||
|
||
private function getPreliminarilyResolvedTypeFromFunctionCall( | ||
FunctionReflection $functionReflection, | ||
FuncCall $functionCall, | ||
Scope $scope | ||
): Type { | ||
$argumentPosition = $this->functions[$functionReflection->getName()]; | ||
if (count($functionCall->args) <= $argumentPosition) { | ||
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
$subjectArgumentType = $scope->getType($functionCall->args[$argumentPosition]->value); | ||
$defaultReturnType = ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
if ($subjectArgumentType instanceof MixedType) { | ||
return TypeUtils::toBenevolentUnion($defaultReturnType); | ||
} | ||
$stringType = new StringType(); | ||
$arrayType = new ArrayType(new MixedType(), new MixedType()); | ||
|
||
$isStringSuperType = $stringType->isSuperTypeOf($subjectArgumentType); | ||
$isArraySuperType = $arrayType->isSuperTypeOf($subjectArgumentType); | ||
$compareSuperTypes = $isStringSuperType->compareTo($isArraySuperType); | ||
if ($compareSuperTypes === $isStringSuperType) { | ||
return $stringType; | ||
} elseif ($compareSuperTypes === $isArraySuperType) { | ||
if ($subjectArgumentType instanceof ArrayType) { | ||
return $subjectArgumentType->generalizeValues(); | ||
} | ||
return $subjectArgumentType; | ||
} | ||
|
||
return $defaultReturnType; | ||
} | ||
} |
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,42 @@ | ||
<?php | ||
|
||
namespace TheCodingMachine\Safe\PHPStan\Rules; | ||
|
||
use PHPStan\Rules\FunctionCallParametersCheck; | ||
use PHPStan\Rules\Methods\CallMethodsRule; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
use TheCodingMachine\Safe\PHPStan\Type\Php\ReplaceSafeFunctionsDynamicReturnTypeExtension; | ||
|
||
class CallMethodRuleTest extends RuleTestCase | ||
{ | ||
protected function getRule(): Rule | ||
{ | ||
$broker = $this->createBroker(); | ||
$ruleLevelHelper = new RuleLevelHelper($broker, true, true, true); | ||
return new CallMethodsRule( | ||
$broker, | ||
new FunctionCallParametersCheck($ruleLevelHelper, true, true), | ||
$ruleLevelHelper, | ||
true, | ||
true | ||
); | ||
} | ||
|
||
public function testSafePregReplace() | ||
{ | ||
// FIXME: this rule actually runs code but will always return no error because the rule executed is not the correct one. | ||
// This provides code coverage but assert is not ok. | ||
$this->analyse([__DIR__ . '/data/safe_pregreplace.php'], []); | ||
} | ||
|
||
|
||
/** | ||
* @return \PHPStan\Type\DynamicFunctionReturnTypeExtension[] | ||
*/ | ||
public function getDynamicFunctionReturnTypeExtensions(): array | ||
{ | ||
return [new ReplaceSafeFunctionsDynamicReturnTypeExtension()]; | ||
} | ||
} |
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,7 @@ | ||
<?php | ||
use function Safe\preg_replace; | ||
|
||
$x = preg_replace('/foo/', 'bar', 'baz'); | ||
$y = stripos($x, 'foo'); | ||
|
||
$x = preg_replace(['/foo/'], ['bar'], ['baz']); |