Skip to content

Commit 123208e

Browse files
committed
Improve MailerClassAnnotatorTask
1 parent e1fb743 commit 123208e

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function shouldRun(string $path, string $content): bool {
2828
$singleLine = false;
2929
if (!$callMatches) {
3030
$singleLine = true;
31-
preg_match('#\$this-\>getMailer\(\'([\w\.]+)\'\)->send\(#', $content, $callMatches);
31+
preg_match('#\$this->getMailer\(\s*\'([\w\.]+)\'\s*\)\s*->\s*send\(\s*\'([\w\.]+)\'#msu', $content, $callMatches);
3232
}
3333
if (!$useMatches && !$callMatches) {
3434
return false;
@@ -64,7 +64,7 @@ public function annotate(string $path): bool {
6464
if (!$useMatches) {
6565
preg_match('#\$\w+\s*=\s*\$this->getMailer\(\'([\w.]+)\'\)#', $this->content, $callMatches);
6666
if (!$callMatches) {
67-
preg_match('#\$this->getMailer\(\'([\w.]+)\'\)->send\(\'(\w+)\'#', $this->content, $callMatches);
67+
preg_match('#\$this->getMailer\(\s*\'([\w\.]+)\'\s*\)\s*->\s*send\(\s*\'([\w\.]+)\'#msu', $this->content, $callMatches);
6868
if (!$callMatches) {
6969
return false;
7070
}
@@ -102,8 +102,21 @@ public function annotate(string $path): bool {
102102
$rows = explode(PHP_EOL, $this->content);
103103
$rowToAnnotate = null;
104104
$rowMatches = null;
105+
$multiLine = str_contains($callMatches[0], PHP_EOL);
105106
foreach ($rows as $i => $row) {
106-
if (!preg_match('#\$this->getMailer\(\'' . $callMatches[1] . '\'\)->send\(\'' . $callMatches[2] . '\'#', $row, $rowMatches)) {
107+
if (
108+
$multiLine
109+
&& preg_match('#\$this->getMailer\(\s*\'' . $callMatches[1] . '\'\s*\)#msu', $row, $rowMatches)
110+
&& !empty($rows[$i + 1])
111+
&& preg_match('#->\s*send\(\s*\'' . $callMatches[2] . '\'#msu', $rows[$i + 1], $rowMatches)
112+
) {
113+
$rowToAnnotate = $i;
114+
$action = $callMatches[2];
115+
116+
break;
117+
}
118+
119+
if (!preg_match('#\$this->getMailer\(\s*\'' . $callMatches[1] . '\'\s*\)\s*->\s*send\(\s*\'' . $callMatches[2] . '\'#msu', $row, $rowMatches)) {
107120
continue;
108121
}
109122
$rowToAnnotate = $i + 1;

tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,24 @@ public function testAnnotateSingleLine() {
107107
$this->assertTextContains(' -> 1 annotation added.', $output);
108108
}
109109

110+
/**
111+
* @return void
112+
*/
113+
public function testAnnotateMultiLine() {
114+
$content = file_get_contents(TEST_FILES . 'MailerAnnotation' . DS . 'MailerAnnotation.missing3.php');
115+
$task = $this->getTask($content);
116+
$path = '/src/Foo/Foo.php';
117+
118+
$result = $task->annotate($path);
119+
$this->assertTrue($result);
120+
121+
$content = $task->getContent();
122+
$this->assertTextContains('* @uses \TestApp\Mailer\NotificationMailer::notify()', $content);
123+
124+
$output = $this->out->output();
125+
$this->assertTextContains(' -> 1 annotation added.', $output);
126+
}
127+
110128
/**
111129
* @return void
112130
*/
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace TestApp\Foo;
3+
4+
class MailerAnnotation {
5+
6+
public function test() {
7+
$this->getMailer('Notification')
8+
->send('notify', []);
9+
}
10+
}

0 commit comments

Comments
 (0)