mirror of
https://github.com/rectorphp/rector.git
synced 2025-04-20 15:31:58 +02:00
Add SpecificAssertInternalTypeRector
This commit is contained in:
parent
e1a386054b
commit
a59e35678c
3
config/level/phpunit/phpunit80.yml
Normal file
3
config/level/phpunit/phpunit80.yml
Normal file
@ -0,0 +1,3 @@
|
||||
services:
|
||||
Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsRector: ~
|
||||
Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector: ~
|
@ -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, '#(?<name>[a-zA-Z_-]+])#');
|
||||
if (isset($match['name'])) {
|
||||
return $this->levelsDirectory . '/' . $match['name'] . '/' . $level;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -0,0 +1,110 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\PHPUnit\Rector\MethodCall;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Identifier;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Rector\AbstractPHPUnitRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-8.0.md
|
||||
* @see https://github.com/sebastianbergmann/phpunit/commit/a406c85c51edd76ace29119179d8c21f590c939e
|
||||
*/
|
||||
final class SpecificAssertInternalTypeRector extends AbstractPHPUnitRector
|
||||
{
|
||||
/**
|
||||
* @var string[][]
|
||||
*/
|
||||
private $typeToMethod = [
|
||||
'array' => ['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;
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
final class SomeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$value = 'value';
|
||||
$this->assertInternalType('string', $value);
|
||||
$this->assertInternalType('array', $value);
|
||||
$this->assertNotInternalType('bool', $value, 'message');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
final class SomeTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
public function test()
|
||||
{
|
||||
$value = 'value';
|
||||
$this->assertIsString($value);
|
||||
$this->assertIsArray($value);
|
||||
$this->assertIsNotBool($value, 'message');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,19 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\SpecificAssertInternalTypeRector;
|
||||
|
||||
use Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class SpecificAssertInternalTypeRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
public function test(): void
|
||||
{
|
||||
$this->doTestFiles([__DIR__ . '/Fixture/fixture.php.inc']);
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return SpecificAssertInternalTypeRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
services:
|
||||
Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector: ~
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user