Method 1: Using composer (recommended)
Install the extension as a development dependency.
composer require --dev yii2-extensions/phpstan:^0.4Add to your composer.json.
{
"require-dev": {
"yii2-extensions/phpstan": "^0.4"
}
}Then run.
composer updateThe easiest way is to use the official PHPStan extension installer.
composer require --dev phpstan/extension-installerAdd the plugin configuration to your composer.json.
{
"require-dev": {
"phpstan/extension-installer": "^1.4",
"yii2-extensions/phpstan": "^0.2"
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true,
"yiisoft/yii2-composer": true
}
}
}With this setup, the extension will be automatically registered, and you only need to configure the Yii specific settings.
If you prefer manual control, include the extension in your phpstan.neon.
includes:
- vendor/yii2-extensions/phpstan/extension.neonCreate a phpstan.neon file in your project root.
includes:
- vendor/yii2-extensions/phpstan/extension.neon
parameters:
level: 5
paths:
- src
- controllers
- models
tmpDir: %currentWorkingDirectory%/runtime
yii2:
config_path: config/phpstan-config.phpCreate a dedicated configuration file for PHPStan analysis. This should be separate from your main application configuration.
Create config/phpstan-config.php.
<?php
declare(strict_types=1);
return [
'phpstan' => [
'application_type' => \yii\web\Application::class,
],
'components' => [
'db' => [
'class' => \yii\db\Connection::class,
],
'user' => [
'class' => \yii\web\User::class,
'identityClass' => \app\models\User::class,
],
'mailer' => [
'class' => \yii\mail\MailerInterface::class,
],
// Add your custom components here
'customService' => [
'class' => \app\services\CustomService::class,
],
],
'container' => [
'definitions' => [
'logger' => \Psr\Log\LoggerInterface::class,
'cache' => \yii\caching\CacheInterface::class,
],
'singletons' => [
'eventDispatcher' => \app\services\EventDispatcher::class,
],
],
];For console applications, create config/phpstan-console-config.php.
<?php
declare(strict_types=1);
return [
'phpstan' => [
'application_type' => \yii\console\Application::class,
],
'components' => [
'db' => [
'class' => \yii\db\Connection::class,
'dsn' => 'sqlite::memory:',
],
// Console-specific components
],
];And update your phpstan.neon.
parameters:
yii2:
config_path: config/phpstan-console-config.phpTest your installation by running PHPStan.
vendor/bin/phpstan analyseYou should see output similar to.
PHPStan - PHP Static Analysis Tool
[OK] No errorsCreate a simple test file to verify type inference is working.
<?php
declare(strict_types=1);
// test-phpstan.php
use yii\web\Application;
// This should be typed as an Application
$app = \Yii::$app;
// This should show proper component types
$db = \Yii::$app->db; // Connection
$user = \Yii::$app->user; // UserRun PHPStan on this file.
vendor/bin/phpstan analyse test-phpstan.php --level=5If your application requires custom bootstrap logic, create a bootstrap file (for example, tests/bootstrap.php).
<?php
declare(strict_types=1);
error_reporting(-1);
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');
require(dirname(__DIR__) . '/vendor/yiisoft/yii2/Yii.php');Reference it in your phpstan.neon.
parameters:
bootstrapFiles:
- tests/bootstrap.phpEnable verbose output to see what is happening.
vendor/bin/phpstan --debug -vvv --error-format=table --memory-limit=1GCheck which extensions are loaded.
vendor/bin/phpstan --versionOnce the installation is complete.