Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);'
),
]);
}
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\Rector\SpecificMethod\AssertCompareToSpecificMethodRector\Fixture;

final class Count extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertSame(5, count($something));
$this->assertEquals(10, iterator_count($something));

$count = 92;
$this->assertNotEquals($count, sizeof($something), 'third argument');
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\Rector\SpecificMethod\AssertCompareToSpecificMethodRector\Fixture;

final class Count extends \PHPUnit\Framework\TestCase
{
public function test()
{
$this->assertCount(5, $something);
$this->assertCount(10, $something);

$count = 92;
$this->assertNotCount($count, $something, 'third argument');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\Rector\SpecificMethod\AssertCompareToSpecificMethodRector\Fixture;

use stdClass;

final class GetClass extends \PHPUnit\Framework\TestCase
{
public function test()
{
$something = new stdClass();
$this->assertSame(get_class($something), 'stdClass');
self::assertSame('stdClass', get_class($something));
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\Rector\SpecificMethod\AssertCompareToSpecificMethodRector\Fixture;

use stdClass;

final class GetClass extends \PHPUnit\Framework\TestCase
{
public function test()
{
$something = new stdClass();
$this->assertInstanceOf('stdClass', $something);
self::assertInstanceOf('stdClass', $something);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 3 additions & 38 deletions rector.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
imports:
- { resource: "create-rector.yaml", ignore_errors: true }

services:
Rector\PSR4\Rector\Namespace_\NormalizeNamespaceByPSR4ComposerAutoloadRector: ~

Expand All @@ -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: >
<?php

class SomeClass
{
public function run($di)
{
$application = new \Phalcon\Mvc\Application();
$response = $application->handle();
}
}

code_after: >
<?php

class SomeClass
{
public function run($di)
{
$application = new \Phalcon\Mvc\Application();
$response = $application->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