Skip to content

Commit

Permalink
Improve MailerClassAnnotatorTask
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 21, 2024
1 parent e1fb743 commit 123208e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/test_files/MailerAnnotation/MailerAnnotation.missing3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace TestApp\Foo;

class MailerAnnotation {

public function test() {
$this->getMailer('Notification')
->send('notify', []);
}
}

0 comments on commit 123208e

Please sign in to comment.