Skip to content

Commit

Permalink
Merge pull request #341 from dereuromark/better-event-method-annotation
Browse files Browse the repository at this point in the history
Add better IDE autocomplete for event method stubs
  • Loading branch information
dereuromark committed Jan 11, 2024
2 parents 445f4a7 + 15744b0 commit 83a2cd1
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 28 deletions.
1 change: 1 addition & 0 deletions config/app.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@
],
// If a custom directory should be used, defaults to TMP otherwise
'codeCompletionPath' => null,
'codeCompletionReturnType' => null, // Auto-detect based on controller/component, set to true/false to force one mode.
],
];
76 changes: 54 additions & 22 deletions src/CodeCompletion/Task/ControllerEventsTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace IdeHelper\CodeCompletion\Task;

use Cake\Core\Configure;

class ControllerEventsTask implements TaskInterface {

/**
Expand All @@ -20,26 +22,11 @@ public function type(): string {
* @return string
*/
public function create(): string {
$events = <<<'TXT'
public function startup(EventInterface $event) {
return null;
}
public function beforeFilter(EventInterface $event) {
return null;
}
public function beforeRender(EventInterface $event) {
return null;
}
public function afterFilter(EventInterface $event) {
return null;
}
public function shutdown(EventInterface $event) {
return null;
}
public function beforeRedirect(EventInterface $event, $url, Response $response) {
return null;
}
TXT;
/** @var bool|null $returnType */
$returnType = Configure::read('IdeHelper.codeCompletionReturnType');

$controllerEvents = $this->events($returnType ?? false);
$componentEvents = $this->events($returnType ?? true);

return <<<CODE
Expand All @@ -48,15 +35,60 @@ public function beforeRedirect(EventInterface $event, $url, Response $response)
if (false) {
class Controller {
$events
$controllerEvents
}
class Component {
$events
$componentEvents
}
}
CODE;
}

/**
* @param bool $returnType
*
* @return string
*/
protected function events(bool $returnType): string {
$type = null;
$docBlock = null;
if ($returnType) {
$type = ': ' . '\Cake\Http\Response|null';
} else {
$docBlock = <<<TXT
/**
* @param \Cake\Event\EventInterface \$event
*
* @return \Cake\Http\Response|null|void
*/
TXT;
$docBlock = trim($docBlock) . PHP_EOL . str_repeat("\t", 2);
}

$events = <<<TXT
{$docBlock}public function startup(EventInterface \$event)$type {
return null;
}
{$docBlock}public function beforeFilter(EventInterface \$event)$type {
return null;
}
{$docBlock}public function beforeRender(EventInterface \$event)$type {
return null;
}
{$docBlock}public function afterFilter(EventInterface \$event)$type {
return null;
}
{$docBlock}public function shutdown(EventInterface \$event)$type {
return null;
}
{$docBlock}public function beforeRedirect(EventInterface \$event, \$url, Response \$response)$type {
return null;
}
TXT;

return $events;
}

}
42 changes: 36 additions & 6 deletions tests/TestCase/CodeCompletion/Task/ControllerEventsTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,73 @@ public function testCollect() {
if (false) {
class Controller {
/**
* @param \Cake\Event\EventInterface $event
*
* @return \Cake\Http\Response|null|void
*/
public function startup(EventInterface $event) {
return null;
}
/**
* @param \Cake\Event\EventInterface $event
*
* @return \Cake\Http\Response|null|void
*/
public function beforeFilter(EventInterface $event) {
return null;
}
/**
* @param \Cake\Event\EventInterface $event
*
* @return \Cake\Http\Response|null|void
*/
public function beforeRender(EventInterface $event) {
return null;
}
/**
* @param \Cake\Event\EventInterface $event
*
* @return \Cake\Http\Response|null|void
*/
public function afterFilter(EventInterface $event) {
return null;
}
/**
* @param \Cake\Event\EventInterface $event
*
* @return \Cake\Http\Response|null|void
*/
public function shutdown(EventInterface $event) {
return null;
}
/**
* @param \Cake\Event\EventInterface $event
*
* @return \Cake\Http\Response|null|void
*/
public function beforeRedirect(EventInterface $event, $url, Response $response) {
return null;
}
}
class Component {
public function startup(EventInterface $event) {
public function startup(EventInterface $event): \Cake\Http\Response|null {
return null;
}
public function beforeFilter(EventInterface $event) {
public function beforeFilter(EventInterface $event): \Cake\Http\Response|null {
return null;
}
public function beforeRender(EventInterface $event) {
public function beforeRender(EventInterface $event): \Cake\Http\Response|null {
return null;
}
public function afterFilter(EventInterface $event) {
public function afterFilter(EventInterface $event): \Cake\Http\Response|null {
return null;
}
public function shutdown(EventInterface $event) {
public function shutdown(EventInterface $event): \Cake\Http\Response|null {
return null;
}
public function beforeRedirect(EventInterface $event, $url, Response $response) {
public function beforeRedirect(EventInterface $event, $url, Response $response): \Cake\Http\Response|null {
return null;
}
}
Expand Down

0 comments on commit 83a2cd1

Please sign in to comment.