yii2
yii2 copied to clipboard
PSALM MissingTemplateParam in 2.0.53 and master
<?php
declare(strict_types=1);
namespace app\Behaviors;
use yii\base\Behavior;
use yii\base\ModelEvent;
use yii\db\ActiveRecord;
class TestBehavior extends Behavior
{
/**
* @return string[]
*/
public function events(): array
{
return [
ActiveRecord::EVENT_BEFORE_INSERT => 'beforeInsert',
];
}
public function beforeInsert(ModelEvent $event): void
{
}
}
ERROR: MissingTemplateParam - Behaviors/TestBehavior.php:11:7 - app\Behaviors\TestBehavior has missing template params when extending yii\base\Behavior, expecting 1 (see https://psalm.dev/182) class TestBehavior extends Behavior
I think this can be fixed like this
<?php
use yii\base\Behavior;
use yii\db\ActiveRecord;
/**
* @extends Behavior<ActiveRecord>
*/
class TestBehavior extends Behavior
I'm going to add a full note to UPGRADE.md about the static annotations that were added to the framework.
/**
* @phpstan-template T of ActiveRecord
*
* @phpstan-extends Behavior<T>
*/
class NestedSetsBehavior extends Behavior {]
/**
* @phpstan-template T of ActiveQuery
*
* @phpstan-extends Behavior<T>
*/
class NestedSetsQueryBehavior extends Behavior {}
/**
* @phpstan-var T|null
*/
public $owner;
This allows static analysis to infer the type of owner, without having to add annotations throughout the code.