From b6fb373a56c6cd75db650a2b7e1807a93a08fe6c Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Mon, 16 Dec 2019 14:14:56 +0100 Subject: [PATCH] [PHPUnit] Add get_class double sided to AssertCompareToSpecificMethodRector --- .../AssertCompareToSpecificMethodRector.php | 68 ++++++++++--------- .../Fixture/count.php.inc | 35 ++++++++++ .../Fixture/fixture.php.inc | 6 -- .../Fixture/get_class.php.inc | 35 ++++++++++ .../ConstFetch/BarewordStringRector.php | 1 - rector.yaml | 41 +---------- 6 files changed, 110 insertions(+), 76 deletions(-) create mode 100644 packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/count.php.inc create mode 100644 packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/get_class.php.inc diff --git a/packages/PHPUnit/src/Rector/SpecificMethod/AssertCompareToSpecificMethodRector.php b/packages/PHPUnit/src/Rector/SpecificMethod/AssertCompareToSpecificMethodRector.php index 0aa49f65a135..a83453c418c1 100644 --- a/packages/PHPUnit/src/Rector/SpecificMethod/AssertCompareToSpecificMethodRector.php +++ b/packages/PHPUnit/src/Rector/SpecificMethod/AssertCompareToSpecificMethodRector.php @@ -5,6 +5,7 @@ namespace Rector\PHPUnit\Rector\SpecificMethod; use PhpParser\Node; +use PhpParser\Node\Arg; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; @@ -37,20 +38,8 @@ public function getDefinition(): RectorDefinition '$this->assertCount(10, $anything, "message");' ), new CodeSample( - '$this->assertSame($value, {function}($anything), "message");', - '$this->assert{function}($value, $anything, "message");' - ), - new CodeSample( - '$this->assertEquals($value, {function}($anything), "message");', - '$this->assert{function}($value, $anything, "message");' - ), - new CodeSample( - '$this->assertNotSame($value, {function}($anything), "message");', - '$this->assertNot{function}($value, $anything, "message")' - ), - new CodeSample( - '$this->assertNotEquals($value, {function}($anything), "message");', - '$this->assertNot{function}($value, $anything, "message")' + '$this->assertNotEquals(get_class($value), stdClass::class);', + '$this->assertNotInstanceOf(stdClass::class, $value);' ), ]); } @@ -72,28 +61,25 @@ public function refactor(Node $node): ?Node return null; } + // we need 2 args if (! isset($node->args[1])) { return null; } - $secondArgumentValue = $node->args[1]->value; - if (! $secondArgumentValue instanceof FuncCall) { - return null; - } + $firstArgument = $node->args[0]; + $secondArgument = $node->args[1]; + $firstArgumentValue = $firstArgument->value; + $secondArgumentValue = $secondArgument->value; - $name = $this->getName($secondArgumentValue); - if ($name === null) { - return null; + if ($secondArgumentValue instanceof FuncCall) { + return $this->processFuncCallArgumentValue($node, $secondArgumentValue, $firstArgument); } - if (! isset($this->defaultOldToNewMethods[$name])) { - return null; + if ($firstArgumentValue instanceof FuncCall) { + return $this->processFuncCallArgumentValue($node, $firstArgumentValue, $secondArgument); } - $this->renameMethod($node, $name); - $this->moveFunctionArgumentsUp($node); - - return $node; + return null; } /** @@ -117,10 +103,30 @@ private function renameMethod(Node $node, string $funcName): void * Handles custom error messages to not be overwrite by function with multiple args. * @param StaticCall|MethodCall $node */ - private function moveFunctionArgumentsUp(Node $node): void + private function moveFunctionArgumentsUp(Node $node, FuncCall $funcCall, Arg $requiredArg): void + { + $node->args[1] = $funcCall->args[0]; + $node->args[0] = $requiredArg; + } + + /** + * @param MethodCall|StaticCall $node + * @return MethodCall|StaticCall|null + */ + private function processFuncCallArgumentValue(Node $node, FuncCall $funcCall, Arg $requiredArg): ?Node { - /** @var FuncCall $secondArgument */ - $secondArgument = $node->args[1]->value; - $node->args[1] = $secondArgument->args[0]; + $name = $this->getName($funcCall); + if ($name === null) { + return null; + } + + if (! isset($this->defaultOldToNewMethods[$name])) { + return null; + } + + $this->renameMethod($node, $name); + $this->moveFunctionArgumentsUp($node, $funcCall, $requiredArg); + + return $node; } } diff --git a/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/count.php.inc b/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/count.php.inc new file mode 100644 index 000000000000..7f10cbb2b7b2 --- /dev/null +++ b/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/count.php.inc @@ -0,0 +1,35 @@ +assertSame(5, count($something)); + $this->assertEquals(10, iterator_count($something)); + + $count = 92; + $this->assertNotEquals($count, sizeof($something), 'third argument'); + } +} + +?> +----- +assertCount(5, $something); + $this->assertCount(10, $something); + + $count = 92; + $this->assertNotCount($count, $something, 'third argument'); + } +} + +?> diff --git a/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/fixture.php.inc b/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/fixture.php.inc index 45300f12e681..9e7537418a73 100644 --- a/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/fixture.php.inc +++ b/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/fixture.php.inc @@ -6,9 +6,6 @@ final class MyTest extends \PHPUnit\Framework\TestCase { public function test() { - $this->assertSame(5, count($something)); - $this->assertEquals(10, iterator_count($something)); - $this->assertNotEquals($count, sizeof($something), 'third argument'); $this->assertEquals('string', gettype($something)); $this->assertEquals('string', $something['property']()); $this->assertNotSame($foo[1]->result, count($this->results)); @@ -28,9 +25,6 @@ final class MyTest extends \PHPUnit\Framework\TestCase { public function test() { - $this->assertCount(5, $something); - $this->assertCount(10, $something); - $this->assertNotCount($count, $something, 'third argument'); $this->assertInternalType('string', $something); $this->assertEquals('string', $something['property']()); $this->assertNotCount($foo[1]->result, $this->results); diff --git a/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/get_class.php.inc b/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/get_class.php.inc new file mode 100644 index 000000000000..bd1bfd50ef2d --- /dev/null +++ b/packages/PHPUnit/tests/Rector/SpecificMethod/AssertCompareToSpecificMethodRector/Fixture/get_class.php.inc @@ -0,0 +1,35 @@ +assertSame(get_class($something), 'stdClass'); + self::assertSame('stdClass', get_class($something)); + } +} + +?> +----- +assertInstanceOf('stdClass', $something); + self::assertInstanceOf('stdClass', $something); + } +} + +?> diff --git a/packages/Php72/src/Rector/ConstFetch/BarewordStringRector.php b/packages/Php72/src/Rector/ConstFetch/BarewordStringRector.php index caf727cbb957..4702c8dc6596 100644 --- a/packages/Php72/src/Rector/ConstFetch/BarewordStringRector.php +++ b/packages/Php72/src/Rector/ConstFetch/BarewordStringRector.php @@ -8,7 +8,6 @@ use PhpParser\Node; use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Scalar\String_; -use Rector\Exception\ShouldNotHappenException; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\Rector\AbstractRector; use Rector\RectorDefinition\CodeSample; diff --git a/rector.yaml b/rector.yaml index 4e84b8a9da39..7841076a2f48 100644 --- a/rector.yaml +++ b/rector.yaml @@ -1,3 +1,6 @@ +imports: + - { resource: "create-rector.yaml", ignore_errors: true } + services: Rector\PSR4\Rector\Namespace_\NormalizeNamespaceByPSR4ComposerAutoloadRector: ~ @@ -15,41 +18,3 @@ parameters: # so Rector code is still PHP 7.2 compatible php_version_features: '7.2' - - # @see utils/RectorGenerator/config/config.yaml - rector_recipe: - # run "bin/rector create" to create a new Rector + tests from this config - package: "Phalcon" - name: "AddRequestToHandleMethodCallRector" - node_types: - # put main node first, it is used to create namespace - - "MethodCall" - - description: "Add $_SERVER REQUEST_URI to method call" - code_before: > - handle(); - } - } - - code_after: > - handle($_SERVER["REQUEST_URI"]); - } - } - - source: # e.g. link to RFC or headline in upgrade guide, 1 or more in the list - - "https://github.com/rectorphp/rector/issues/2408" - set: "phalcon40" # e.g. symfony30, target config to append this rector to