From e1fb74375d2bda09055f1d7689df155e6d60425f Mon Sep 17 00:00:00 2001 From: mscherer Date: Fri, 20 Dec 2024 03:19:03 +0100 Subject: [PATCH] Improve MailerClassAnnotatorTask --- .../MailerClassAnnotatorTask.php | 62 ++++++++++++++----- .../MailerClassAnnotatorTaskTest.php | 18 ++++++ .../MailerAnnotation.missing2.php | 9 +++ 3 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 tests/test_files/MailerAnnotation/MailerAnnotation.missing2.php diff --git a/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php b/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php index f4a2e789..03002f19 100644 --- a/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php +++ b/src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php @@ -25,6 +25,11 @@ public function shouldRun(string $path, string $content): bool { preg_match('#\buse (\w+)\\\\Mailer\\\\(\w+)Mailer\b#', $content, $useMatches); preg_match('#\$\w+\s*=\s*\$this-\>getMailer\(\'([\w\.]+)\'\)#', $content, $callMatches); + $singleLine = false; + if (!$callMatches) { + $singleLine = true; + preg_match('#\$this-\>getMailer\(\'([\w\.]+)\'\)->send\(#', $content, $callMatches); + } if (!$useMatches && !$callMatches) { return false; } @@ -32,10 +37,15 @@ public function shouldRun(string $path, string $content): bool { if ($useMatches) { $varName = lcfirst($useMatches[2]) . 'Mailer'; } else { - [$plugin, $name] = pluginSplit($callMatches[1]); + $class = $callMatches[1]; + [$plugin, $name] = pluginSplit($class); $varName = lcfirst($name) . 'Mailer'; } + if ($singleLine && !empty($callMatches)) { + return true; + } + if (!preg_match('#\$' . $varName . '->send\(\'\w+\'#', $content)) { return false; } @@ -49,10 +59,17 @@ public function shouldRun(string $path, string $content): bool { */ public function annotate(string $path): bool { preg_match('#\buse (\w+)\\\\Mailer\\\\(\w+)Mailer\b#', $this->content, $useMatches); + + $singleCall = false; if (!$useMatches) { - preg_match('#\$\w+\s*=\s*\$this->getMailer\(\'([\w\.]+)\'\)#', $this->content, $callMatches); + preg_match('#\$\w+\s*=\s*\$this->getMailer\(\'([\w.]+)\'\)#', $this->content, $callMatches); if (!$callMatches) { - return false; + preg_match('#\$this->getMailer\(\'([\w.]+)\'\)->send\(\'(\w+)\'#', $this->content, $callMatches); + if (!$callMatches) { + return false; + } + + $singleCall = true; } } @@ -65,24 +82,41 @@ public function annotate(string $path): bool { $name = $name . 'Mailer'; } - $varName = lcfirst($name); - $rows = explode(PHP_EOL, $this->content); - $rowToAnnotate = null; - $rowMatches = null; - foreach ($rows as $i => $row) { - if (!preg_match('#\$' . $varName . '->send\(\'(\w+)\'#', $row, $rowMatches)) { - continue; + $action = null; + if (!$singleCall) { + $varName = lcfirst($name); + $rows = explode(PHP_EOL, $this->content); + $rowToAnnotate = null; + $rowMatches = null; + foreach ($rows as $i => $row) { + if (!preg_match('#\$' . $varName . '->send\(\'(\w+)\'#', $row, $rowMatches)) { + continue; + } + $rowToAnnotate = $i + 1; + $action = $rowMatches[1]; + + break; + } + } else { + assert(!empty($callMatches)); + $rows = explode(PHP_EOL, $this->content); + $rowToAnnotate = null; + $rowMatches = null; + foreach ($rows as $i => $row) { + if (!preg_match('#\$this->getMailer\(\'' . $callMatches[1] . '\'\)->send\(\'' . $callMatches[2] . '\'#', $row, $rowMatches)) { + continue; + } + $rowToAnnotate = $i + 1; + $action = $callMatches[2]; + + break; } - $rowToAnnotate = $i + 1; - - break; } if (!$rowToAnnotate) { return false; } - $action = $rowMatches[1]; $method = $appNamespace . '\\Mailer\\' . $name . '::' . $action . '()'; $annotations = $this->buildUsesAnnotations([$method]); diff --git a/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php b/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php index a9131359..45dc639b 100644 --- a/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php +++ b/tests/TestCase/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTaskTest.php @@ -89,6 +89,24 @@ public function testAnnotate() { $this->assertTextContains(' -> 1 annotation added.', $output); } + /** + * @return void + */ + public function testAnnotateSingleLine() { + $content = file_get_contents(TEST_FILES . 'MailerAnnotation' . DS . 'MailerAnnotation.missing2.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.missing2.php b/tests/test_files/MailerAnnotation/MailerAnnotation.missing2.php new file mode 100644 index 00000000..d11055d2 --- /dev/null +++ b/tests/test_files/MailerAnnotation/MailerAnnotation.missing2.php @@ -0,0 +1,9 @@ +getMailer('Notification')->send('notify', []); + } +}