Installation guide
このコンテンツはまだ日本語訳がありません。
Requirements
Section titled “Requirements”The package declares these constraints in composer.json:
| Dependency | Constraint |
|---|---|
| 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.
Choose a version
Section titled “Choose a version”Install the extension as a development dependency:
composer require --dev yii2-extensions/phpstan:^0.6See migration when updating an existing integration.
Register the extension
Section titled “Register the extension”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.phpAlternatively, 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.
Describe the application
Section titled “Describe the application”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/.
Bootstrap only what is needed
Section titled “Bootstrap only what is needed”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.phpLoad 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.
Verify
Section titled “Verify”composer validate --strictcomposer auditvendor/bin/phpstan analyse -c phpstan.neonvendor/bin/phpstan diagnose -c phpstan.neonSee the usage examples for inferred types and their limitations.