psalm: false positive: MissingDependency on Yii2 Active record

Hi, I have this Yii2 project using active record. for a while running psalm on it would work fine and point out errors. But suddenly it started complaining abount missing dependency. Here is the error I get:

ERROR: MissingDependency - utils/SchoolUtils.php:19:16 - app\models\Subject depends on class or interface yii\db\activerecordinterface that does not exist (see https://psalm.dev/157)
        return Subject::getDb()

Subject is a class extending yii\db\ActiveRecord[1] defined as

<?php

namespace app\models; 

class Subject extends \yii\db\ActiveRecord
{
    //.....
}
  

psalm.xml:

<?xml version="1.0"?>
<psalm
    errorLevel="8"
    resolveFromConfigFile="true"
    findUnusedCode="false"
    findUnusedBaselineEntry="false"
    phpVersion="8.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="https://getpsalm.org/schema/config"
    xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
    <projectFiles>
        <directory name="." />
        <file name="web/index.php" />
        <ignoreFiles>
            <directory name="vendor" />
            <directory name="web/assets" />
            <directory name="runtime" />
            <directory name="views" />
            <directory name="migrations" />
            <directory name="mail" />
            <directory name="modules/*/views" /> 
            <directory name="tests" />
            <file name="requirements.php" />
        </ignoreFiles>
    </projectFiles>
</psalm>

I have tried everything I could and could not even make sense of where this might be coming from. One thing I note in the error is this part class or interface yii\db\activerecordinterface that does not exist which is true. yii\db\activerecordinterface does not exist in Yii2 framework. The actual classes are Subject extends \yii\db\ActiveRecord then ActiveRecord extends BaseActiveRecord then BaseActiveRecord extends Model implements ActiveRecordInterface, so it is yii\db\ActiveRecordInterface (note the casing)

That made me think there is a bug somewhere or may be am doing something wrong.

ENV:

  • PHP 8.3.2
  • Psalm on composer ("vimeo/psalm": "^4.12" but "vimeo/psalm": "^5.0.0" didn’t help either)

Let me know if there is further information that is needed.

[1] https://www.yiiframework.com/doc/api/2.0/yii-db-activerecord

About this issue

  • Original URL
  • State: closed
  • Created 4 months ago
  • Comments: 15

Most upvoted comments

Finally solved it. Thanks to help from @terabytesoftw I had to add psr4 entry for namespace in composer.json. I had assumed my composer.json is all roses and it was not.

"autoload": {
        "psr-4": {
            "app\\": "."
        }
    },

then had to make sure that Yii is included. So the whole new config is

<?xml version="1.0"?>
<psalm
    phpVersion="8.3"
    errorLevel="7"
    resolveFromConfigFile="true"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="https://getpsalm.org/schema/config"
    xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
    findUnusedBaselineEntry="true"
    findUnusedCode="false"
>
    <projectFiles>
        <directory name="." />
        <file name="vendor/yiisoft/yii2/Yii.php" />
        <file name="web/index.php" />
        <ignoreFiles>
            <directory name="vendor" />
            <directory name="web" />
            <directory name="runtime" />
            <directory name="views" />
            <directory name="migrations" />
            <directory name="mail" />
            <directory name="widgets" />
            <directory name="modules/*/views" />
            <directory name="modules/school/modules/*/views" />
            <directory name="tests" />
            <file name="requirements.php" />
        </ignoreFiles>
    </projectFiles>
</psalm>

Thanks for helping, everyone. Cheers!

“vimeo/psalm”: “^4.12”

The latest 4.x version was released over two years ago. Even if it had a bug we definitely not going to fix anything in 4.x.

To properly look into your issue, we would need to see it for ourselves. Please try to create a minimal reproducer in a separate repository. To get you started on this I created https://github.com/weirdan/10780 - fork it and tweak it until you can reproduce your issue in that fork.