|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\Rector\MethodCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Expr\MethodCall; |
| 9 | +use PhpParser\Node\Expr\StaticCall; |
| 10 | +use PhpParser\Node\Identifier; |
| 11 | +use PHPStan\Type\StringType; |
| 12 | +use Rector\Core\Rector\AbstractPHPUnitRector; |
| 13 | +use Rector\Core\RectorDefinition\CodeSample; |
| 14 | +use Rector\Core\RectorDefinition\RectorDefinition; |
| 15 | + |
| 16 | +/** |
| 17 | + * @see https://github.com/sebastianbergmann/phpunit/issues/3426 |
| 18 | + * @see \Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertContainsWithoutIdentityRector\SpecificAssertContainsWithoutIdentityRectorTest |
| 19 | + */ |
| 20 | +final class SpecificAssertContainsWithoutIdentityRector extends AbstractPHPUnitRector |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var string[][] |
| 24 | + */ |
| 25 | + private const OLD_METHODS_NAMES_TO_NEW_NAMES = [ |
| 26 | + 'string' => [ |
| 27 | + 'assertContains' => 'assertContainsEquals', |
| 28 | + 'assertNotContains' => 'assertNotContainsEquals', |
| 29 | + ], |
| 30 | + ]; |
| 31 | + |
| 32 | + public function getDefinition(): RectorDefinition |
| 33 | + { |
| 34 | + return new RectorDefinition( |
| 35 | + 'Change assertContains()/assertNotContains() with non-strict comparison to new specific alternatives', |
| 36 | + [ |
| 37 | + new CodeSample( |
| 38 | + <<<'PHP' |
| 39 | +<?php |
| 40 | +
|
| 41 | +final class SomeTest extends \PHPUnit\Framework\TestCase |
| 42 | +{ |
| 43 | + public function test() |
| 44 | + { |
| 45 | + $objects = [ new \stdClass(), new \stdClass(), new \stdClass() ]; |
| 46 | + $this->assertContains(new \stdClass(), $objects, 'message', false, false); |
| 47 | + $this->assertNotContains(new \stdClass(), $objects, 'message', false, false); |
| 48 | + } |
| 49 | +} |
| 50 | +PHP |
| 51 | + , |
| 52 | + <<<'PHP' |
| 53 | +<?php |
| 54 | +
|
| 55 | +final class SomeTest extends TestCase |
| 56 | +{ |
| 57 | + public function test() |
| 58 | + { |
| 59 | + $objects = [ new \stdClass(), new \stdClass(), new \stdClass() ]; |
| 60 | + $this->assertContainsEquals(new \stdClass(), $objects, 'message'); |
| 61 | + $this->assertNotContainsEquals(new \stdClass(), $objects, 'message'); |
| 62 | + } |
| 63 | +} |
| 64 | +PHP |
| 65 | + ), |
| 66 | + ] |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return string[] |
| 72 | + */ |
| 73 | + public function getNodeTypes(): array |
| 74 | + { |
| 75 | + return [MethodCall::class, StaticCall::class]; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @param MethodCall|StaticCall $node |
| 80 | + */ |
| 81 | + public function refactor(Node $node): ?Node |
| 82 | + { |
| 83 | + if (! $this->isPHPUnitMethodNames($node, ['assertContains', 'assertNotContains'])) { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + //when second argument is string: do nothing |
| 88 | + if ($this->isStaticType($node->args[1]->value, StringType::class)) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + //when less then 5 arguments given: do nothing |
| 93 | + if (! isset($node->args[4]) || $node->args[4]->value === null) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + //when 5th argument check identity is true: do nothing |
| 98 | + if ($this->isValue($node->args[4]->value, true)) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + /* here we search for element of array without identity check and we can replace functions */ |
| 103 | + $methodName = $this->getName($node->name); |
| 104 | + |
| 105 | + $node->name = new Identifier(self::OLD_METHODS_NAMES_TO_NEW_NAMES['string'][$methodName]); |
| 106 | + unset($node->args[3], $node->args[4]); |
| 107 | + |
| 108 | + return $node; |
| 109 | + } |
| 110 | +} |
0 commit comments