From 7b138c7877de32405f98f9b82d83bcf407c342b7 Mon Sep 17 00:00:00 2001 From: Jonas Elfering Date: Fri, 4 Oct 2019 17:57:14 +0200 Subject: [PATCH 1/4] Add FunctionCallToConstantRector Fixes: https://github.com/rectorphp/rector/issues/2008 --- config/set/coding-style/coding-style.yaml | 1 + .../FuncCall/FunctionCallToConstantRector.php | 191 ++++++++++++++++++ .../Fixture/pi.php.inc | 27 +++ .../Fixture/sapi-name.php.inc | 27 +++ .../Fixture/skip-version-compare.php.inc | 27 +++ .../Fixture/version-compare.php.inc | 39 ++++ .../FunctionCallToConstantRectorTest.php | 33 +++ 7 files changed, 345 insertions(+) create mode 100644 packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php create mode 100644 packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/pi.php.inc create mode 100644 packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc create mode 100644 packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc create mode 100644 packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/version-compare.php.inc create mode 100644 packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php diff --git a/config/set/coding-style/coding-style.yaml b/config/set/coding-style/coding-style.yaml index 555b97c54d09..917b8628e59e 100644 --- a/config/set/coding-style/coding-style.yaml +++ b/config/set/coding-style/coding-style.yaml @@ -29,3 +29,4 @@ services: Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector: ~ Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector: ~ Rector\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector: ~ + Rector\CodingStyle\Rector\FuncCall\FunctionCallToConstantRector: ~ diff --git a/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php new file mode 100644 index 000000000000..b17ed4269798 --- /dev/null +++ b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php @@ -0,0 +1,191 @@ + Identical::class, + '==' => Identical::class, + 'eq' => Identical::class, + '!=' => NotIdentical::class, + '<>' => NotIdentical::class, + 'ne' => NotIdentical::class, + '>' => Greater::class, + 'gt' => Greater::class, + '<' => Smaller::class, + 'lt' => Smaller::class, + '>=' => GreaterOrEqual::class, + 'ge' => GreaterOrEqual::class, + '<=' => SmallerOrEqual::class, + 'le' => SmallerOrEqual::class, + ]; + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Changes use of function calls to use constants', [ + new CodeSample( + <<isName($node, 'version_compare')) { + return $this->refactorVersionCompare($node); + } + + if ($this->isName($node, 'php_sapi_name')) { + return new ConstFetch(new Name('PHP_SAPI')); + } + + if ($this->isName($node, 'pi')) { + return new ConstFetch(new Name('M_PI')); + } + + return null; + } + + private function refactorVersionCompare(Node $node): ?Node + { + if (count($node->args) !== 3) { + return null; + } + + if (!$this->isPhpVersionConstant($node->args[0]->value) && !$this->isPhpVersionConstant($node->args[1]->value)) { + return null; + } + + $left = $this->getNewNodeForArg($node->args[0]->value); + $right = $this->getNewNodeForArg($node->args[1]->value); + + $comparison = $this->operatorToComparison[$node->args[2]->value->value]; + + return new $comparison($left, $right); + } + + private function isPhpVersionConstant(Expr $value): bool + { + if ($value instanceof ConstFetch && $value->name->toString() === 'PHP_VERSION') { + return true; + } + + return false; + } + + private function getNewNodeForArg(Expr $value): Node + { + if ($this->isPhpVersionConstant($value)) { + return new ConstFetch(new Name('PHP_VERSION_ID')); + } + + return $this->getVersionNumberFormVersionString($value); + } + + private function getVersionNumberFormVersionString(Expr $value): LNumber + { + if (! $value instanceof String_ && ! preg_match('/^\d+\.\d+\.\d+$/', $value->value)) { + throw new ShouldNotHappenException(); + } + + $versionParts = explode('.', $value->value); + + return new LNumber($versionParts[0] * 10000 + $versionParts[1] * 100 + $versionParts[2]); + } +} diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/pi.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/pi.php.inc new file mode 100644 index 000000000000..09db8f55bf42 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/pi.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc new file mode 100644 index 000000000000..49517c784691 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc new file mode 100644 index 000000000000..783107d6aaec --- /dev/null +++ b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc @@ -0,0 +1,27 @@ +version, '5.3.0', '<'); + } +} + +?> +----- +version, '5.3.0', '<'); + } +} + +?> diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/version-compare.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/version-compare.php.inc new file mode 100644 index 000000000000..ce4cb2a1c883 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/version-compare.php.inc @@ -0,0 +1,39 @@ +'); + version_compare(PHP_VERSION, '5.3.0', '='); + + version_compare('5.3.0', PHP_VERSION, 'lt'); + version_compare('5.3.0', PHP_VERSION, 'gt'); + version_compare('5.3.0', PHP_VERSION, 'eq'); + } +} + +?> +----- + 50300; + PHP_VERSION_ID === 50300; + + 50300 < PHP_VERSION_ID; + 50300 > PHP_VERSION_ID; + 50300 === PHP_VERSION_ID; + } +} + +?> diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php new file mode 100644 index 000000000000..db591a4d78e0 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php @@ -0,0 +1,33 @@ +doTestFile($file); + } + + public function provideDataForTest(): Iterator + { + yield [__DIR__ . '/Fixture/version-compare.php.inc']; + yield [__DIR__ . '/Fixture/sapi-name.php.inc']; + yield [__DIR__ . '/Fixture/pi.php.inc']; + yield [__DIR__ . '/Fixture/skip-version-compare.php.inc']; + + } + + protected function getRectorClass(): string + { + return FunctionCallToConstantRector::class; + } +} From ce91f7d58d3484194cf862aed866e77f225fabd9 Mon Sep 17 00:00:00 2001 From: Jonas Elfering Date: Fri, 4 Oct 2019 20:37:43 +0200 Subject: [PATCH 2/4] Fix phpstan errors --- .../FuncCall/FunctionCallToConstantRector.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php index b17ed4269798..b692e54cf426 100644 --- a/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php +++ b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php @@ -26,7 +26,7 @@ final class FunctionCallToConstantRector extends AbstractRector { /** - * @var string[][] + * @var string[]string */ private $operatorToComparison = [ '=' => Identical::class, @@ -49,7 +49,7 @@ public function getDefinition(): RectorDefinition { return new RectorDefinition('Changes use of function calls to use constants', [ new CodeSample( - <<value)) { + if (! $value instanceof String_) { + throw new ShouldNotHappenException(); + } + + if (! preg_match('/^\d+\.\d+\.\d+$/', $value->value)) { throw new ShouldNotHappenException(); } $versionParts = explode('.', $value->value); - return new LNumber($versionParts[0] * 10000 + $versionParts[1] * 100 + $versionParts[2]); + return new LNumber((int) $versionParts[0] * 10000 + (int) $versionParts[1] * 100 + (int) $versionParts[2]); } } From 8e54c1068da1c86824988569e2e368287129c8ef Mon Sep 17 00:00:00 2001 From: Jonas Elfering Date: Fri, 4 Oct 2019 21:03:27 +0200 Subject: [PATCH 3/4] Split specific and generic rector --- config/set/coding-style/coding-style.yaml | 6 +- .../FuncCall/FunctionCallToConstantRector.php | 117 ++------------- ...VersionCompareFuncCallToConstantRector.php | 140 ++++++++++++++++++ .../Fixture/{pi.php.inc => fixture.php.inc} | 8 +- .../Fixture/sapi-name.php.inc | 27 ---- .../Fixture/skip-version-compare.php.inc | 27 ---- .../FunctionCallToConstantRectorTest.php | 18 ++- .../Fixture/skip-version-compare.php.inc | 13 ++ .../Fixture/version-compare.php.inc | 4 +- ...ionCompareFuncCallToConstantRectorTest.php | 32 ++++ 10 files changed, 219 insertions(+), 173 deletions(-) create mode 100644 packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php rename packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/{pi.php.inc => fixture.php.inc} (77%) delete mode 100644 packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc delete mode 100644 packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc create mode 100644 packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture/skip-version-compare.php.inc rename packages/CodingStyle/tests/Rector/FuncCall/{FunctionCallToConstantRector => VersionCompareFuncCallToConstantRector}/Fixture/version-compare.php.inc (77%) create mode 100644 packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/VersionCompareFuncCallToConstantRectorTest.php diff --git a/config/set/coding-style/coding-style.yaml b/config/set/coding-style/coding-style.yaml index 917b8628e59e..679225987c37 100644 --- a/config/set/coding-style/coding-style.yaml +++ b/config/set/coding-style/coding-style.yaml @@ -29,4 +29,8 @@ services: Rector\CodingStyle\Rector\Class_\AddArrayDefaultToArrayPropertyRector: ~ Rector\CodingStyle\Rector\ClassMethod\MakeInheritedMethodVisibilitySameAsParentRector: ~ Rector\CodingStyle\Rector\FuncCall\CallUserFuncCallToVariadicRector: ~ - Rector\CodingStyle\Rector\FuncCall\FunctionCallToConstantRector: ~ + Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector: ~ + Rector\CodingStyle\Rector\FuncCall\FunctionCallToConstantRector: + $functionsToConstants: + php_sapi_name: PHP_SAPI + pi: M_PI diff --git a/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php index b692e54cf426..631ca005dd3c 100644 --- a/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php +++ b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php @@ -3,19 +3,9 @@ namespace Rector\CodingStyle\Rector\FuncCall; use PhpParser\Node; -use PhpParser\Node\Expr; -use PhpParser\Node\Expr\BinaryOp\Greater; -use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; -use PhpParser\Node\Expr\BinaryOp\Identical; -use PhpParser\Node\Expr\BinaryOp\NotIdentical; -use PhpParser\Node\Expr\BinaryOp\Smaller; -use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\LNumber; -use PhpParser\Node\Scalar\String_; -use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; use Rector\RectorDefinition\RectorDefinition; @@ -28,47 +18,16 @@ final class FunctionCallToConstantRector extends AbstractRector /** * @var string[]string */ - private $operatorToComparison = [ - '=' => Identical::class, - '==' => Identical::class, - 'eq' => Identical::class, - '!=' => NotIdentical::class, - '<>' => NotIdentical::class, - 'ne' => NotIdentical::class, - '>' => Greater::class, - 'gt' => Greater::class, - '<' => Smaller::class, - 'lt' => Smaller::class, - '>=' => GreaterOrEqual::class, - 'ge' => GreaterOrEqual::class, - '<=' => SmallerOrEqual::class, - 'le' => SmallerOrEqual::class, - ]; + private $functionsToConstants; - public function getDefinition(): RectorDefinition - { - return new RectorDefinition('Changes use of function calls to use constants', [ - new CodeSample( - <<<'EOS' -class SomeClass -{ - public function run() + public function __construct(array $functionsToConstants = []) { - version_compare(PHP_VERSION, '5.3.0', '<'); + $this->functionsToConstants = $functionsToConstants; } -} -EOS - , - <<<'EOS' -class SomeClass -{ - public function run() + + public function getDefinition(): RectorDefinition { - PHP_VERSION_ID < 50300; - } -} -EOS - ), + return new RectorDefinition('Changes use of function calls to use constants', [ new CodeSample( <<<'EOS' class SomeClass @@ -127,69 +86,11 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($this->isName($node, 'version_compare')) { - return $this->refactorVersionCompare($node); - } - - if ($this->isName($node, 'php_sapi_name')) { - return new ConstFetch(new Name('PHP_SAPI')); - } - - if ($this->isName($node, 'pi')) { - return new ConstFetch(new Name('M_PI')); - } - - return null; - } - - private function refactorVersionCompare(Node $node): ?Node - { - if (count($node->args) !== 3) { - return null; - } - - if (!$this->isPhpVersionConstant($node->args[0]->value) && !$this->isPhpVersionConstant($node->args[1]->value)) { + $functionName = $this->getName($node); + if (! array_key_exists($functionName, $this->functionsToConstants)) { return null; } - $left = $this->getNewNodeForArg($node->args[0]->value); - $right = $this->getNewNodeForArg($node->args[1]->value); - - $comparison = $this->operatorToComparison[$node->args[2]->value->value]; - - return new $comparison($left, $right); - } - - private function isPhpVersionConstant(Expr $value): bool - { - if ($value instanceof ConstFetch && $value->name->toString() === 'PHP_VERSION') { - return true; - } - - return false; - } - - private function getNewNodeForArg(Expr $value): Node - { - if ($this->isPhpVersionConstant($value)) { - return new ConstFetch(new Name('PHP_VERSION_ID')); - } - - return $this->getVersionNumberFormVersionString($value); - } - - private function getVersionNumberFormVersionString(Expr $value): LNumber - { - if (! $value instanceof String_) { - throw new ShouldNotHappenException(); - } - - if (! preg_match('/^\d+\.\d+\.\d+$/', $value->value)) { - throw new ShouldNotHappenException(); - } - - $versionParts = explode('.', $value->value); - - return new LNumber((int) $versionParts[0] * 10000 + (int) $versionParts[1] * 100 + (int) $versionParts[2]); + return new ConstFetch(new Name($this->functionsToConstants[$functionName])); } } diff --git a/packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php b/packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php new file mode 100644 index 000000000000..1a70f948d991 --- /dev/null +++ b/packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php @@ -0,0 +1,140 @@ + Identical::class, + '==' => Identical::class, + 'eq' => Identical::class, + '!=' => NotIdentical::class, + '<>' => NotIdentical::class, + 'ne' => NotIdentical::class, + '>' => Greater::class, + 'gt' => Greater::class, + '<' => Smaller::class, + 'lt' => Smaller::class, + '>=' => GreaterOrEqual::class, + 'ge' => GreaterOrEqual::class, + '<=' => SmallerOrEqual::class, + 'le' => SmallerOrEqual::class, + ]; + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition('Changes use of call to version compare function to use of PHP version constant', [ + new CodeSample( + <<<'EOS' +class SomeClass +{ + public function run() + { + version_compare(PHP_VERSION, '5.3.0', '<'); + } +} +EOS + , + <<<'EOS' +class SomeClass +{ + public function run() + { + PHP_VERSION_ID < 50300; + } +} +EOS + ), + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [FuncCall::class]; + } + + /** + * @param FuncCall $node + */ + public function refactor(Node $node): ?Node + { + if (!$this->isName($node, 'version_compare')) { + return null; + } + + if (count($node->args) !== 3) { + return null; + } + + if (!$this->isPhpVersionConstant($node->args[0]->value) && !$this->isPhpVersionConstant($node->args[1]->value)) { + return null; + } + + $left = $this->getNewNodeForArg($node->args[0]->value); + $right = $this->getNewNodeForArg($node->args[1]->value); + + $comparison = $this->operatorToComparison[$node->args[2]->value->value]; + + return new $comparison($left, $right); + } + + private function isPhpVersionConstant(Expr $value): bool + { + if ($value instanceof ConstFetch && $value->name->toString() === 'PHP_VERSION') { + return true; + } + + return false; + } + + private function getNewNodeForArg(Expr $value): Node + { + if ($this->isPhpVersionConstant($value)) { + return new ConstFetch(new Name('PHP_VERSION_ID')); + } + + return $this->getVersionNumberFormVersionString($value); + } + + private function getVersionNumberFormVersionString(Expr $value): LNumber + { + if (! $value instanceof String_) { + throw new ShouldNotHappenException(); + } + + if (! preg_match('/^\d+\.\d+\.\d+$/', $value->value)) { + throw new ShouldNotHappenException(); + } + + $versionParts = explode('.', $value->value); + + return new LNumber((int) $versionParts[0] * 10000 + (int) $versionParts[1] * 100 + (int) $versionParts[2]); + } +} diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/pi.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/fixture.php.inc similarity index 77% rename from packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/pi.php.inc rename to packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/fixture.php.inc index 09db8f55bf42..c9daf5cd58c5 100644 --- a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/pi.php.inc +++ b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/fixture.php.inc @@ -2,11 +2,13 @@ namespace Rector\CodingStyle\Tests\Rector\FuncCall\FunctionCallToConstantRector\Fixture; -class PiClass +class SomeClass { public function run() { $value = pi(); + + $value2 = php_sapi_name(); } } @@ -16,11 +18,13 @@ class PiClass namespace Rector\CodingStyle\Tests\Rector\FuncCall\FunctionCallToConstantRector\Fixture; -class PiClass +class SomeClass { public function run() { $value = M_PI; + + $value2 = PHP_SAPI; } } diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc deleted file mode 100644 index 49517c784691..000000000000 --- a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/sapi-name.php.inc +++ /dev/null @@ -1,27 +0,0 @@ - ------ - diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc deleted file mode 100644 index 783107d6aaec..000000000000 --- a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/skip-version-compare.php.inc +++ /dev/null @@ -1,27 +0,0 @@ -version, '5.3.0', '<'); - } -} - -?> ------ -version, '5.3.0', '<'); - } -} - -?> diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php index db591a4d78e0..ce572941a1db 100644 --- a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php +++ b/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/FunctionCallToConstantRectorTest.php @@ -3,8 +3,10 @@ namespace Rector\CodingStyle\Tests\Rector\FuncCall\FunctionCallToConstantRector; use Iterator; +use Rector\Autodiscovery\Rector\FileSystem\MoveServicesBySuffixToDirectoryRector; use Rector\CodingStyle\Rector\FuncCall\ConsistentImplodeRector; use Rector\CodingStyle\Rector\FuncCall\FunctionCallToConstantRector; +use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector; use Rector\Testing\PHPUnit\AbstractRectorTestCase; final class FunctionCallToConstantRectorTest extends AbstractRectorTestCase @@ -19,15 +21,19 @@ public function test(string $file): void public function provideDataForTest(): Iterator { - yield [__DIR__ . '/Fixture/version-compare.php.inc']; - yield [__DIR__ . '/Fixture/sapi-name.php.inc']; - yield [__DIR__ . '/Fixture/pi.php.inc']; - yield [__DIR__ . '/Fixture/skip-version-compare.php.inc']; + yield [__DIR__ . '/Fixture/fixture.php.inc']; } - protected function getRectorClass(): string + protected function getRectorsWithConfiguration(): array { - return FunctionCallToConstantRector::class; + return [ + FunctionCallToConstantRector::class => [ + '$functionsToConstants' => [ + 'php_sapi_name' => 'PHP_SAPI', + 'pi' => 'M_PI' + ], + ], + ]; } } diff --git a/packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture/skip-version-compare.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture/skip-version-compare.php.inc new file mode 100644 index 000000000000..e3f04fbf9a43 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture/skip-version-compare.php.inc @@ -0,0 +1,13 @@ +version, '5.3.0', '<'); + } +} + +?> diff --git a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/version-compare.php.inc b/packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture/version-compare.php.inc similarity index 77% rename from packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/version-compare.php.inc rename to packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture/version-compare.php.inc index ce4cb2a1c883..a14560273291 100644 --- a/packages/CodingStyle/tests/Rector/FuncCall/FunctionCallToConstantRector/Fixture/version-compare.php.inc +++ b/packages/CodingStyle/tests/Rector/FuncCall/VersionCompareFuncCallToConstantRector/Fixture/version-compare.php.inc @@ -1,6 +1,6 @@ doTestFile($file); + } + + public function provideDataForTest(): Iterator + { + yield [__DIR__ . '/Fixture/version-compare.php.inc']; + yield [__DIR__ . '/Fixture/skip-version-compare.php.inc']; + + } + + protected function getRectorClass(): string + { + return VersionCompareFuncCallToConstantRector::class; + } +} From b52a1730770f388b31c9c12877bf86970a9216ff Mon Sep 17 00:00:00 2001 From: Jonas Elfering Date: Fri, 4 Oct 2019 21:14:16 +0200 Subject: [PATCH 4/4] Fix phpstan errors --- .../src/Rector/FuncCall/FunctionCallToConstantRector.php | 5 ++++- .../FuncCall/VersionCompareFuncCallToConstantRector.php | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php index 631ca005dd3c..e1935c989c1c 100644 --- a/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php +++ b/packages/CodingStyle/src/Rector/FuncCall/FunctionCallToConstantRector.php @@ -20,6 +20,9 @@ final class FunctionCallToConstantRector extends AbstractRector */ private $functionsToConstants; + /** + * @param string[] $functionsToConstants + */ public function __construct(array $functionsToConstants = []) { $this->functionsToConstants = $functionsToConstants; @@ -87,7 +90,7 @@ public function getNodeTypes(): array public function refactor(Node $node): ?Node { $functionName = $this->getName($node); - if (! array_key_exists($functionName, $this->functionsToConstants)) { + if (! $functionName || ! array_key_exists($functionName, $this->functionsToConstants)) { return null; } diff --git a/packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php b/packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php index 1a70f948d991..564e30d66338 100644 --- a/packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php +++ b/packages/CodingStyle/src/Rector/FuncCall/VersionCompareFuncCallToConstantRector.php @@ -100,7 +100,9 @@ public function refactor(Node $node): ?Node $left = $this->getNewNodeForArg($node->args[0]->value); $right = $this->getNewNodeForArg($node->args[1]->value); - $comparison = $this->operatorToComparison[$node->args[2]->value->value]; + /** @var String_ $operator */ + $operator = $node->args[2]->value; + $comparison = $this->operatorToComparison[$operator->value]; return new $comparison($left, $right); }