mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-20 01:52:22 +00:00
Merge pull request #2833 from Aerendir/2824-annotate-throwables
#2824: Automatically add to DocBlock comments the thrown `\Throwables`.
This commit is contained in:
commit
470238c71e
|
@ -1712,6 +1712,30 @@ Adds array default value to property to prevent foreach over null error
|
|||
|
||||
<br>
|
||||
|
||||
### `AnnotateThrowablesRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\Throw_\AnnotateThrowablesRector`
|
||||
|
||||
Adds @throws DocBlock comments to methods that thrwo \Throwables.
|
||||
|
||||
```diff
|
||||
class RootExceptionInMethodWithDocblock
|
||||
{
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
+ * @throws \RuntimeException
|
||||
*/
|
||||
public function throwException(int $code)
|
||||
{
|
||||
throw new \RuntimeException('', $code);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
<br>
|
||||
|
||||
### `BinarySwitchToIfElseRector`
|
||||
|
||||
- class: `Rector\CodingStyle\Rector\Switch_\BinarySwitchToIfElseRector`
|
||||
|
|
|
@ -0,0 +1,235 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Rector\Throw_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\New_;
|
||||
use PhpParser\Node\Expr\StaticCall;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\Node\Stmt\Function_;
|
||||
use PhpParser\Node\Stmt\Throw_;
|
||||
use PhpParser\Node\Stmt\Use_;
|
||||
use PhpParser\Node\Stmt\UseUse;
|
||||
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwareGenericTagValueNode;
|
||||
use Rector\AttributeAwarePhpDoc\Ast\PhpDoc\AttributeAwarePhpDocTagNode;
|
||||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
||||
use Rector\Core\Exception\ShouldNotHappenException;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
use Rector\Core\RectorDefinition\CodeSample;
|
||||
use Rector\Core\RectorDefinition\RectorDefinition;
|
||||
use Rector\NodeTypeResolver\Node\AttributeKey;
|
||||
|
||||
/**
|
||||
* Adds "throws" DocBlock to methods.
|
||||
*/
|
||||
final class AnnotateThrowablesRector extends AbstractRector
|
||||
{
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Throw_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* From this method documentation is generated.
|
||||
*/
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition(
|
||||
'Adds @throws DocBlock comments to methods that thrwo \Throwables.', [
|
||||
new CodeSample(
|
||||
// code before
|
||||
<<<'PHP'
|
||||
class RootExceptionInMethodWithDocblock
|
||||
{
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
*/
|
||||
public function throwException(int $code)
|
||||
{
|
||||
throw new \RuntimeException('', $code);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
// code after
|
||||
<<<'PHP'
|
||||
class RootExceptionInMethodWithDocblock
|
||||
{
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function throwException(int $code)
|
||||
{
|
||||
throw new \RuntimeException('', $code);
|
||||
}
|
||||
}
|
||||
PHP
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Throw_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if ($this->isThrowableAnnotated($node)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$this->annotateThrowable($node);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function isThrowableAnnotated(Throw_ $node): bool
|
||||
{
|
||||
$identifiedThrownThrowables = $this->identifyThrownThrowables($node);
|
||||
$phpDocInfo = $this->getThrowingStmtDocblock($node);
|
||||
$alreadyAnnotatedThrowTags = $phpDocInfo->getTagsByName('throws');
|
||||
|
||||
if (empty($alreadyAnnotatedThrowTags)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$alreadyAnnotatedThrowClassNames = array_map(
|
||||
static function (AttributeAwarePhpDocTagNode $alreadyAnnotatedThrowTag): string {
|
||||
return $alreadyAnnotatedThrowTag->value->type->name;
|
||||
},
|
||||
$alreadyAnnotatedThrowTags
|
||||
);
|
||||
|
||||
$notAnnotatedThrowables = [];
|
||||
foreach ($identifiedThrownThrowables as $identifiedThrownThrowable) {
|
||||
if ($this->isIdentifiedThrownThrowableInAlreadyAnnotatedOnes(
|
||||
$node,
|
||||
$alreadyAnnotatedThrowClassNames,
|
||||
$identifiedThrownThrowable
|
||||
) === false) {
|
||||
$notAnnotatedThrowables[] = $identifiedThrownThrowable;
|
||||
}
|
||||
}
|
||||
|
||||
return empty($notAnnotatedThrowables);
|
||||
}
|
||||
|
||||
private function isIdentifiedThrownThrowableInAlreadyAnnotatedOnes(
|
||||
Throw_ $node,
|
||||
array $alreadyAnnotatedThrowClassNames,
|
||||
string $identifiedThrownThrowable
|
||||
): bool {
|
||||
foreach ($alreadyAnnotatedThrowClassNames as $alreadyAnnotatedThrowClassName) {
|
||||
if ($alreadyAnnotatedThrowClassName === $identifiedThrownThrowable) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (
|
||||
! Strings::contains($alreadyAnnotatedThrowClassName, '\\') &&
|
||||
Strings::contains($identifiedThrownThrowable, $alreadyAnnotatedThrowClassName) &&
|
||||
$this->isThrowableImported($node)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function identifyThrownThrowables(Throw_ $node): array
|
||||
{
|
||||
switch (get_class($node->expr)) {
|
||||
case New_::class:
|
||||
return [$this->buildFQN($node)];
|
||||
case StaticCall::class:
|
||||
return $this->identifyThrownThrowablesInStaticCall($node);
|
||||
default:
|
||||
// throw new ShouldNotHappenException(sprintf('The \Throwable %s is not supported. Please, open an issue.', get_class($node->expr)));
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private function identifyThrownThrowablesInStaticCall(Throw_ $node): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
private function isThrowableImported(Throw_ $node): bool
|
||||
{
|
||||
$throwClassName = $this->getName($node->expr->class);
|
||||
$useNodes = $node->getAttribute(AttributeKey::USE_NODES);
|
||||
|
||||
if ($useNodes === null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var Use_ $useNode */
|
||||
foreach ($useNodes as $useNode) {
|
||||
/** @var UseUse $useStmt */
|
||||
foreach ($useNode->uses as $useStmt) {
|
||||
if ($this->getName($useStmt) === $throwClassName) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function annotateThrowable(Throw_ $node): void
|
||||
{
|
||||
$throwClass = $this->buildFQN($node);
|
||||
$docComment = $this->buildThrowsDocComment($throwClass);
|
||||
|
||||
$throwingStmtDocblock = $this->getThrowingStmtDocblock($node);
|
||||
$throwingStmtDocblock->addPhpDocTagNode($docComment);
|
||||
}
|
||||
|
||||
private function buildThrowsDocComment(string $FQNOrThrowableName): AttributeAwarePhpDocTagNode
|
||||
{
|
||||
$value = new AttributeAwareGenericTagValueNode($FQNOrThrowableName);
|
||||
return new AttributeAwarePhpDocTagNode('@throws', $value);
|
||||
}
|
||||
|
||||
private function buildFQN(Throw_ $node): string
|
||||
{
|
||||
return '\\' . $this->getName($node->expr->class);
|
||||
}
|
||||
|
||||
private function getThrowingStmtDocblock(Throw_ $node): PhpDocInfo
|
||||
{
|
||||
$stmt = $this->getThrowingStmt($node);
|
||||
|
||||
return $stmt->getAttribute(AttributeKey::PHP_DOC_INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ClassMethod|Function_
|
||||
*
|
||||
* @throws ShouldNotHappenException
|
||||
*/
|
||||
private function getThrowingStmt(Throw_ $node): Stmt
|
||||
{
|
||||
$method = $node->getAttribute(AttributeKey::METHOD_NODE);
|
||||
$function = $node->getAttribute(AttributeKey::FUNCTION_NODE);
|
||||
$stmt = $method ?? $function ?? null;
|
||||
|
||||
if ($stmt === null) {
|
||||
throw new ShouldNotHappenException();
|
||||
}
|
||||
|
||||
return $stmt;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowablesRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodingStyle\Rector\Throw_\AnnotateThrowablesRector;
|
||||
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class AnnotateThrowablesRectorTest 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 AnnotateThrowablesRector::class;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
function throwCustomExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
function throwCustomExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
*/
|
||||
function throwCustomExceptionInFunctionWithDockblock(int $code)
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException('', $code);
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
function throwCustomExceptionInFunctionWithDockblock(int $code)
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException('', $code);
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
function throwCustomExceptionInFunctionWithoutDockblock()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
function throwCustomExceptionInFunctionWithoutDockblock()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionInMethodWithDocblock
|
||||
{
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
*/
|
||||
public function throwException(int $code)
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException('', $code);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionInMethodWithDocblock
|
||||
{
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
public function throwException(int $code)
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException('', $code);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionInMethodWithoutDocblock
|
||||
{
|
||||
public function throwException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionInMethodWithoutDocblock
|
||||
{
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
function throwCustomExceptionThrownInTwoFunctionsFirst()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
function throwCustomExceptionThrownInTwoFunctionsSecond()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
function throwCustomExceptionThrownInTwoFunctionsFirst()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
function throwCustomExceptionThrownInTwoFunctionsSecond()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionThrownInTwoMethods
|
||||
{
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
public function anotherThrowException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class CustomExceptionThrownInTwoMethods
|
||||
{
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException
|
||||
*/
|
||||
public function anotherThrowException()
|
||||
{
|
||||
throw new \Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
|
||||
/**
|
||||
* @throws TheException
|
||||
*/
|
||||
function throwCustomImportedExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new TheException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
|
||||
/**
|
||||
* @throws TheException
|
||||
*/
|
||||
function throwCustomImportedExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new TheException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
|
||||
class CustomImportedExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws TheException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
|
||||
class CustomImportedExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws TheException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new TheException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwRootExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwRootExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
*/
|
||||
function throwRootExceptionInFunctionWithDocblock(int $code)
|
||||
{
|
||||
throw new \RuntimeException('', $code);
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwRootExceptionInFunctionWithDocblock(int $code)
|
||||
{
|
||||
throw new \RuntimeException('', $code);
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
function throwRootExceptionInFunctionWithoutDocblock()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwRootExceptionInFunctionWithoutDocblock()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionInMethodWithDocblock
|
||||
{
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
*/
|
||||
public function throwException(int $code)
|
||||
{
|
||||
throw new \RuntimeException('', $code);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionInMethodWithDocblock
|
||||
{
|
||||
/**
|
||||
* This is a comment.
|
||||
*
|
||||
* @param int $code
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function throwException(int $code)
|
||||
{
|
||||
throw new \RuntimeException('', $code);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionInMethodWithoutDocblock
|
||||
{
|
||||
public function throwException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionInMethodWithoutDocblock
|
||||
{
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwRootExceptionThrownInTwoFunctionsFirst()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
function throwRootExceptionThrownInTwoFunctionsSecond()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwRootExceptionThrownInTwoFunctionsFirst()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwRootExceptionThrownInTwoFunctionsSecond()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionThrownInTwoMethods
|
||||
{
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
public function anotherThrowException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
class RootExceptionThrownInTwoMethods
|
||||
{
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function anotherThrowException()
|
||||
{
|
||||
throw new \RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use \RuntimeException;
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
function throwRootImportedExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use \RuntimeException;
|
||||
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
function throwRootImportedExceptionAlreadyAnnotatedInFunction()
|
||||
{
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use \RuntimeException;
|
||||
|
||||
class RootImportedExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use \RuntimeException;
|
||||
|
||||
class RootImportedExceptionAlreadyAnnotatedInMethod
|
||||
{
|
||||
/**
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function throwException()
|
||||
{
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryNoReturnTypeHingting
|
||||
{
|
||||
public static function createException(int $code)
|
||||
{
|
||||
return new TheException();
|
||||
}
|
||||
}
|
||||
|
||||
function throwWithFactoryMethodNoReturnTypeHinting()
|
||||
{
|
||||
throw ExceptionsFactoryNotAnnotated::createException(1);
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryNoReturnTypeHingting
|
||||
{
|
||||
public static function createException(int $code)
|
||||
{
|
||||
return new TheException();
|
||||
}
|
||||
}
|
||||
|
||||
function throwWithFactoryMethodNoReturnTypeHinting()
|
||||
{
|
||||
throw ExceptionsFactoryNotAnnotated::createException(1);
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryMethodNothingAnnotated
|
||||
{
|
||||
public function cercoQuestoMetodoQui(int $code)
|
||||
{
|
||||
switch ($code) {
|
||||
case 1:
|
||||
return new TheException();
|
||||
case 2:
|
||||
return new TheExceptionTheSecond();
|
||||
case 3:
|
||||
return new TheExceptionTheThird();
|
||||
default:
|
||||
return new \RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function throwWithFactoryMethodNotAnnotated()
|
||||
{
|
||||
$factory = new ExceptionsFactoryMethodNothingAnnotated();
|
||||
throw $factory->cercoQuestoMetodoQui(1);
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryMethodNothingAnnotated
|
||||
{
|
||||
public function cercoQuestoMetodoQui(int $code)
|
||||
{
|
||||
switch ($code) {
|
||||
case 1:
|
||||
return new TheException();
|
||||
case 2:
|
||||
return new TheExceptionTheSecond();
|
||||
case 3:
|
||||
return new TheExceptionTheThird();
|
||||
default:
|
||||
return new \RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function throwWithFactoryMethodNotAnnotated()
|
||||
{
|
||||
$factory = new ExceptionsFactoryMethodNothingAnnotated();
|
||||
throw $factory->cercoQuestoMetodoQui(1);
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryStaticMethodNothingAnnotated
|
||||
{
|
||||
public static function createException(int $code)
|
||||
{
|
||||
switch ($code) {
|
||||
case 1:
|
||||
return new TheException();
|
||||
case 2:
|
||||
return new TheExceptionTheSecond();
|
||||
case 3:
|
||||
return new TheExceptionTheThird();
|
||||
default:
|
||||
return new \RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function throwWithFactoryStaticMethodNotAnnotated()
|
||||
{
|
||||
throw ExceptionsFactoryStaticMethodNothingAnnotated::createException(1);
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryStaticMethodNothingAnnotated
|
||||
{
|
||||
public static function createException(int $code)
|
||||
{
|
||||
switch ($code) {
|
||||
case 1:
|
||||
return new TheException();
|
||||
case 2:
|
||||
return new TheExceptionTheSecond();
|
||||
case 3:
|
||||
return new TheExceptionTheThird();
|
||||
default:
|
||||
return new \RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TheException
|
||||
* @throws TheExceptionTheSecond
|
||||
* @throws TheExceptionTheThird
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwWithFactoryStaticMethodNotAnnotated()
|
||||
{
|
||||
throw ExceptionsFactoryStaticMethodNothingAnnotated::createException(1);
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryStaticMethodWithReturnDockblock
|
||||
{
|
||||
/**
|
||||
* @param int $code
|
||||
*
|
||||
* @return TheException|TheExceptionTheSecond|TheExceptionTheThird|\RuntimeException
|
||||
*/
|
||||
public static function createExceptionEccolo(int $code)
|
||||
{
|
||||
switch ($code) {
|
||||
case 1:
|
||||
return new TheException();
|
||||
case 2:
|
||||
return new TheExceptionTheSecond();
|
||||
case 3:
|
||||
return new TheExceptionTheThird();
|
||||
default:
|
||||
return new \RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function throwWithFactoryStaticMethodWithReturnDockblock()
|
||||
{
|
||||
throw ExceptionsFactoryStaticMethodWithReturnDockblock::createExceptionEccolo(1);
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Fixture;
|
||||
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheException;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheSecond;
|
||||
use Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source\TheExceptionTheThird;
|
||||
|
||||
class ExceptionsFactoryStaticMethodWithReturnDockblock
|
||||
{
|
||||
/**
|
||||
* @param int $code
|
||||
*
|
||||
* @return TheException|TheExceptionTheSecond|TheExceptionTheThird|\RuntimeException
|
||||
*/
|
||||
public static function createExceptionEccolo(int $code)
|
||||
{
|
||||
switch ($code) {
|
||||
case 1:
|
||||
return new TheException();
|
||||
case 2:
|
||||
return new TheExceptionTheSecond();
|
||||
case 3:
|
||||
return new TheExceptionTheThird();
|
||||
default:
|
||||
return new \RuntimeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws TheException
|
||||
* @throws TheExceptionTheSecond
|
||||
* @throws TheExceptionTheThird
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
function throwWithFactoryStaticMethodWithReturnDockblock()
|
||||
{
|
||||
throw ExceptionsFactoryStaticMethodWithReturnDockblock::createExceptionEccolo(1);
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source;
|
||||
|
||||
class TheException extends \RuntimeException {}
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source;
|
||||
|
||||
class TheExceptionTheSecond extends \RuntimeException {}
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodingStyle\Tests\Rector\Throw_\AnnotateThrowables\Source;
|
||||
|
||||
class TheExceptionTheThird extends \RuntimeException {}
|
Loading…
Reference in New Issue
Block a user