Provide services and rules for a better PHPStan analyze on CakePHP applications, includes services to resolve types (Table, Helpers, Behaviors, etc) and multiple rules.
To use this extension, require it through Composer:
composer require --dev cakedc/cakephp-phpstan
If you also install phpstan/extension-installer, then you're all set!
Features included:
Cake\ORM\Locator\LocatorInterface::get()
Cake\Controller\Controller::loadComponent()
Cake\Controller\Controller::fetchTable()
Cake\Controller\Component::fetchTable()
Cake\Command\Command::fetchTable()
Cake\Mailer\Mailer::fetchTable()
Cake\View\Cell::fetchTable()
Cake\Console\ConsoleIo::helper()
Cake\ORM\Table::get
based on your table class nameCake\ORM\Table::newEntity
based on your table class nameCake\ORM\Table::newEntities
based on your table class nameCake\ORM\Table::newEmptyEntity
based on your table class nameCake\ORM\Table::findOrCreate
based on your table class nameCake\ORM\Table::patchEntity
based on the first argument passedCake\ORM\Table::patchEntities
based on the first argument passedCake\ORM\Table::save
based on the first argument passedCake\ORM\Table::saveOrFail
based on the first argument passedCake\ORM\Table::saveMany
based on the first argument passedCake\ORM\Table::saveManyOrFail
based on the first argument passedCake\ORM\Table::deleteMany
based on the first argument passedCake\ORM\Table::deleteManyOrFail
based on the first argument passedCake\ORM\Locator\LocatorAwareTrait::fetchTable
based on the first argument passedCake\Mailer\MailerAwareTrait::getMailer
based on the first argument passedAll rules provided by this library are included in rules.neon and are enabled by default:
This rule check if the target association has a valid table class when calling to Table::belongsTo, Table::hasMany, Table::belongsToMany, Table::hasOne and AssociationCollection::load.
This rule check if association options are valid option types based on what each class expects. This cover calls to Table::belongsTo, Table::hasMany, Table::belongsToMany, Table::hasOne and AssociationCollection::load.
This rule check if the target behavior has a valid class when calling to Table::addBehavior and BehaviorRegistry::load.
This rule disallow array access to entity in favor of object notation, is easier to detect a wrong property and to refactor code.
This rule check if the target mailer is a valid class when calling to Cake\Mailer\MailerAwareTrait::getMailer.
This rule check if the target component has a valid class when calling to Controller::loadComponent and ComponentRegistry::load.
This rule check if the options (args) passed to Table::find and SelectQuery are valid find options types.
This rule check if the options (args) passed to Table::get are valid find options types.
To enable this rule update your phpstan.neon with:
parameters:
cakeDC:
disallowEntityArrayAccessRule: true
Each rule has a parameter in cakeDC 'namespace' to enable or disable, it is the same name of the rule with first letter in lowercase. For example to disable the rule AddAssociationExistsTableClassRule you should have
parameters:
cakeDC:
addAssociationExistsTableClassRule: false
Fix intersection association phpDoc to correct generic object type, ex:
Change \Cake\ORM\Association\BelongsTo&\App\Model\Table\UsersTable
to \Cake\ORM\Association\BelongsTo<\App\Model\Table\UsersTable>
To make your life easier make sure to have @mixin
and @method
annotations in your table classes.
The @mixin
annotation will help phpstan know you are using methods from behavior, and @method
annotations
will allow it to know the correct return types for methods like Table::get()
, Table::newEntity()
.
You can easily update annotations with the plugin IdeHelper.
For bugs and feature requests, please use the issues section of this repository.
Commercial support is also available, contact us for more information.
If you'd like to contribute new features, enhancements or bug fixes to the plugin, please read our Contribution Guidelines for detailed instructions.