raul338/cakephp-phpstan-extensions

Services to aid phpstan analysis on CakePHP projects

Installs: 38 635

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:phpstan-extension

3.0.1 2024-09-21 02:51 UTC

This package is auto-updated.

Last update: 2024-11-21 03:09:46 UTC


README

CI

Services to aid phpstan analysis on CakePHP projects

Install

composer require --dev raul338/cakephp-phpstan-extensions

This extensions load automatically if you install phpstan/extension-installer

composer require --dev phpstan/extension-installer

or if you don't use phpstan/extension-installer, include in your phpstan.neon

includes:
	- vendor/raul338/cakephp-phpstan-extensions/src/cakephp-extensions.neon

How does this help me

This extension includes rules to analyze the following snippets wihout using var annotations

Dynamic Finders

Link to the Book

$query = $this->Users->findAllByUsername('joebob');

Detect methods from behaviors

Link to the Book

This only works if the method name is not modified in implementedMethods. Otherwise the analysis may be wrong, or you'll have to decorate your code with dockblocks

/**
 * @mixin \Cake\ORM\Behavior\TimestampBehavior
 */
public class UsersTable extends Table
{
    public function initialize(array $config)
    {
        parent::initialize($config);
        $this->addBehavior('Timestamp');
    }
}
// somewhere else - phpstan will know its the Timestamp touch method
$this->Users->touch($user);

FriendsOfCake/crud Actions & Listeners

This is for use with FriendsOfCake/crud

public function add()
{
    // phpstan will know action() is a AddAction instead of BaseAction
    $this->Crud->action()->saveOptions([]);

    // phpstan will know it is a \Crud\Listener\RelatedModelsListener
    $this->Crud->listener('relatedModels')->relatedModels(true);
}

It will only work if you use one of the default action

  • add
  • delete
  • edit
  • index
  • view

And in spanish names:

  • agregar
  • editar
  • borrar
  • ver

If you use an action with crud you'll have to do something like this:

public function custom()
{
    $this->Crud->mapAction('custom', 'Crud.Index');
    /** @var \Crud\Action\IndexAction */
    $action = $this->Crud->action();
}

FriendsOfCake/crud Event Subject

Tell phpstan that if is an event inside a Controller, the subject will probably be a CrudSubject

Example:

$this->Crud->on('beforePaginate', function (Event $event) {
    $query = $event->getSubject()->query;
    $query->where([ /** ... */]);
});

License

MIT