PHPStan-CakePHP2 Documentation
pieceofcake2/phpstan-cakephp2 is a PHPStan extension for CakePHP 2 applications and plugins running on pieceofcake2/cakephp. It teaches PHPStan the magic of CakePHP 2: models, components and tasks loaded as properties, behavior methods called on models, and ClassRegistry::init(). It is a fork of sidz/phpstan-cakephp2.
Installation
Install the extension with Composer as a development dependency:
composer require --dev pieceofcake2/phpstan-cakephp2Include the extension in your PHPStan configuration:
# phpstan.neon
includes:
- vendor/pieceofcake2/phpstan-cakephp2/extension.neonIf you use phpstan/extension-installer, the extension is included automatically and this is not necessary.
The pieceofcake2/app skeleton is already set up with the extension.
Behavior and Schema Locations
Behaviors and schema files are discovered with glob patterns, relative to the directory PHPStan is run from (normally your project root). The defaults in extension.neon cover:
- the core behaviors of pieceofcake2/cakephp (
vendor/pieceofcake2/cakephp/src/Cake/Model/Behavior, and thelib/Cakeandsrc/Cakepaths used when analysing older releases or CakePHP itself) - the modern pieceofcake2/app layout:
src/Model/Behavior,plugins/*/Model/Behaviorandconfig/Schema - the legacy CakePHP 2 layout:
app/Model/Behavior,app/Plugin/*/Model/Behaviorandapp/Config/Schema
If you have behavior classes or schema files in other locations (perhaps in a vendor directory), add those locations to your configuration. They are merged with the defaults. For example:
# phpstan.neon
parameters:
ModelBehaviorsExtension:
behaviorPaths:
- vendor/my-vendor/my-plugin/src/Model/Behavior/*.php
SchemaService:
schemaPaths:
- vendor/my-vendor/my-plugin/config/Schema/*.phpFeatures
The following features are added to PHPStan:
- Treat behavior methods as extra methods on all models (
$model->behaviorMethod()). - Treat controller, model and shell properties named after model classes as instances of those classes (
$controller->Model). - Treat controller and component properties named after components listed in
$componentsas instances of those classes ($controller->Component,$component->Component). - Treat shell properties named after task classes as instances of those classes (
$shell->Task). A task listed in the$tasksof a shell or task takes precedence over a model of the same name (e.g. theModeltask of bake). - Leave properties declared with
@propertytags to PHPStan, so that the tags take precedence over the properties added by this extension. - Treat
ClassRegistry::init($className)as returning an instance of$classNamewhere possible, orModelwhen only a table for it is defined in a schema file. The class name can be in plugin syntax ('Plugin.Model'), or given as an array with aclasskey (['class' => 'Plugin.Model', 'alias' => 'Alias']). - Treat
$this->Components->load('Name')as returning an instance ofNameComponent.
For example, PHPStan knows the types of all the properties and calls below:
class PostsController extends AppController
{
public $components = ['Paginator'];
public function index()
{
// Post (a model class), PaginatorComponent (listed in $components)
$this->set('posts', $this->Paginator->paginate($this->Post));
// TreeBehavior::generateTreeList(), called on the model
$this->set('categories', $this->Post->Category->generateTreeList());
// Tag
$Tag = ClassRegistry::init('Tag');
$this->set('tags', $Tag->find('list'));
}
}Stubs
The extension also ships PHPStan stubs with more precise types for Model, Router and ClassRegistry, and declarations of the global translation functions (__(), __n(), ...) of basics.php. They are loaded by extension.neon.
Development
composer install
# Run the tests
vendor/bin/phpunit
# Run static analysis
vendor/bin/phpstan
# Check the coding standard (cakephp/cakephp-codesniffer), and fix what can be fixed automatically
vendor/bin/phpcs
vendor/bin/phpcbfThe same checks are available as Composer scripts: composer phpunit, composer phpstan, composer cs-check, composer cs-fix, and composer test to run the tests, static analysis and coding standard check in one go.
