forked from rectorphp/rector-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftSetBodyToHtmlPlainMethodCallRector.php
95 lines (80 loc) · 2.5 KB
/
SwiftSetBodyToHtmlPlainMethodCallRector.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
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types=1);
namespace Rector\Symfony\SwiftMailer\Rector\MethodCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Symfony\Tests\SwiftMailer\Rector\MethodCall\SwiftSetBodyToHtmlPlainMethodCallRector\SwiftSetBodyToHtmlPlainMethodCallRectorTest
*
* @changelog https://github.com/laravel/framework/pull/38481/files#diff-2310168aa86b70a22595ba784039cbdde829bd38245c9586eedd111dfd0f806d
*/
final class SwiftSetBodyToHtmlPlainMethodCallRector extends AbstractRector
{
public function __construct(
private readonly ValueResolver $valueResolver
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes setBody() method call on Swift_Message into a html() or plain() based on second argument',
[
new CodeSample(
<<<'CODE_SAMPLE'
$message = new Swift_Message();
$message->setBody('...', 'text/html');
$message->setBody('...', 'text/plain');
$message->setBody('...');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$message = new Swift_Message();
$message->html('...');
$message->text('...');
$message->text('...');
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
}
/**
* @param MethodCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->name, 'setBody')) {
return null;
}
if (! $this->isObjectType($node->var, new ObjectType('Swift_Message'))) {
return null;
}
if (count($node->args) === 2) {
$firstArg = $node->args[1];
if (! $firstArg instanceof Arg) {
return null;
}
$secondArgValue = $this->valueResolver->getValue($firstArg->value);
if ($secondArgValue === 'text/html') {
unset($node->args[1]);
$node->name = new Identifier('html');
return $node;
}
}
$node->name = new Identifier('plain');
return $node;
}
}