This commit is contained in:
TomasVotruba 2017-09-08 20:56:56 +02:00
parent 7f27fe39f6
commit 135bee7d28
4 changed files with 41 additions and 53 deletions

View File

@ -21,13 +21,13 @@ final class ExtractCommand extends Command
private const ARGUMENT_SOURCE_NAME = 'source';
/**
* @var DeprecationExtractor
* @var deprecationExtractor
*/
private $DeprecationExtractor;
private $deprecationExtractor;
public function __construct(DeprecationExtractor $DeprecationExtractor)
public function __construct(deprecationExtractor $deprecationExtractor)
{
$this->DeprecationExtractor = $DeprecationExtractor;
$this->deprecationExtractor = $deprecationExtractor;
parent::__construct();
}
@ -46,7 +46,7 @@ final class ExtractCommand extends Command
protected function execute(InputInterface $input, OutputInterface $output): int
{
$source = $input->getArgument(self::ARGUMENT_SOURCE_NAME);
$this->DeprecationExtractor->scanDirectories($source);
$this->deprecationExtractor->scanDirectories($source);
// write found deprecations...

View File

@ -11,11 +11,21 @@ final class DeprecationCollector
*/
private $deprecations = [];
/**
* @var string[]
*/
private $deprecationMessages = [];
public function addDeprecation(DeprecationInterface $deprecation): void
{
$this->deprecations[] = $deprecation;
}
public function addDeprecationMessage(string $deprecationMessage): void
{
$this->deprecationMessages[] = $deprecationMessage;
}
/**
* @return DeprecationInterface[]
*/

View File

@ -1,9 +1,5 @@
<?php declare(strict_types=1);
/**
* @todo rename to deprecation extractor
*/
namespace Rector\DeprecationExtractor\NodeVisitor;
use Nette\Utils\Strings;
@ -24,11 +20,6 @@ use Rector\DeprecationExtractor\Deprecation\DeprecationFactory;
*/
final class DeprecationDetector extends NodeVisitorAbstract
{
/**
* @var string[]
*/
private const DEPRECATEABLE_NODES = [ClassLike::class, ClassMethod::class, Function_::class];
/**
* @var DeprecationCollector
*/
@ -62,52 +53,31 @@ final class DeprecationDetector extends NodeVisitorAbstract
}
public function enterNode(Node $node): void
{
if (! $this->isDeprecateableNode($node)) {
return;
}
if (! $this->hasDeprecation($node)) {
return;
}
$scope = $node->getAttribute(Attribute::SCOPE);
/** @var FuncCall $node */
$deprecation = $this->deprecationFactory->createFromNode($node->args[0]->value, $scope);
$this->deprecationCollector->addDeprecation($deprecation);
}
private function isDeprecateableNode(Node $node): bool
{
foreach (self::DEPRECATEABLE_NODES as $deprecateableNode) {
if (is_a($node, $deprecateableNode, true)) {
return true;
}
}
return false;
}
private function hasDeprecation(Node $node): bool
{
if ($this->docBlockAnalyzer->hasAnnotation($node, 'deprecated')) {
return true;
$this->processDocBlockDeprecation($node);
return;
}
if ($this->hasTriggerErrorUserDeprecated($node)) {
return true;
}
dump($node);
die;
return false;
$scope = $node->getAttribute(Attribute::SCOPE);
$this->deprecationCollector->addDeprecation($deprecation);
return;
}
}
private function hasTriggerErrorUserDeprecated(Node $node): bool
{
return Strings::contains(
$this->prettyPrinter->prettyPrint([$node]),
'E_USER_DEPRECATED'
);
return Strings::contains($this->prettyPrinter->prettyPrint([$node]), 'E_USER_DEPRECATED');
}
private function processDocBlockDeprecation(Node $node): void
{
$deprecation = $this->docBlockAnalyzer->getAnnotationFromNode($node, 'deprecated');
$this->deprecationCollector->addDeprecationMessage($deprecation);
}
}

View File

@ -37,8 +37,16 @@ final class DocBlockAnalyzer
return '';
}
if (count($annotationTags) === 1 && $annotationTags[0]->getTag()->getName() === 'var') {
return implode('|', $annotationTags[0]->getTypes());
if (count($annotationTags) === 1) {
$type = $annotationTags[0]->getTag()->getName();
if ($type === 'var') {
return implode('|', $annotationTags[0]->getTypes());
}
if ($type === 'deprecated') {
$content = $annotationTags[0]->getContent();
return ltrim($content, '* @deprecated ');
}
}
throw new NotImplementedException(sprintf(