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"
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.
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
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.
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
Check the documentation testing to learn about testing.
BSD-3-Clause license. Please see License File for more information.
This package is a fork of proget-hq/phpstan-yii2 with some corrections.