Перейти до вмісту
PHP Ecosystem
Explore

Installation guide

Цей контент ще не доступний вашою мовою.

The package declares these constraints in composer.json:

DependencyConstraint
PHP>=8.3
PHPStan^2.3
Yii~2.0.55 || ^22.0@dev
PHP Parser^4.1 || ^5.4

The selected Yii release may require a newer PHP version.

Install the extension as a development dependency:

Terminal window
composer require --dev yii2-extensions/phpstan:^0.6

See migration when updating an existing integration.

Choose one registration method. For manual registration, add this to the application’s phpstan.neon:

includes:
- vendor/yii2-extensions/phpstan/extension.neon
parameters:
level: 5
paths:
- models
yii2:
config_path: config/phpstan.php

Alternatively, PHPStan extension-installer reads this package’s extra.phpstan.includes. If it is installed and its Composer plugin is allowed, omit the manual include.

Use paths that exist, and run PHPStan from the application root. Unlike PHPStan’s file parameters, this extension’s yii2.config_path is resolved against the process working directory. An absolute path is also accepted. It must resolve to a readable regular file whose resolved extension is .php; a symlink to such a file is accepted.

Create config/phpstan.php:

<?php
declare(strict_types=1);
return [
'params' => ['adminEmail' => 'admin@example.com'],
'components' => [
'db' => ['class' => yii\db\Connection::class],
],
];

This file is executed with require and must return an array. It describes types for analysis rather than configuring a running application. Keep it free of database connections and other startup side effects. See configuration for params, components, factories and behaviors. An application whose own configuration loads without side effects can reuse it instead of repeating it.

For no configuration-derived types, omit config_path or set it to ''. The defaults are a Web application and unshaped params. See source-checkout stubs if including the extension outside vendor/.

Composer autoloading discovers the package classes. The analyzed code of an application using global Yii may also need a small bootstrap file, such as config/phpstan-bootstrap.php, to load that class, aliases or additional application autoloading. Keep it safe to load more than once:

<?php
declare(strict_types=1);
require_once dirname(__DIR__) . '/vendor/yiisoft/yii2/Yii.php';

Register it with parameters.bootstrapFiles in NEON:

parameters:
bootstrapFiles:
- config/phpstan-bootstrap.php

Load what the analysis configuration needs

Section titled “Load what the analysis configuration needs”

bootstrapFiles prepare the analyzed code, not the file named by yii2.config_path. The extension requires the analysis configuration while PHPStan builds its services. With PHPStan 2.3.1, bootstrapFiles ran later in cold and warm result cache runs and in a single-process --debug run. Do not rely on a bootstrap file running first: a constant, alias or class provided only by it can be missing when the configuration runs, and a branch on it silently takes the other path.

The extension itself defines YII_DEBUG=true, YII_ENV_DEV=false, YII_ENV_PROD=false and YII_ENV_TEST=true before requiring the analysis configuration, unless already defined. It does not define YII_ENV. Load any other prerequisite from the analysis configuration itself, for example by requiring the same bootstrap file:

<?php
declare(strict_types=1);
require_once __DIR__ . '/phpstan-bootstrap.php';
return [
'params' => ['adminEmail' => 'admin@example.com'],
];

PHPStan 2.3.1 also loads bootstrapFiles with require_once, so a file shared this way runs once per process. Keep its contents idempotent anyway: use require_once and guard constants with defined(). Loading Yii.php with plain require after Yii is already loaded redeclares its classes and stops the analysis. On Yii 22, Composer’s classmap autoloads Yii as soon as the configuration uses it, so a later plain require in a bootstrap file fails.

Terminal window
composer validate --strict
composer audit
vendor/bin/phpstan analyse -c phpstan.neon
vendor/bin/phpstan diagnose -c phpstan.neon

See the usage examples for inferred types and their limitations.

Back to documentation