forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleExceptionToErrorEventConstantRector.php
More file actions
76 lines (65 loc) · 2.35 KB
/
ConsoleExceptionToErrorEventConstantRector.php
File metadata and controls
76 lines (65 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony33\Rector\ClassConstFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* Covers:
* - https://github.com/symfony/symfony/pull/22441/files
* - https://github.com/symfony/symfony/blob/master/UPGRADE-3.3.md#console
*
* @see \Rector\Symfony\Tests\Symfony33\Rector\ClassConstFetch\ConsoleExceptionToErrorEventConstantRector\ConsoleExceptionToErrorEventConstantRectorTest
*/
final class ConsoleExceptionToErrorEventConstantRector extends AbstractRector
{
private readonly ObjectType $consoleEventsObjectType;
public function __construct()
{
$this->consoleEventsObjectType = new ObjectType('Symfony\Component\Console\ConsoleEvents');
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Turns old event name with EXCEPTION to ERROR constant in Console in Symfony',
[
new CodeSample('"console.exception"', 'Symfony\Component\Console\ConsoleEvents::ERROR'),
new CodeSample(
'Symfony\Component\Console\ConsoleEvents::EXCEPTION',
'Symfony\Component\Console\ConsoleEvents::ERROR'
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassConstFetch::class, String_::class];
}
/**
* @param ClassConstFetch|String_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof ClassConstFetch && (
$this->isObjectType($node->class, $this->consoleEventsObjectType) &&
$this->isName($node->name, 'EXCEPTION')
)
) {
return $this->nodeFactory->createClassConstFetch($this->consoleEventsObjectType->getClassName(), 'ERROR');
}
if (! $node instanceof String_) {
return null;
}
if ($node->value !== 'console.exception') {
return null;
}
return $this->nodeFactory->createClassConstFetch($this->consoleEventsObjectType->getClassName(), 'ERROR');
}
}