forked from palantirnet/drupal-rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFromUriRector.php
70 lines (62 loc) · 2.06 KB
/
FromUriRector.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
<?php
declare(strict_types=1);
namespace DrupalRector\Drupal9\Rector\Deprecation;
use DrupalRector\Utility\GetDeclaringSourceTrait;
use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class FromUriRector extends AbstractRector
{
use GetDeclaringSourceTrait;
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixes deprecated file_create_url() calls from \Drupal\Core\Url::fromUri().', [
new CodeSample(
<<<'CODE_BEFORE'
\Drupal\Core\Url::fromUri(file_create_url($uri));
CODE_BEFORE
,
<<<'CODE_AFTER'
\Drupal::service('file_url_generator')->generate($uri);
CODE_AFTER
),
]);
}
public function getNodeTypes(): array
{
return [
Node\Expr\StaticCall::class,
];
}
public function refactor(Node $node): ?Node
{
assert($node instanceof Node\Expr\StaticCall);
if ($this->getName($node->name) !== 'fromUri') {
return null;
}
$type = $this->nodeTypeResolver->getType($node);
if (!$type instanceof FullyQualifiedObjectType
|| $type->getClassName() !== 'Drupal\Core\Url') {
return null;
}
$args = $node->getArgs();
if (count($args) !== 1) {
return null;
}
$fileUrlArg = $args[0]->value;
if (!$fileUrlArg instanceof Node\Expr\FuncCall
|| $this->getName($fileUrlArg->name) !== 'file_create_url'
) {
return null;
}
$args = $fileUrlArg->getArgs();
$service = new Node\Expr\StaticCall(
new Node\Name\FullyQualified('Drupal'),
'service',
[new Node\Arg(new Node\Scalar\String_('file_url_generator'))]
);
return new Node\Expr\MethodCall($service, new Node\Identifier('generate'), $args);
}
}