diff --git a/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php b/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php index 03002f19..a4cfb123 100644 --- a/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php +++ b/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php @@ -28,7 +28,7 @@ public function shouldRun(string $path, string $content): bool { $singleLine = false; if (!$callMatches) { $singleLine = true; - preg_match('#\$this-\>getMailer\(\'([\w\.]+)\'\)->send\(#', $content, $callMatches); + preg_match('#\$this->getMailer\(\s*\'([\w\.]+)\'\s*\)\s*->\s*send\(\s*\'([\w\.]+)\'#msu', $content, $callMatches); } if (!$useMatches && !$callMatches) { return false; @@ -64,7 +64,7 @@ public function annotate(string $path): bool { if (!$useMatches) { preg_match('#\$\w+\s*=\s*\$this->getMailer\(\'([\w.]+)\'\)#', $this->content, $callMatches); if (!$callMatches) { - preg_match('#\$this->getMailer\(\'([\w.]+)\'\)->send\(\'(\w+)\'#', $this->content, $callMatches); + preg_match('#\$this->getMailer\(\s*\'([\w\.]+)\'\s*\)\s*->\s*send\(\s*\'([\w\.]+)\'#msu', $this->content, $callMatches); if (!$callMatches) { return false; } @@ -102,8 +102,21 @@ public function annotate(string $path): bool { $rows = explode(PHP_EOL, $this->content); $rowToAnnotate = null; $rowMatches = null; + $multiLine = str_contains($callMatches[0], PHP_EOL); foreach ($rows as $i => $row) { - if (!preg_match('#\$this->getMailer\(\'' . $callMatches[1] . '\'\)->send\(\'' . $callMatches[2] . '\'#', $row, $rowMatches)) { + if ( + $multiLine + && preg_match('#\$this->getMailer\(\s*\'' . $callMatches[1] . '\'\s*\)#msu', $row, $rowMatches) + && !empty($rows[$i + 1]) + && preg_match('#->\s*send\(\s*\'' . $callMatches[2] . '\'#msu', $rows[$i + 1], $rowMatches) + ) { + $rowToAnnotate = $i; + $action = $callMatches[2]; + + break; + } + + if (!preg_match('#\$this->getMailer\(\s*\'' . $callMatches[1] . '\'\s*\)\s*->\s*send\(\s*\'' . $callMatches[2] . '\'#msu', $row, $rowMatches)) { continue; } $rowToAnnotate = $i + 1; diff --git a/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php b/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php index 45dc639b..05348502 100644 --- a/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php +++ b/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php @@ -107,6 +107,24 @@ public function testAnnotateSingleLine() { $this->assertTextContains(' -> 1 annotation added.', $output); } + /** + * @return void + */ + public function testAnnotateMultiLine() { + $content = file_get_contents(TEST_FILES . 'MailerAnnotation' . DS . 'MailerAnnotation.missing3.php'); + $task = $this->getTask($content); + $path = '/src/Foo/Foo.php'; + + $result = $task->annotate($path); + $this->assertTrue($result); + + $content = $task->getContent(); + $this->assertTextContains('* @uses \TestApp\Mailer\NotificationMailer::notify()', $content); + + $output = $this->out->output(); + $this->assertTextContains(' -> 1 annotation added.', $output); + } + /** * @return void */ diff --git a/tests/test_files/MailerAnnotation/MailerAnnotation.missing3.php b/tests/test_files/MailerAnnotation/MailerAnnotation.missing3.php new file mode 100644 index 00000000..5a89174b --- /dev/null +++ b/tests/test_files/MailerAnnotation/MailerAnnotation.missing3.php @@ -0,0 +1,10 @@ +getMailer('Notification') + ->send('notify', []); + } +}