yii2-extensions / phpstan
PHPStan extension for Yii2
Fund package maintenance!
terabytesoftw
Installs: 19 731
Dependents: 26
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 2
Open Issues: 2
Requires
- php: >=8.1
- nikic/php-parser: ^4.1|^5.4.0
- phpstan/phpstan: ^2.1
- yiisoft/yii2: ^2.0.52 || ^22
Requires (Dev)
- maglnet/composer-require-checker: ^4.7
- phpstan/phpstan-phpunit: ^2.0
- phpstan/phpstan-strict-rules: ^2.0.3
- phpunit/phpunit: ^10.2
- symplify/easy-coding-standard: ^12.1
This package is auto-updated.
Last update: 2025-06-19 00:09:06 UTC
README
Extension for PHPStan
A comprehensive PHPStan extension that provides enhanced static analysis for Yii2 applications with precise type inference, dynamic method resolution, and comprehensive property reflection.
Features
✅ ActiveRecord & ActiveQuery Analysis
- Array/object result type inference based on
asArray()
usage. - Dynamic return type inference for
find()
,findOne()
,findAll()
methods. - Generic type support for
ActiveQuery<Model>
with proper chaining. - Hierarchical type resolution: model properties take precedence over behavior properties.
- Precise type inference for
getAttribute()
method calls based on PHPDoc annotations. - Property type resolution from both model classes and attached behaviors.
- Relation methods (
hasOne()
,hasMany()
) with accurate return types. - Support for behavior property definitions through ServiceMap integration.
✅ Application Component Resolution
- Automatic type inference for
Yii::$app->component
access. - Behavior property and method reflection.
- Support for custom component configurations.
- User component with
identity
,id
,isGuest
property types.
✅ Dependency Injection Container
- Service map integration for custom services.
- Support for closures, singletons, and nested definitions.
- Type-safe
Container::get()
method resolution.
✅ Framework Integration
- Header collection dynamic method types.
- Stub files for different application types (web, console, base).
- Support for Yii2 constants (
YII_DEBUG
,YII_ENV_*
).
Quick start
Installation
composer require --dev yii2-extensions/phpstan
Create a phpstan.neon
file in your project root.
includes: - vendor/yii2-extensions/phpstan/extension.neon parameters: level: 5 paths: - src - controllers - models tmpDir: %currentWorkingDirectory%/tests/runtime yii2: config_path: config/phpstan-config.php
Create a PHPStan-specific config file (config/phpstan-config.php
).
<?php declare(strict_types=1); return [ 'components' => [ 'db' => [ 'class' => yii\db\Connection::class, 'dsn' => 'sqlite::memory:', ], 'user' => [ 'class' => yii\web\User::class, 'identityClass' => app\models\User::class, ], // Add your custom components here ], ];
Run PHPStan
.
vendor/bin/phpstan analyse
Type inference examples
Active Record
// ✅ Typed as User|null $user = User::findOne(1); // ✅ Typed as User[] $users = User::findAll(['status' => 'active']); // ✅ Generic ActiveQuery<User> with method chaining $query = User::find()->where(['active' => 1])->orderBy('name'); // ✅ Array results typed as array{id: int, name: string}[] $userData = User::find()->asArray()->all(); // ✅ Typed based on model property annotations string $userName = $user->getAttribute('name'); // ✅ Behavior property resolution string $slug = $user->getAttribute('slug');
Application components
// ✅ Typed based on your configuration $mailer = Yii::$app->mailer; // MailerInterface $db = Yii::$app->db; // Connection $user = Yii::$app->user; // User // ✅ User identity with proper type inference if (Yii::$app->user->isGuest === false) { $userId = Yii::$app->user->id; // int|string|null $identity = Yii::$app->user->identity; // YourUserClass }
Dependency injection
$container = new Container(); // ✅ Type-safe service resolution $service = $container->get(MyService::class); // MyService $logger = $container->get('logger'); // LoggerInterface (if configured)
Documentation
For detailed configuration options and advanced usage.
Quality code
Our social networks
License
BSD-3-Clause license. Please see License File for more information.
Fork
This package is a fork of proget-hq/phpstan-yii2 with some corrections.