yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Inconsistent PHPDoc and type definitions across Yii2 (for example `BaseYii::$app`).

Open terabytesoftw opened this issue 1 month ago • 3 comments

Several Yii2 classes contain inconsistent or incomplete PHPDoc annotations that affect static analysis (PHPStan/Psalm) and IDE inference.

A representative example is found in BaseYii.php:

/**
 * @var \yii\console\Application|\yii\web\Application the application instance
 *
 * @phpstan-var \yii\console\Application|\yii\web\Application<TUserIdentity>
 * @psalm-var \yii\console\Application|\yii\web\Application<TUserIdentity>
 */
public static $app;

However, in many parts of the framework, $app can be null (for example, during test teardown or before bootstrap), leading to PHPStan errors like:

Property Yii::$app (yii\web\Application|yii\console\Application) does not accept null.

This specific case also appears in:

tests/TestCase.php tests/framework/console/controllers/DbMessageControllerTest.php tests/framework/i18n/DbMessageSourceTest.php

The same pattern occurs elsewhere: properties and return types that are dynamically nullable or polymorphic, but their PHPDoc types don’t reflect that.

A review of the phpstan-baseline.neon file shows multiple suppressions related to:

null assignments to properties without null mixed or union type mismatches

This indicates a systemic documentation/type inconsistency between runtime behavior and declared PHPDoc types.

terabytesoftw avatar Oct 31 '25 20:10 terabytesoftw

Yes, many of these issues have been fixed, but many still remain. I plan to fix some of them when updating PHPStan level to 4.

mspirkov avatar Nov 01 '25 04:11 mspirkov

It would also be great to include tests in the analysis, but I don't have enough time to fix all the errors yet, as there are many of them.

mspirkov avatar Nov 01 '25 05:11 mspirkov

It would also be great to include tests in the analysis, but I don't have enough time to fix all the errors yet, as there are many of them.

Once we fix the PHPDoc inconsistencies, then I'll fix the static analysis in the tests.

terabytesoftw avatar Nov 02 '25 10:11 terabytesoftw