Skip to content

yii2-extensions/phpstan

Yii Framework

Extension for PHPStan

PHP-Version Yii-22.0.52 Yii2-22 PHPUnit Static-Analysis Codecov

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

phpstan-level style-ci

Our social networks

Twitter

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.

About

PHPStan plugin.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages