Skip to content

Commit

Permalink
Improve MailerClassAnnotatorTask
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 20, 2024
1 parent 69bfae7 commit e1fb743
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 14 deletions.
62 changes: 48 additions & 14 deletions src/Annotator/ClassAnnotatorTask/MailerClassAnnotatorTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,27 @@ 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;
}

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;
}
Expand All @@ -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;
}
}

Expand All @@ -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]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace TestApp\Foo;

class MailerAnnotation {

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

0 comments on commit e1fb743

Please sign in to comment.