Services to aid phpstan analysis on CakePHP projects
Services to aid phpstan analysis on CakePHP projects
Version | CakePHP Version | phpstan version |
---|---|---|
5.x | 5.x | 2.0+ |
4.x | 4.x | 2.0+ |
3.x | 4.x | 1.10+ |
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
includes:
- vendor/raul338/cakephp-phpstan-extensions/src/cakephp-extensions.neon
This extension includes rules to analyze the following snippets wihout using var annotations
$query = $this->Users->findAllByUsername('joebob');
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);
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
And in spanish names:
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();
}
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([ /** ... */]);
});
MIT