mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-12 14:12:23 +00:00
[TriggerExtractor] add DeprecationCollector
This commit is contained in:
parent
e94c2957d3
commit
b932cd912e
|
@ -46,10 +46,10 @@ final class ExtractCommand extends Command
|
|||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$source = $input->getArgument(self::ARGUMENT_SOURCE_NAME);
|
||||
$foundDeprecations = $this->triggerExtractor->scanDirectories($source);
|
||||
$this->triggerExtractor->scanDirectories($source);
|
||||
|
||||
dump($foundDeprecations);
|
||||
die;
|
||||
// $output->writeln()
|
||||
// write found deprecations...
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Rector\TriggerExtractor\Deprecation;
|
||||
|
||||
final class DeprecationCollector
|
||||
{
|
||||
/**
|
||||
* @var mixed[]
|
||||
*/
|
||||
private $deprecations = [];
|
||||
|
||||
public function addDeprecation(string $deprecation): void
|
||||
{
|
||||
$this->deprecations[] = $deprecation;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function getDeprecations(): array
|
||||
{
|
||||
return $this->deprecations;
|
||||
}
|
||||
}
|
|
@ -2,7 +2,9 @@
|
|||
|
||||
namespace Rector\TriggerExtractor\NodeVisitor;
|
||||
|
||||
use Exception;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Arg;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Expr\ConstFetch;
|
||||
use PhpParser\Node\Expr\FuncCall;
|
||||
|
@ -11,9 +13,20 @@ use PhpParser\Node\Scalar\MagicConst\Method;
|
|||
use PhpParser\Node\Scalar\String_;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\NodeVisitorAbstract;
|
||||
use Rector\TriggerExtractor\Deprecation\DeprecationCollector;
|
||||
|
||||
final class DeprecationDetector extends NodeVisitorAbstract
|
||||
final class DeprecationDetector extends NodeVisitorAbstract // @todo use : class aware node visitor
|
||||
{
|
||||
/**
|
||||
* @var DeprecationCollector
|
||||
*/
|
||||
private $deprecationCollector;
|
||||
|
||||
public function __construct(DeprecationCollector $deprecationCollector)
|
||||
{
|
||||
$this->deprecationCollector = $deprecationCollector;
|
||||
}
|
||||
|
||||
public function enterNode(Node $node): void
|
||||
{
|
||||
if (! $this->isTriggerErrorUserDeprecated($node)) {
|
||||
|
@ -30,41 +43,38 @@ final class DeprecationDetector extends NodeVisitorAbstract
|
|||
$message .= $this->processConcatNode($messageNode->right);
|
||||
}
|
||||
|
||||
// @todo add to: deprecation collector
|
||||
// return $message;
|
||||
$this->deprecationCollector->addDeprecation($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* This detects: "trigger_error(<some-content>, E_USER_DEPREDCATED)";
|
||||
*/
|
||||
private function isTriggerErrorUserDeprecated(Node $node): bool
|
||||
{
|
||||
if (! $node instanceof FuncCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->name instanceof Name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($node->name->toString() !== 'trigger_error') {
|
||||
if (! $this->isFunctionWithName($node, 'trigger_error')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var FuncCall $node */
|
||||
if (count($node->args) !== 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->args[1]->value instanceof ConstFetch) {
|
||||
/** @var Arg $secondArgumentNode */
|
||||
$secondArgumentNode = $node->args[1];
|
||||
if (! $secondArgumentNode->value instanceof ConstFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var ConstFetch $constFetchNode */
|
||||
$constFetchNode = $node->args[1]->value;
|
||||
$constFetchNode = $secondArgumentNode->value;
|
||||
|
||||
return $constFetchNode->name->toString() === 'E_USER_DEPRECATED';
|
||||
}
|
||||
|
||||
private function processConcatNode(Node $node): string
|
||||
{
|
||||
if ($node instanceof Method) { // get method name in stirng, e.g. "getValue()"
|
||||
if ($node instanceof Method) {
|
||||
$classMethodNode = $this->findParentOfType($node, ClassMethod::class);
|
||||
|
||||
return $classMethodNode->name->name;
|
||||
|
@ -74,17 +84,34 @@ final class DeprecationDetector extends NodeVisitorAbstract
|
|||
return $node->value;
|
||||
}
|
||||
|
||||
// @todo implement
|
||||
throw new Exception(sprintf(
|
||||
'Not implemented yet %s::%s()',
|
||||
__CLASS__,
|
||||
__METHOD__
|
||||
));
|
||||
}
|
||||
|
||||
private function findParentOfType(Node $node, string $type): ClassMethod
|
||||
{
|
||||
$parentNode = $node->getAttribute('parent');
|
||||
|
||||
while (! is_a($parentNode , $type, true)) {
|
||||
while (! is_a($parentNode, $type, true)) {
|
||||
$parentNode = $parentNode->getAttribute('parent');
|
||||
}
|
||||
|
||||
return $parentNode;
|
||||
}
|
||||
|
||||
private function isFunctionWithName(Node $node, string $name): bool
|
||||
{
|
||||
if (! $node instanceof FuncCall) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $node->name instanceof Name) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $node->name->toString() !== $name;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,9 +40,8 @@ final class TriggerExtractor
|
|||
|
||||
/**
|
||||
* @param string[] $directories
|
||||
* @return array
|
||||
*/
|
||||
public function scanDirectories(array $directories): array
|
||||
public function scanDirectories(array $directories): void
|
||||
{
|
||||
$files = $this->findPhpFilesInDirectories($directories);
|
||||
|
||||
|
@ -50,12 +49,9 @@ final class TriggerExtractor
|
|||
$nodes = $this->parser->parseFile($file->getRealPath());
|
||||
$this->standaloneTraverseNodeTraverser->traverse($nodes);
|
||||
$this->mainNodeTraverser->traverse($nodes);
|
||||
die;
|
||||
}
|
||||
die;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string[] $directories
|
||||
* @return SplFileInfo[] array
|
||||
|
|
|
@ -4,37 +4,37 @@
|
|||
* This file is part of the Nette Framework (https://nette.org)
|
||||
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Nette\DI;
|
||||
|
||||
use Nette;
|
||||
|
||||
final class ServiceDefinition
|
||||
final class Definition
|
||||
{
|
||||
/**
|
||||
* @param string|null
|
||||
* @return static
|
||||
* @deprecated
|
||||
*/
|
||||
public function setClass($type)
|
||||
public function setClass(?string $type)
|
||||
{
|
||||
($this->notifier)();
|
||||
$this->type = $type;
|
||||
if (func_num_args() > 1) {
|
||||
trigger_error(__METHOD__ . '() second parameter $args is deprecated, use setFactory()', E_USER_DEPRECATED);
|
||||
@trigger_error(__METHOD__ . '() second parameter $args is deprecated, use setFactory()', E_USER_DEPRECATED);
|
||||
if ($args = func_get_arg(1)) {
|
||||
$this->setFactory($type, $args);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
public function setInject(bool $state = true)
|
||||
{
|
||||
trigger_error(__METHOD__ . "() is deprecated, use addTag('inject')", E_USER_DEPRECATED);
|
||||
@trigger_error(__METHOD__ . "() is deprecated, use addTag('inject')", E_USER_DEPRECATED);
|
||||
|
||||
return $this->addTag(Extensions\InjectExtension::TAG_INJECT, $state);
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
namespace Rector\TriggerExtractor\Tests;
|
||||
|
||||
use Rector\Tests\AbstractContainerAwareTestCase;
|
||||
use Rector\TriggerExtractor\Deprecation\DeprecationCollector;
|
||||
use Rector\TriggerExtractor\TriggerExtractor;
|
||||
|
||||
final class TriggerExtractorTest extends AbstractContainerAwareTestCase
|
||||
|
@ -12,14 +13,20 @@ final class TriggerExtractorTest extends AbstractContainerAwareTestCase
|
|||
*/
|
||||
private $triggerExtractor;
|
||||
|
||||
/**
|
||||
* @var DeprecationCollector
|
||||
*/
|
||||
private $deprecationCollector;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->triggerExtractor = $this->container->get(TriggerExtractor::class);
|
||||
$this->deprecationCollector = $this->container->get(DeprecationCollector::class);
|
||||
}
|
||||
|
||||
public function test(): void
|
||||
{
|
||||
$foundDeprecations = $this->triggerExtractor->scanDirectories([__DIR__ . '/TriggerExtractorSource']);
|
||||
$this->assertCount(2, $foundDeprecations);
|
||||
$this->triggerExtractor->scanDirectories([__DIR__ . '/TriggerExtractorSource']);
|
||||
$this->assertCount(2, $this->deprecationCollector->getDeprecations());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user