yii2-extensions / phpstan
PHPStan extension for Yii2
Requires
- php: >=8.3
- nikic/php-parser: ^4.1|^5.4
- phpstan/phpstan: ^2.3
- yiisoft/yii2: ~2.0.55 || ^22.0@dev
Requires (Dev)
- brianium/paratest: ^7.12
- maglnet/composer-require-checker: ^4.7
- php-forge/coding-standard: ^0.3
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.1
- phpstan/phpstan-strict-rules: ^2.1
- phpunit/phpunit: ^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
PHPStan
Enhanced static analysis for Yii2 applications with PHPStan
Precise type inference, dynamic method resolution, and comprehensive property reflection
Features
Quick start
Installation
composer require --dev yii2-extensions/phpstan:^0.6
Requirements, version selection and bootstrap files are covered in the Installation Guide.
Usage
A blog: Article declares @property tags for id, title and status, and its find() returns an ArticleQuery
with a published(): static scope. SlugBehavior adds a slug property and a permalink() method, Feed is a
component with a string url and an integer limit, and User is the identity class.
Create config/phpstan.php. PHPStan reads it to learn what the application contains; it never configures the running
application:
<?php declare(strict_types=1); return [ // Becomes an array shape: each key keeps the type of its value. 'params' => [ 'adminEmail' => 'admin@example.com', 'pageSize' => 20, ], // Component IDs, read as Yii::$app->feed or Yii::$app->get('feed'). 'components' => [ 'feed' => ['class' => app\components\Feed::class], // identityClass also types Yii::$app->user->identity. 'user' => [ 'class' => yii\web\User::class, 'identityClass' => app\models\User::class, ], ], // Container IDs, read as Yii::$container->get('feed.latest'). 'container' => [ 'singletons' => [ 'feed.latest' => ['class' => app\components\Feed::class], ], ], // Behaviors the application attaches at runtime: their members become visible on the owner. 'behaviors' => [ app\models\Article::class => [app\behaviors\SlugBehavior::class], ], ];
The sections have the shapes of Yii's own configuration, so an application whose configuration loads without side effects can reuse it instead of repeating it.
Create phpstan.neon in the project root, choosing paths that exist in your application, and run
vendor/bin/phpstan analyse:
includes: - vendor/yii2-extensions/phpstan/extension.neon parameters: level: 5 paths: - models yii2: config_path: config/phpstan.php
PHPStan now knows the types that Yii resolves at runtime:
use app\models\Article; // Active Record: the custom query stays bound to its model through scopes. $article = Article::findOne(1); // Article|null $query = Article::find()->published(); // ArticleQuery&ActiveQuery<Article> $articles = $query->all(); // array<Article> // Rows take their shape from the @property tags of the model. $rows = Article::find()->published()->asArray()->all(); // array<array{id: int, title: string, status: string}> if ($article !== null) { $title = $article->getAttribute('title'); // string // Members of the behavior declared for Article. $slug = $article->slug; // string $url = $article->permalink(); // string } // Application: Yii::$app is typed without null, and each component has its configured class. $feed = Yii::$app->feed; // app\components\Feed $request = Yii::$app->request; // yii\web\Request $identity = Yii::$app->user->identity; // app\models\User|null $pageSize = Yii::$app->params['pageSize']; // int $email = Yii::$app->params['adminEmail']; // string // Container: services are resolved by their own IDs, apart from component IDs. $latest = Yii::$container->get('feed.latest'); // app\components\Feed
Opt-in rules check the configuration arrays that Yii only validates when the code runs. Enable the ones you need:
parameters: yii2: rules: createObjectUnknownOption: true createObjectInvalidOptionType: true newUnknownOption: true
use app\components\Feed; // Valid: both options exist and accept these values. Yii::createObject(['class' => Feed::class, 'url' => '/rss', 'limit' => 20]); // yii2.createObject.unknownOption: Feed has no public property or setter named "limits". Yii::createObject(['class' => Feed::class, 'url' => '/rss', 'limits' => 20]); // yii2.createObject.invalidOptionType: "limit" accepts only int, even with type coercion. Yii::createObject(['class' => Feed::class, 'url' => '/rss', 'limit' => 'all']); // yii2.new.unknownOption: the same check for a direct construction. new Feed(['uri' => '/rss']);
Every rule is disabled by default. The Configuration Guide lists them all, including the ones for widgets, behaviors, validators and external actions, and details console applications, generic components and container definition forms. Custom query classes, optional row keys, the service locator, headers and logger messages are in the Usage Examples.
Documentation
Practical guides and complete examples for Yii type inference and application analysis.
- ๐ Installation Guide
- โ๏ธ Configuration Guide
- ๐ก Usage Examples
- ๐ Upgrade Guide and Migration Guide
- ๐งช Testing Guide
The guides and the API reference generated from
the PHPDoc in src/ are published at https://yii2-framework.github.io/docs/extensions/phpstan/.
AI assistance
AI agents: read the documentation index and fetch the Markdown version
of a page by appending index.md to its URL. The
AI agents guide covers skill installation, client setup and
verification.
- ๐ค
yii2-phpstan-analysisskill: extension setup with separate Web and console analyses, diagnosis of component, params, behavior, container and Active Record inference, the opt-in configuration rules, and upgrade migration. - ๐งญ
yii2-frameworkskill: Yii 22.0 application conventions.
The hosted documentation covers Yii 22.0. If it does not match the installed revision or is unavailable, use the repository guides, the skill source, the source PHPDoc and the changelog.