forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add rule to refactor exception methods
- Loading branch information
Showing
10 changed files
with
351 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
services: | ||
Rector\PHPUnit\Rector\Class_\TestListenerToHooksRector: null | ||
Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/PHPUnit/src/Rector/MethodCall/ExplicitPhpErrorApiRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use Rector\Rector\AbstractPHPUnitRector; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
|
||
/** | ||
* @see https://github.com/sebastianbergmann/phpunit/blob/master/ChangeLog-9.0.md | ||
* @see https://github.com/sebastianbergmann/phpunit/commit/1ba2e3e1bb091acda3139f8a9259fa8161f3242d | ||
* @see \Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\ExplicitPhpErrorApiRectorTest | ||
*/ | ||
final class ExplicitPhpErrorApiRector extends AbstractPHPUnitRector | ||
{ | ||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition( | ||
'Use explicit API for expecting PHP errors, warnings, and notices', | ||
[ | ||
new CodeSample( | ||
<<<'PHP' | ||
final class SomeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class); | ||
$this->expectException(\PHPUnit\Framework\TestCase\Error::class); | ||
$this->expectException(\PHPUnit\Framework\TestCase\Notice::class); | ||
$this->expectException(\PHPUnit\Framework\TestCase\Warning::class); | ||
} | ||
} | ||
PHP | ||
, | ||
<<<'PHP' | ||
final class SomeTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectDeprecation(); | ||
$this->expectError(); | ||
$this->expectNotice(); | ||
$this->expectWarning(); | ||
} | ||
} | ||
PHP | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $this->isPHPUnitMethodNames($node, ['expectException'])) { | ||
return null; | ||
} | ||
|
||
$replacements = [ | ||
'PHPUnit\Framework\TestCase\Notice' => 'expectNotice', | ||
'PHPUnit\Framework\TestCase\Deprecated' => 'expectDeprecation', | ||
'PHPUnit\Framework\TestCase\Error' => 'expectError', | ||
'PHPUnit\Framework\TestCase\Warning' => 'expectWarning', | ||
]; | ||
|
||
foreach ($replacements as $class => $method) { | ||
$this->replaceExceptionWith($node, $class, $method); | ||
} | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
* @param string $exceptionClass | ||
* @param string $explicitMethod | ||
*/ | ||
private function replaceExceptionWith(Node $node, $exceptionClass, $explicitMethod): void | ||
{ | ||
if (isset($node->args[0]) && property_exists( | ||
$node->args[0]->value, | ||
'class' | ||
) && (string) $node->args[0]->value->class === $exceptionClass) { | ||
$this->replaceNode($node, $this->createPHPUnitCallWithName($node, $explicitMethod)); | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...PUnit/tests/Rector/MethodCall/ExplicitPhpErrorApiRector/ExplicitPhpErrorApiRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector; | ||
|
||
use Iterator; | ||
use Rector\PHPUnit\Rector\MethodCall\ExplicitPhpErrorApiRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ExplicitPhpErrorApiRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideDataForTest() | ||
*/ | ||
public function test(string $file): void | ||
{ | ||
$this->doTestFile($file); | ||
} | ||
|
||
public function provideDataForTest(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
protected function getRectorClass(): string | ||
{ | ||
return ExplicitPhpErrorApiRector::class; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...PHPUnit/tests/Rector/MethodCall/ExplicitPhpErrorApiRector/Fixture/php_combination.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorCombination extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class); | ||
$this->expectException(\PHPUnit\Framework\TestCase\Error::class); | ||
$this->expectException(\PHPUnit\Framework\TestCase\Notice::class); | ||
$this->expectException(\PHPUnit\Framework\TestCase\Warning::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorCombination extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectDeprecation(); | ||
$this->expectError(); | ||
$this->expectNotice(); | ||
$this->expectWarning(); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...PHPUnit/tests/Rector/MethodCall/ExplicitPhpErrorApiRector/Fixture/php_deprecation.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorDeprecated extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\PHPUnit\Framework\TestCase\Deprecated::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorDeprecated extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectDeprecation(); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
packages/PHPUnit/tests/Rector/MethodCall/ExplicitPhpErrorApiRector/Fixture/php_error.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorError extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\PHPUnit\Framework\TestCase\Error::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorError extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectError(); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...ages/PHPUnit/tests/Rector/MethodCall/ExplicitPhpErrorApiRector/Fixture/php_notice.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorNotice extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\PHPUnit\Framework\TestCase\Notice::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorNotice extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectNotice(); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
.../tests/Rector/MethodCall/ExplicitPhpErrorApiRector/Fixture/php_standard_exception.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DoNotRefactorException extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\Exception::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class DoNotRefactorException extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\Exception::class); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...ges/PHPUnit/tests/Rector/MethodCall/ExplicitPhpErrorApiRector/Fixture/php_warning.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorWarning extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectException(\PHPUnit\Framework\TestCase\Warning::class); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\Rector\MethodCall\ExplicitPhpErrorApiRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class RefactorWarning extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->expectWarning(); | ||
} | ||
} | ||
|
||
?> |