diff --git a/config/level/phpunit/phpunit80.yml b/config/level/phpunit/phpunit80.yml new file mode 100644 index 00000000000..69f494a3880 --- /dev/null +++ b/config/level/phpunit/phpunit80.yml @@ -0,0 +1,3 @@ +services: + Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsRector: ~ + Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector: ~ diff --git a/packages/ContributorTools/src/Configuration/ConfigurationFactory.php b/packages/ContributorTools/src/Configuration/ConfigurationFactory.php index a44e5e0c733..f66fad922e5 100644 --- a/packages/ContributorTools/src/Configuration/ConfigurationFactory.php +++ b/packages/ContributorTools/src/Configuration/ConfigurationFactory.php @@ -139,6 +139,12 @@ final class ConfigurationFactory /** @var SplFileInfo[] $fileInfos */ $fileInfos = iterator_to_array($finder->getIterator()); if (! count($fileInfos)) { + // assume new one is created + $match = Strings::match($level, '#(?[a-zA-Z_-]+])#'); + if (isset($match['name'])) { + return $this->levelsDirectory . '/' . $match['name'] . '/' . $level; + } + return null; } diff --git a/packages/ContributorTools/src/TemplateVariablesFactory.php b/packages/ContributorTools/src/TemplateVariablesFactory.php index a5f85a303b7..3913aef0629 100644 --- a/packages/ContributorTools/src/TemplateVariablesFactory.php +++ b/packages/ContributorTools/src/TemplateVariablesFactory.php @@ -50,7 +50,7 @@ final class TemplateVariablesFactory { if (Strings::contains($code, PHP_EOL)) { // multi lines - return sprintf("<<<'CODE_SAMPLE'%s%sCODE_SAMPLE%s", PHP_EOL, $code, PHP_EOL); + return sprintf("<<<'CODE_SAMPLE'%s%s%sCODE_SAMPLE%s", PHP_EOL, $code, PHP_EOL, PHP_EOL); } // single line diff --git a/packages/PHPUnit/src/Rector/MethodCall/SpecificAssertInternalTypeRector.php b/packages/PHPUnit/src/Rector/MethodCall/SpecificAssertInternalTypeRector.php new file mode 100644 index 00000000000..5e148a95fe5 --- /dev/null +++ b/packages/PHPUnit/src/Rector/MethodCall/SpecificAssertInternalTypeRector.php @@ -0,0 +1,110 @@ + ['assertIsArray', 'assertIsNotArray'], + 'bool' => ['assertIsBool', 'assertIsNotBool'], + 'float' => ['assertIsFloat', 'assertIsNotFloat'], + 'int' => ['assertIsInt', 'assertIsNotInt'], + 'numeric' => ['assertIsNumeric', 'assertIsNotNumeric'], + 'object' => ['assertIsObject', 'assertIsNotObject'], + 'resource' => ['assertIsResource', 'assertIsNotResource'], + 'string' => ['assertIsString', 'assertIsNotString'], + 'scalar' => ['assertIsScalar', 'assertIsNotScalar'], + 'callable' => ['assertIsCallable', 'assertIsNotCallable'], + 'iterable' => ['assertIsIterable', 'assertIsNotIterable'], + ]; + + public function getDefinition(): RectorDefinition + { + return new RectorDefinition( + 'Change assertInternalType()/assertNotInternalType() method to new specific alternatives', + [ + new CodeSample( + <<<'CODE_SAMPLE' +final class SomeTest extends \PHPUnit\Framework\TestCase +{ + public function test() + { + $value = 'value'; + $this->assertInternalType('string', $value); + $this->assertNotInternalType('array', $value); + } +} +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +final class SomeTest extends \PHPUnit\Framework\TestCase +{ + public function test() + { + $value = 'value'; + $this->assertIsString($value); + $this->assertIsNotArray($value); + } +} +CODE_SAMPLE + ), + ] + ); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [MethodCall::class, StaticCall::class]; + } + + /** + * @param MethodCall|StaticCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isInTestClass($node)) { + return null; + } + + if (! $this->isNames($node, ['assertInternalType', 'assertNotInternalType'])) { + return null; + } + + $typeNode = $node->args[0]->value; + if (! $typeNode instanceof String_) { + return null; + } + + $type = $typeNode->value; + if (! isset($this->typeToMethod[$type])) { + return null; + } + + array_shift($node->args); + + $methodName = $this->typeToMethod[$type][$this->isName($node, 'assertInternalType') ? 0 : 1]; + + $node->name = new Identifier($methodName); + + return $node; + } +} diff --git a/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/Fixture/fixture.php.inc b/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/Fixture/fixture.php.inc new file mode 100644 index 00000000000..1f70d652699 --- /dev/null +++ b/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/Fixture/fixture.php.inc @@ -0,0 +1,29 @@ +assertInternalType('string', $value); + $this->assertInternalType('array', $value); + $this->assertNotInternalType('bool', $value, 'message'); + } +} + +?> +----- +assertIsString($value); + $this->assertIsArray($value); + $this->assertIsNotBool($value, 'message'); + } +} + +?> diff --git a/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/SpecificAssertInternalTypeRectorTest.php b/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/SpecificAssertInternalTypeRectorTest.php new file mode 100644 index 00000000000..8f858274593 --- /dev/null +++ b/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/SpecificAssertInternalTypeRectorTest.php @@ -0,0 +1,19 @@ +doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']); + } + + protected function getRectorClass(): string + { + return SpecificAssertInternalTypeRector::class; + } +} diff --git a/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/config.yml b/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/config.yml new file mode 100644 index 00000000000..9a8ffc289ff --- /dev/null +++ b/packages/PHPUnit/tests/Rector/MethodCall/SpecificAssertInternalTypeRector/config.yml @@ -0,0 +1,2 @@ +services: + Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector: ~ diff --git a/src/CodingStyle/AfterRectorCodingStyle.php b/src/CodingStyle/AfterRectorCodingStyle.php index 53648eec51b..e55383a20bf 100644 --- a/src/CodingStyle/AfterRectorCodingStyle.php +++ b/src/CodingStyle/AfterRectorCodingStyle.php @@ -26,7 +26,10 @@ final class AfterRectorCodingStyle { $this->validate(); - $command = [self::ECS_BIN_PATH, 'check', '--config', self::ECS_AFTER_RECTOR_CONFIG, '--fix'] + $source; + $command = array_merge( + [self::ECS_BIN_PATH, 'check', '--config', self::ECS_AFTER_RECTOR_CONFIG, '--fix'], + $source + ); $process = new Process($command); $process->run();