yii2-extensions / phpstan
PHPStan extension for Yii2
Fund package maintenance!
terabytesoftw
Installs: 18 683
Dependents: 24
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 2
Open Issues: 1
Requires
- php: >=8.1
- nikic/php-parser: ^4.1|^5.4.0
- phpstan/phpstan: ^2.1
- yiisoft/yii2: ^2.0.52 || ^22
Requires (Dev)
- maglnet/composer-require-checker: ^4.7
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^10.2
- symplify/easy-coding-standard: ^12.1
This package is auto-updated.
Last update: 2025-06-06 16:05:59 UTC
README
Extension for PHPStan.
Installation
The preferred way to install this extension is through composer.
Either run
composer require --dev --prefer-dist yii2-extensions/phpstan:^0.2
or add
"yii2-extensions/phpstan": "^0.2"
Usage
This extension provides enhanced static analysis for Yii2
applications by adding:
- Container service resolution with proper type inference.
- Dynamic method return types for
ActiveRecord
andActiveQuery
. - Header collection dynamic methods support.
- Property reflection extensions for
Application
,Request
,Response
, andUser
components. - Service map integration for dependency injection analysis.
Basic Configuration
To use this extension, you need to add the following configuration to your phpstan.neon
file:
includes: - vendor/yii2-extensions/phpstan/extension.neon parameters: bootstrapFiles: - tests/bootstrap.php level: 5 paths: - src # Exclude paths from analysis excludePaths: - c3.php - requirements.php - config - tests - vendor yii2: # Path to your `Yii2` configuration file (optional) # If not provided or empty, will work without explicit configuration config_path: %currentWorkingDirectory%/config/test.php
Dynamic Constants Configuration
The extension automatically recognizes common Yii2
dynamic constants:
YII_DEBUG
YII_ENV
YII_ENV_DEV
YII_ENV_PROD
YII_ENV_TEST
If you need to add additional dynamic constants, you can extend the configuration:
includes: - vendor/yii2-extensions/phpstan/extension.neon parameters: # Your existing dynamic constants will be merged with the extension's defaults dynamicConstantNames: - YII_DEBUG # Already included by the extension - YII_ENV # Already included by the extension - YII_ENV_DEV # Already included by the extension - YII_ENV_PROD # Already included by the extension - YII_ENV_TEST # Already included by the extension - MY_CUSTOM_CONSTANT - ANOTHER_CONSTANT yii2: config_path: %currentWorkingDirectory%/config/test.php
Note: When you define dynamicConstantNames
in your configuration, it replaces the extension's default
constants.
To maintain the Yii2
constants recognition, you must include them explicitly along with your custom constants, as
shown above.
Advanced Configuration Example
includes: - vendor/yii2-extensions/phpstan/extension.neon parameters: bootstrapFiles: - tests/bootstrap.php # Complete dynamic constants list (extension defaults + custom) dynamicConstantNames: - YII_DEBUG - YII_ENV - YII_ENV_DEV - YII_ENV_PROD - YII_ENV_TEST - APP_VERSION - MAINTENANCE_MODE level: 8 paths: - src - controllers - models - widgets excludePaths: - src/legacy - tests/_support - vendor yii2: config_path: %currentWorkingDirectory%/config/web.php
PHPstan extension installer
You can use the phpstan-extension-installer
to automatically install this extension.
To do this, you need to add the following configuration to your composer.json
file:
composer require --dev phpstan/extension-installer
or, add the following to your composer.json
:
{ "require-dev": { "phpstan/extension-installer": "^1.4" }, "config": { "allow-plugins": { "phpstan/extension-installer": true } }, }
Config yii2
application for PHPStan
To configure the yii2
application, you can use the yii2
section in your phpstan.neon
file:
parameters: yii2: # Path to your `Yii2` configuration file config_path: %currentWorkingDirectory%/config/test.php
config/test.php
file should return an array with the application configuration, similar to the following:
<?php declare(strict_types=1); use yii2\extensions\localeurls\UrlLanguageManager; return [ 'components' => [ // custom component 'helper' => [ 'class' => \yii2\extensions\helper\Helper::class, ], // your component extended 'urlManager' => [ 'class' => UrlLanguageManager::class, ], ], ];
Quality code
Testing
Check the documentation testing to learn about testing.
Our social networks
License
BSD-3-Clause license. Please see License File for more information.
Fork
This package is a fork of proget-hq/phpstan-yii2 with some corrections.