forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParseFileRector.php
More file actions
133 lines (110 loc) · 3.59 KB
/
ParseFileRector.php
File metadata and controls
133 lines (110 loc) · 3.59 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace Rector\Symfony\Symfony28\Rector\StaticCall;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Printer\BetterStandardPrinter;
use Rector\Rector\AbstractRector;
use Rector\Util\StringUtils;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\Symfony28\Rector\StaticCall\ParseFileRector\ParseFileRectorTest
*/
final class ParseFileRector extends AbstractRector
{
/**
* @var string
* @changelog https://regex101.com/r/ZaY42i/1
*/
private const YAML_SUFFIX_IN_QUOTE_REGEX = '#\.(yml|yaml)(\'|\")$#';
/**
* @var string
* @changelog https://regex101.com/r/YHA05g/1
*/
private const FILE_SUFFIX_REGEX = '#File$#';
/**
* @var string
* @changelog https://regex101.com/r/JmNhZj/1
*/
private const YAML_SUFFIX_REGEX = '#\.(yml|yaml)$#';
public function __construct(
private readonly BetterStandardPrinter $betterStandardPrinter
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replaces deprecated Yaml::parse() of file argument with file contents', [
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Yaml\Yaml;
$parsedFile = Yaml::parse('someFile.yml');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Yaml\Yaml;
$parsedFile = Yaml::parse(file_get_contents('someFile.yml'));
CODE_SAMPLE
), ]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}
/**
* Process Node of matched type
*
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'parse')) {
return null;
}
if (! $this->isObjectType($node->class, new ObjectType('Symfony\Component\Yaml\Yaml'))) {
return null;
}
if (! $this->isArgumentYamlFile($node)) {
return null;
}
$funcCall = $this->nodeFactory->createFuncCall('file_get_contents', [$node->args[0]]);
$node->args[0] = new Arg($funcCall);
return $node;
}
private function isArgumentYamlFile(StaticCall $staticCall): bool
{
$firstArg = $staticCall->args[0];
if (! $firstArg instanceof Arg) {
return false;
}
$possibleFileNode = $firstArg->value;
$possibleFileNodeAsString = $this->betterStandardPrinter->print($possibleFileNode);
// is yml/yaml file
if (StringUtils::isMatch($possibleFileNodeAsString, self::YAML_SUFFIX_IN_QUOTE_REGEX)) {
return true;
}
// is probably a file variable
if (StringUtils::isMatch($possibleFileNodeAsString, self::FILE_SUFFIX_REGEX)) {
return true;
}
// try to detect current value
$nodeScope = $possibleFileNode->getAttribute(AttributeKey::SCOPE);
if (! $nodeScope instanceof Scope) {
return false;
}
$nodeType = $nodeScope->getType($possibleFileNode);
if (! $nodeType instanceof ConstantStringType) {
return false;
}
return (bool) Strings::match($nodeType->getValue(), self::YAML_SUFFIX_REGEX);
}
}