1. Go to this page and download the library: Download yii2-extensions/phpstan library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii2-extensions / phpstan example snippets
declare(strict_types=1);
return [
// PHPStan only: used by this extension for behavior property/method type inference
'behaviors' => [
app\models\User::class => [
app\behaviors\SoftDeleteBehavior::class,
yii\behaviors\TimestampBehavior::class,
],
],
'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
],
];
// ✅ 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');
// ✅ 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
}
// Behaviors are attached via the `phpstan-config.php` behaviors map (PHPStan only)
/**
* @property string $slug
* @property-read int $created_at
*
* Note: `created_at` is provided by `TimestampBehavior`.
*/
class SoftDeleteBehavior extends \yii\base\Behavior
{
public function softDelete(): bool { /* ... */ }
}
// ✅ Typed based on your configuration
$user = new User();
// ✅ Typed as string (inferred from behavior property)
$slug = $user->getAttribute('slug');
// ✅ Direct property access is also inferred (behavior property)
$slug2 = $user->slug;
// ✅ Typed as int (inferred from behavior property)
$createdAt = $user->getAttribute('created_at');
// ✅ Typed as bool (method defined in attached behavior)
$result = $user->softDelete();
$container = new Container();
// ✅ Type-safe service resolution
$service = $container->get(MyService::class); // MyService
$logger = $container->get('logger'); // LoggerInterface (if configured) or mixed
$headers = new HeaderCollection();
// ✅ Typed as string|null
$host = $headers->get('Host');
// ✅ Typed as array<int, string>
$forwardedFor = $headers->get('X-Forwarded-For', ['127.0.0.1'], false);
// ✅ Dynamic return type inference with mixed default
$default = 'default-value';
$requestId = $headers->get('X-Request-ID', $default, true); // string|null
$allRequestIds = $headers->get('X-Request-ID', $default, false); // array<int, string>|null
$serviceLocator = new ServiceLocator();
// ✅ Get component with type inference with class
$mailer = $serviceLocator->get(Mailer::class); // MailerInterface
// ✅ Get component with string identifier and without configuration in ServiceMap
$mailer = $serviceLocator->get('mailer'); // MailerInterface (if configured) or mixed
// ✅ User component with proper type inference in Action or Controller
$user = $this->controller->module->get('user'); // UserInterface
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.