Repository navigation
Expand file tree
/
Copy pathAbsolutizeRequireAndIncludePathRector.php
More file actions
162 lines (134 loc) · 4.08 KB
/
Copy pathAbsolutizeRequireAndIncludePathRector.php
File metadata and controls
162 lines (134 loc) · 4.08 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Include_;
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\Concat;
use PhpParser\Node\Expr\Include_;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\String_;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\Util\StringUtils;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\AbsolutizeRequireAndIncludePathRectorTest
*/
final class AbsolutizeRequireAndIncludePathRector extends AbstractRector
{
/**
* @see https://regex101.com/r/N8oLqv/1
*/
private const string WINDOWS_DRIVE_REGEX = '#^[a-zA-z]\:[\/\\\]#';
public function __construct(
private readonly ValueResolver $valueResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Migrate include/require to absolute path. This Rector might introduce backwards incompatible code, when the include/require being changed depends on the current working directory.',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
require 'autoload.php';
require $variable;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
require __DIR__ . '/autoload.php';
require $variable;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Include_::class];
}
/**
* @param Include_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->expr instanceof Concat && $node->expr->left instanceof String_ && $this->isRefactorableStringPath(
$node->expr->left
)) {
$node->expr->left = $this->prefixWithDirConstant($node->expr->left);
return $node;
}
if (! $node->expr instanceof String_) {
return null;
}
if (! $this->isRefactorableStringPath($node->expr)) {
return null;
}
/** @var string $includeValue */
$includeValue = $this->valueResolver->getValue($node->expr);
// skip phar
if (\str_starts_with($includeValue, 'phar://')) {
return null;
}
// skip absolute paths
if (\str_starts_with($includeValue, '/') || \str_starts_with($includeValue, '\\')) {
return null;
}
if (str_contains($includeValue, 'config/')) {
return null;
}
if (StringUtils::isMatch($includeValue, self::WINDOWS_DRIVE_REGEX)) {
return null;
}
// add preslash to string
$node->expr->value = \str_starts_with($includeValue, './') ? Strings::substring(
$includeValue,
1
) : '/' . $includeValue;
$node->expr = $this->prefixWithDirConstant($node->expr);
return $node;
}
private function isRefactorableStringPath(String_ $string): bool
{
return ! \str_starts_with($string->value, 'phar://');
}
private function prefixWithDirConstant(String_ $string): Concat
{
$this->removeExtraDotSlash($string);
$this->prependSlashIfMissing($string);
return new Concat(new Dir(), $string);
}
/**
* Remove "./" which would break the path
*/
private function removeExtraDotSlash(String_ $string): void
{
if (! \str_starts_with($string->value, './')) {
return;
}
$string->value = Strings::replace($string->value, '#^\.\/#', '/');
}
private function prependSlashIfMissing(String_ $string): void
{
if (\str_starts_with($string->value, '/')) {
return;
}
$string->value = '/' . $string->value;
}
}