Services to aid phpstan analysis on CakePHP projects

Installs: 12 318

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 0

Open Issues: 0


2.1.1 2020-03-25 03:29 UTC

This package is auto-updated.

Last update: 2023-01-05 09:14:28 UTC



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


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

	- 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)
// somewhere else - phpstan will know its the Timestamp touch method

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

    // phpstan will know it is a \Crud\Listener\RelatedModelsListener

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

        - Crud\Event\Subject


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