Skip to content

Commit

Permalink
Merge pull request #10 from thecodingmachine/preg_replace
Browse files Browse the repository at this point in the history
Adding Safe\preg_replace support
  • Loading branch information
moufmouf authored Mar 7, 2019
2 parents ffbdebd + 47cbb9e commit 00f4845
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
4 changes: 4 additions & 0 deletions phpstan-safe-rule.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ services:
class: TheCodingMachine\Safe\PHPStan\Rules\UseSafeFunctionsRule
tags:
- phpstan.rules.rule
-
class: TheCodingMachine\Safe\PHPStan\Type\Php\ReplaceSafeFunctionsDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
79 changes: 79 additions & 0 deletions src/Type/Php/ReplaceSafeFunctionsDynamicReturnTypeExtension.php
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;
}
}
42 changes: 42 additions & 0 deletions tests/Rules/CallMethodRuleTest.php
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()];
}
}
1 change: 1 addition & 0 deletions tests/Rules/UseSafeFunctionsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TheCodingMachine\Safe\PHPStan\Rules;

use PHPStan\Testing\RuleTestCase;
use TheCodingMachine\Safe\PHPStan\Type\Php\ReplaceSafeFunctionsDynamicReturnTypeExtension;

class UseSafeFunctionsRuleTest extends RuleTestCase
{
Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/data/safe_pregreplace.php
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']);

0 comments on commit 00f4845

Please sign in to comment.