-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
SendNotifyEmailToMailApiRector.php
352 lines (300 loc) · 10.8 KB
/
SendNotifyEmailToMailApiRector.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
declare(strict_types=1);
namespace Ssch\TYPO3Rector\TYPO310\v1;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/10.1/Deprecation-88850-ContentObjectRendererSendNotifyEmail.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v10\v1\SendNotifyEmailToMailApiRector\SendNotifyEmailToMailApiRectorTest
*/
final class SendNotifyEmailToMailApiRector extends AbstractRector implements DocumentedRuleInterface
{
/**
* @var string
*/
private const MAIL = 'mail';
/**
* @var string
*/
private const MESSAGE = 'message';
/**
* @var string
*/
private const TRIM = 'trim';
/**
* @var string
*/
private const SENDER_ADDRESS = 'senderAddress';
/**
* @var string
*/
private const MESSAGE_PARTS = 'messageParts';
/**
* @var string
*/
private const SUBJECT = 'subject';
/**
* @var string
*/
private const PARSED_RECIPIENTS = 'parsedRecipients';
/**
* @var string
*/
private const SUCCESS = 'success';
/**
* @var string
*/
private const PARSED_REPLY_TO = 'parsedReplyTo';
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class, If_::class, Return_::class];
}
/**
* @param Expression|If_|Return_ $node
*
* @return Node[]|null
*/
public function refactor(Node $node): ?array
{
$methodCall = null;
if ($node instanceof Expression) {
$methodCall = $node->expr;
} elseif ($node instanceof If_) {
$methodCall = $node->cond;
} elseif ($node instanceof Return_) {
$methodCall = $node->expr;
}
if (! $methodCall instanceof MethodCall) {
return null;
}
if ($this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$methodCall,
new ObjectType('TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer')
)) {
return null;
}
if (! $this->isName($methodCall->name, 'sendNotifyEmail')) {
return null;
}
$nodes = [
$this->initializeSuccessVariable(),
$this->initializeMailClass(),
$this->trimMessage($methodCall),
$this->trimSenderName($methodCall),
$this->trimSenderAddress($methodCall),
$this->ifSenderAddress(),
];
$replyTo = isset($methodCall->args[5]) ? $methodCall->args[5]->value : null;
if ($replyTo instanceof Expr) {
$nodes[] = $this->parsedReplyTo($replyTo);
$nodes[] = $this->methodReplyTo();
}
$ifMessageNotEmpty = $this->messageNotEmpty();
$ifMessageNotEmpty->stmts[] = $this->messageParts();
$ifMessageNotEmpty->stmts[] = $this->subjectFromMessageParts();
$ifMessageNotEmpty->stmts[] = $this->bodyFromMessageParts();
$ifMessageNotEmpty->stmts[] = $this->parsedRecipients($methodCall);
$ifMessageNotEmpty->stmts[] = $this->ifParsedRecipients();
$ifMessageNotEmpty->stmts[] = $this->createSuccessTrue();
$nodes[] = $ifMessageNotEmpty;
$successVariable = new Variable(self::SUCCESS);
if ($node instanceof Return_) {
$node->expr = $successVariable;
$nodes[] = $node;
return $nodes;
}
if ($node instanceof If_) {
$node->cond = $successVariable;
$nodes[] = $node;
return $nodes;
}
$nodes[] = new Expression($successVariable);
return $nodes;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor ContentObjectRenderer::sendNotifyEmail to MailMessage-API', [
new CodeSample(
<<<'CODE_SAMPLE'
$GLOBALS['TSFE']->cObj->sendNotifyEmail("Subject\nMessage", '[email protected]', '[email protected]', '[email protected]');
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Mime\Address;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\MailUtility;$success = false;
$mail = GeneralUtility::makeInstance(MailMessage::class);
$message = trim("Subject\nMessage");
$senderName = trim(null);
$senderAddress = trim('[email protected]');
if ($senderAddress !== '') {
$mail->from(new Address($senderAddress, $senderName));
}
if ($message !== '') {
$messageParts = explode(LF, $message, 2);
$subject = trim($messageParts[0]);
$plainMessage = trim($messageParts[1]);
$parsedRecipients = MailUtility::parseAddresses('[email protected]');
if (!empty($parsedRecipients)) {
$mail->to(...$parsedRecipients)->subject($subject)->text($plainMessage);
$mail->send();
}
$success = true;
}
CODE_SAMPLE
),
]);
}
private function initializeSuccessVariable(): Node
{
return new Expression(new Assign(new Variable(self::SUCCESS), $this->nodeFactory->createFalse()));
}
private function initializeMailClass(): Node
{
return new Expression(new Assign(new Variable(self::MAIL), $this->nodeFactory->createStaticCall(
'TYPO3\CMS\Core\Utility\GeneralUtility',
'makeInstance',
[$this->nodeFactory->createClassConstReference('TYPO3\CMS\Core\Mail\MailMessage')]
)));
}
private function trimMessage(MethodCall $methodCall): Node
{
return new Expression(new Assign(new Variable(self::MESSAGE), $this->nodeFactory->createFuncCall(
self::TRIM,
[$methodCall->args[0]]
)));
}
private function trimSenderName(MethodCall $methodCall): Node
{
return new Expression(new Assign(new Variable('senderName'), $this->nodeFactory->createFuncCall(
self::TRIM,
[$methodCall->args[4] ?? new ConstFetch(new Name('null'))]
)));
}
private function trimSenderAddress(MethodCall $methodCall): Node
{
return new Expression(new Assign(new Variable(self::SENDER_ADDRESS), $this->nodeFactory->createFuncCall(
self::TRIM,
[$methodCall->args[3]]
)));
}
private function mailFromMethodCall(): MethodCall
{
return $this->nodeFactory->createMethodCall(self::MAIL, 'from', [
new New_(new FullyQualified('Symfony\Component\Mime\Address'), [
$this->nodeFactory->createArg(new Variable(self::SENDER_ADDRESS)),
$this->nodeFactory->createArg(new Variable('senderName')),
]),
]);
}
private function ifSenderAddress(): Node
{
$mailFromMethodCall = $this->mailFromMethodCall();
$ifSenderName = new If_(new NotIdentical(new Variable(self::SENDER_ADDRESS), new String_('')));
$ifSenderName->stmts[0] = new Expression($mailFromMethodCall);
return $ifSenderName;
}
private function messageNotEmpty(): If_
{
return new If_(new NotIdentical(new Variable(self::MESSAGE), new String_('')));
}
private function messageParts(): Expression
{
return new Expression(new Assign(new Variable(self::MESSAGE_PARTS), $this->nodeFactory->createFuncCall(
'explode',
[new ConstFetch(new Name('LF')), new Variable(self::MESSAGE), new Int_(2)]
)));
}
private function subjectFromMessageParts(): Expression
{
return new Expression(new Assign(new Variable(self::SUBJECT), $this->nodeFactory->createFuncCall(self::TRIM, [
new ArrayDimFetch(new Variable(self::MESSAGE_PARTS), new Int_(0)),
])));
}
private function bodyFromMessageParts(): Expression
{
return new Expression(new Assign(new Variable('plainMessage'), $this->nodeFactory->createFuncCall(self::TRIM, [
new ArrayDimFetch(new Variable(self::MESSAGE_PARTS), new Int_(1)),
])));
}
private function parsedRecipients(MethodCall $methodCall): Expression
{
return new Expression(
new Assign(
new Variable(self::PARSED_RECIPIENTS),
$this->nodeFactory->createStaticCall(
'TYPO3\CMS\Core\Utility\MailUtility',
'parseAddresses',
[$methodCall->args[1]]
)
)
);
}
private function ifParsedRecipients(): If_
{
$ifParsedRecipients = new If_(new BooleanNot(new Empty_(new Variable(self::PARSED_RECIPIENTS))));
$ifParsedRecipients->stmts[] = new Expression($this->nodeFactory->createMethodCall(
$this->nodeFactory->createMethodCall(
$this->nodeFactory->createMethodCall(
self::MAIL,
'to',
[new Arg(new Variable(self::PARSED_RECIPIENTS), false, true)]
),
self::SUBJECT,
[new Variable(self::SUBJECT)]
),
'text',
[new Variable('plainMessage')]
));
$ifParsedRecipients->stmts[] = new Expression($this->nodeFactory->createMethodCall(self::MAIL, 'send'));
return $ifParsedRecipients;
}
private function createSuccessTrue(): Expression
{
return new Expression(new Assign(new Variable(self::SUCCESS), $this->nodeFactory->createTrue()));
}
private function parsedReplyTo(Expr $replyTo): Node
{
return new Expression(new Assign(new Variable(self::PARSED_REPLY_TO), $this->nodeFactory->createStaticCall(
'TYPO3\CMS\Core\Utility\MailUtility',
'parseAddresses',
[$replyTo]
)));
}
private function methodReplyTo(): Node
{
$if = new If_(new BooleanNot(new Empty_(new Variable(self::PARSED_REPLY_TO))));
$if->stmts[] = new Expression($this->nodeFactory->createMethodCall(
self::MAIL,
'setReplyTo',
[new Variable(self::PARSED_REPLY_TO)]
));
return $if;
}
}