yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

PSALM MissingTemplateParam in 2.0.53 and master

Open s1lver opened this issue 4 months ago • 2 comments

<?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

s1lver avatar Jul 30 '25 14:07 s1lver

I think this can be fixed like this

<?php

use yii\base\Behavior;
use yii\db\ActiveRecord;

/**
 * @extends Behavior<ActiveRecord>
 */
class TestBehavior extends Behavior

mspirkov avatar Jul 31 '25 06:07 mspirkov

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.

terabytesoftw avatar Jul 31 '25 07:07 terabytesoftw