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:
- phar://phpstan.phar/conf/bleedingEdge.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- 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
# Enable strict advanced checks
checkImplicitMixed: true
checkBenevolentUnionTypes: true
checkUninitializedProperties: true
checkMissingCallableSignature: true
checkTooWideReturnTypesInProtectedAndPublicMethods: true
reportAnyTypeWideningInVarTag: true
reportPossiblyNonexistentConstantArrayOffset: true
reportPossiblyNonexistentGeneralArrayOffset: true
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
}
},
}
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,
],
],
];
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.