Lewati ke konten
PHP Ecosystem
Explore

PHPStan

Konten ini belum tersedia dalam bahasa Anda.

Enhanced static analysis for Yii2 applications with PHPStan

Precise type inference, dynamic method resolution, and comprehensive property reflection

Feature Overview

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

Requirements, version selection and bootstrap files are covered in the Installation Guide.

A blog: Article declares @property tags for id, title and status, and its find() returns an ArticleQuery with a published(): static scope. SlugBehavior adds a slug property and a permalink() method, Feed is a component with a string url and an integer limit, and User is the identity class.

Create config/phpstan.php. PHPStan reads it to learn what the application contains; it never configures the running application:

<?php
declare(strict_types=1);
return [
// Becomes an array shape: each key keeps the type of its value.
'params' => [
'adminEmail' => 'admin@example.com',
'pageSize' => 20,
],
// Component IDs, read as Yii::$app->feed or Yii::$app->get('feed').
'components' => [
'feed' => ['class' => app\components\Feed::class],
// identityClass also types Yii::$app->user->identity.
'user' => [
'class' => yii\web\User::class,
'identityClass' => app\models\User::class,
],
],
// Container IDs, read as Yii::$container->get('feed.latest').
'container' => [
'singletons' => [
'feed.latest' => ['class' => app\components\Feed::class],
],
],
// Behaviors the application attaches at runtime: their members become visible on the owner.
'behaviors' => [
app\models\Article::class => [app\behaviors\SlugBehavior::class],
],
];

The sections have the shapes of Yii’s own configuration, so an application whose configuration loads without side effects can reuse it instead of repeating it.

Create phpstan.neon in the project root, choosing paths that exist in your application, and run vendor/bin/phpstan analyse:

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

PHPStan now knows the types that Yii resolves at runtime:

use app\models\Article;
// Active Record: the custom query stays bound to its model through scopes.
$article = Article::findOne(1); // Article|null
$query = Article::find()->published(); // ArticleQuery&ActiveQuery<Article>
$articles = $query->all(); // array<Article>
// Rows take their shape from the @property tags of the model.
$rows = Article::find()->published()->asArray()->all(); // array<array{id: int, title: string, status: string}>
if ($article !== null) {
$title = $article->getAttribute('title'); // string
// Members of the behavior declared for Article.
$slug = $article->slug; // string
$url = $article->permalink(); // string
}
// Application: Yii::$app is typed without null, and each component has its configured class.
$feed = Yii::$app->feed; // app\components\Feed
$request = Yii::$app->request; // yii\web\Request
$identity = Yii::$app->user->identity; // app\models\User|null
$pageSize = Yii::$app->params['pageSize']; // int
$email = Yii::$app->params['adminEmail']; // string
// Container: services are resolved by their own IDs, apart from component IDs.
$latest = Yii::$container->get('feed.latest'); // app\components\Feed

Opt-in rules check the configuration arrays that Yii only validates when the code runs. Enable the ones you need:

parameters:
yii2:
rules:
createObjectUnknownOption: true
createObjectInvalidOptionType: true
newUnknownOption: true
use app\components\Feed;
// Valid: both options exist and accept these values.
Yii::createObject(['class' => Feed::class, 'url' => '/rss', 'limit' => 20]);
// yii2.createObject.unknownOption: Feed has no public property or setter named "limits".
Yii::createObject(['class' => Feed::class, 'url' => '/rss', 'limits' => 20]);
// yii2.createObject.invalidOptionType: "limit" accepts only int, even with type coercion.
Yii::createObject(['class' => Feed::class, 'url' => '/rss', 'limit' => 'all']);
// yii2.new.unknownOption: the same check for a direct construction.
new Feed(['uri' => '/rss']);

Every rule is disabled by default. The Configuration Guide lists them all, including the ones for widgets, behaviors, validators and external actions, and details console applications, generic components and container definition forms. Custom query classes, optional row keys, the service locator, headers and logger messages are in the Usage Examples.

Practical guides and complete examples for Yii type inference and application analysis.

The guides and the API reference generated from the PHPDoc in src/ are published at https://yii2-framework.github.io/docs/extensions/phpstan/.

AI agents: read the documentation index and fetch the Markdown version of a page by appending index.md to its URL. The AI agents guide covers skill installation, client setup and verification.

  • 🤖 yii2-phpstan-analysis skill: extension setup with separate Web and console analyses, diagnosis of component, params, behavior, container and Active Record inference, the opt-in configuration rules, and upgrade migration.
  • 🧭 yii2-framework skill: Yii 22.0 application conventions.

The hosted documentation covers Yii 22.0. If it does not match the installed revision or is unavailable, use the repository guides, the skill source, the source PHPDoc and the changelog.

Follow on X Follow on Facebook Join our Subreddit Join on Telegram

License