mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 18:00:10 +01:00
[PHPUnit] Add get_class double sided to AssertCompareToSpecific… (#2439)
[PHPUnit] Add get_class double sided to AssertCompareToSpecificMethodRector
This commit is contained in:
commit
7292171e69
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
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 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
|
||||
'$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 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
|
||||
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;
|
||||
|
||||
if ($secondArgumentValue instanceof FuncCall) {
|
||||
return $this->processFuncCallArgumentValue($node, $secondArgumentValue, $firstArgument);
|
||||
}
|
||||
|
||||
$name = $this->getName($secondArgumentValue);
|
||||
if ($name === null) {
|
||||
return null;
|
||||
if ($firstArgumentValue instanceof FuncCall) {
|
||||
return $this->processFuncCallArgumentValue($node, $firstArgumentValue, $secondArgument);
|
||||
}
|
||||
|
||||
if (! isset($this->defaultOldToNewMethods[$name])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->renameMethod($node, $name);
|
||||
$this->moveFunctionArgumentsUp($node);
|
||||
|
||||
return $node;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -117,10 +103,30 @@ final class AssertCompareToSpecificMethodRector extends AbstractPHPUnitRector
|
||||
* 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
|
||||
{
|
||||
/** @var FuncCall $secondArgument */
|
||||
$secondArgument = $node->args[1]->value;
|
||||
$node->args[1] = $secondArgument->args[0];
|
||||
$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
|
||||
{
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -8,7 +8,6 @@ use Nette\Utils\Strings;
|
||||
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;
|
||||
|
41
rector.yaml
41
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: >
|
||||
<?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
|
||||
|
Loading…
x
Reference in New Issue
Block a user