raul338/cakephp-phpstan-extensions

Services to aid phpstan analysis on CakePHP projects

Installs: 4 445

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0

Type:phpstan-extension

2.1.1 2020-03-25 03:29 UTC

This package is auto-updated.

Last update: 2021-12-05 06:45:45 UTC


README

CI

Services to aid phpstan analysis on CakePHP projects

Version CakePHP Version phpstan version
2.x 3.x 0.12
1.x 3.x 0.11

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/extension.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);

Load Model in controllers

Link to the Book

// phpstan will know $users is App\Model\UsersTable
$users = $this->loadModel('Users');

Query functions getters/setters

Without this extensions phpstan will complain that contain returns \Cake\ORM\Query|array instad of just a Query The same with

  • formatResults
  • join
$query = $this->Users->find('all')->contain(['Books']);

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

You'll have to add to you phpstan.neon

parameters:
    universalObjectCratesClasses:
        - Crud\Event\Subject

Example:

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

License

MIT