[PHPUnit] finish ExceptionAnnotationRector

This commit is contained in:
TomasVotruba 2017-11-05 17:35:51 +01:00
parent 3f08549901
commit d7e56e025a
4 changed files with 38 additions and 13 deletions

View File

@ -5,3 +5,5 @@ checkers:
- PhpCsFixer\Fixer\Import\OrderedImportsFixer
# remove extra spaces from DocBlocks
- PhpCsFixer\Fixer\Comment\NoTrailingWhitespaceInCommentFixer
# clean empty doc blocks, e.g. after annotation removal
- PhpCsFixer\Fixer\Phpdoc\NoEmptyPhpdocFixer

View File

@ -57,7 +57,7 @@ final class MethodCallNodeFactory
array $arguments
): MethodCall {
$methodCallNode = $this->createWithVariableNameAndMethodName($variableName, $methodName);
$methodCallNode->args = $arguments;
$methodCallNode->args = $this->nodeFactory->createArgs($arguments);
return $methodCallNode;
}

View File

@ -5,7 +5,9 @@ namespace Rector\Rector\Contrib\PHPUnit;
use phpDocumentor\Reflection\DocBlock\Tags\Generic;
use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Node\MethodCallNodeFactory;
use Rector\Node\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\ReflectionDocBlock\NodeAnalyzer\DocBlockAnalyzer;
@ -30,10 +32,19 @@ final class ExceptionAnnotationRector extends AbstractRector
*/
private $docBlockAnalyzer;
public function __construct(MethodCallNodeFactory $methodCallNodeFactory, DocBlockAnalyzer $docBlockAnalyzer)
{
/**
* @var NodeFactory
*/
private $nodeFactory;
public function __construct(
MethodCallNodeFactory $methodCallNodeFactory,
DocBlockAnalyzer $docBlockAnalyzer,
NodeFactory $nodeFactory
) {
$this->methodCallNodeFactory = $methodCallNodeFactory;
$this->docBlockAnalyzer = $docBlockAnalyzer;
$this->nodeFactory = $nodeFactory;
}
public function isCandidate(Node $node): bool
@ -64,21 +75,31 @@ final class ExceptionAnnotationRector extends AbstractRector
/** @var Generic[] $tags */
$tags = $this->docBlockAnalyzer->getTagsByName($classMethodNode, $annotation);
foreach ($tags as $tag) {
$annotationContent = (string) $tag->getDescription();
$methodCall = $this->methodCallNodeFactory->createWithVariableNameMethodNameAndArguments(
'this',
$method,
[$annotationContent]
);
$methodCallExpressions = [];
// @todo
// $this->prependNodeInsideNode($methodCall, $classMethodNode);
foreach ($tags as $tag) {
$methodCallExpressions[] = $this->createMethodCallExpressionFromTag($tag, $method);
}
$classMethodNode->stmts = array_merge($methodCallExpressions, $classMethodNode->stmts);
$this->docBlockAnalyzer->removeAnnotationFromNode($classMethodNode, $annotation);
}
return $classMethodNode;
}
private function createMethodCallExpressionFromTag(Generic $genericTag, string $method): Expression
{
$annotationContent = (string)$genericTag->getDescription();
$annotationContent = ltrim($annotationContent, '\\'); // this is needed due to BuilderHelpers
$methodCall = $this->methodCallNodeFactory->createWithVariableNameMethodNameAndArguments(
'this',
$method,
[$annotationContent]
);
return new Expression($methodCall);
}
}

View File

@ -1,9 +1,11 @@
<?php
final class MyTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$this->setExpectedException(\FooException::class);
$this->expectException('FooException');
// some code
}
}