-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathFileBuildUriRector.php
84 lines (73 loc) · 2.55 KB
/
FileBuildUriRector.php
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
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal9\Rector\Deprecation;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class FileBuildUriRector extends AbstractRector
{
/**
* {@inheritdoc}
*/
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated file_build_uri() calls', [
new CodeSample(
<<<'CODE_BEFORE'
$uri1 = file_build_uri('path/to/file.txt');
$path = 'path/to/other/file.png';
$uri2 = file_build_uri($path);
CODE_BEFORE
,
<<<'CODE_AFTER'
$uri1 = \Drupal::service('stream_wrapper_manager')->normalizeUri(\Drupal::config('system.file')->get('default_scheme') . ('://' . 'path/to/file.txt'));
$path = 'path/to/other/file.png';
$uri2 = \Drupal::service('stream_wrapper_manager')->normalizeUri(\Drupal::config('system.file')->get('default_scheme') . ('://' . $path));
CODE_AFTER
),
]);
}
/**
* {@inheritdoc}
*/
public function getNodeTypes(): array
{
return [
Node\Expr\FuncCall::class,
];
}
/**
* {@inheritdoc}
*/
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\FuncCall);
if ($this->getName($node->name) !== 'file_build_uri') {
return null;
}
assert(count($node->getArgs()) === 1);
$config = new Node\Expr\StaticCall(
new Node\Name\FullyQualified('Drupal'),
'config',
[new Node\Arg(new Node\Scalar\String_('system.file'))]
);
$scheme = new Node\Expr\MethodCall($config, new Node\Identifier('get'), [new Node\Arg(new Node\Scalar\String_('default_scheme'))]);
$arg = new Node\Arg(new Node\Expr\BinaryOp\Concat(
$scheme,
// The nested concatenation is enclosed in parentheses.
// @see https://github.com/rectorphp/rector/issues/7188
new Node\Expr\BinaryOp\Concat(
new Node\Scalar\String_('://'),
$node->getArgs()[0]->value
)
));
$service = new Node\Expr\StaticCall(
new Node\Name\FullyQualified('Drupal'),
'service',
[new Node\Arg(new Node\Scalar\String_('stream_wrapper_manager'))]
);
$methodName = new Node\Identifier('normalizeUri');
return new Node\Expr\MethodCall($service, $methodName, [$arg]);
}
}