psalm-plugin-phpunit icon indicating copy to clipboard operation
psalm-plugin-phpunit copied to clipboard

`MissingConstructor` issues detected when explicitly specifying a test code analysis path

Open Ocramius opened this issue 4 years ago • 4 comments

While working on a project that uses very strict psalm settings and this plugin, we discovered that running vendor/bin/psalm with following configuration and no arguments, no issues are reported:

<?xml version="1.0"?>
<psalm
    totallyTyped="true"
    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"
>
    <projectFiles>
        <directory name="src"/>
        <directory name="tests/src"/>
        <ignoreFiles>
            <directory name="vendor"/>
        </ignoreFiles>
    </projectFiles>

    <issueHandlers>
        <InternalMethod>
            <errorLevel type="suppress">
                <referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturnCallback"/>
                <referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::willReturn"/>
                <referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::method"/>
                <referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::with"/>
                <referencedMethod name="PHPUnit\Framework\MockObject\Builder\InvocationMocker::withConsecutive"/>
            </errorLevel>
        </InternalMethod>
    </issueHandlers>

    <plugins>
        <pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
    </plugins>
</psalm>

If I run vendor/bin/psalm tests (new argument added!), the output changes, and starts reporting stuff that is generally silenced by this plugin (the following, but for every test):

ERROR: MissingConstructor - tests/src/Unit/Presentation/UserViewTest.php:27:35 - SomeProject\Core\Tests\Unit\Presentation\UserViewTest has an uninitialized property $this->user, but no constructor (see https://psalm.dev/073)
    private UserSettingRepository $userSettingRepository;

Interestingly, no other errors around assertions nor anything: it seems like the translation from setUp to __construct isn't really working as expected :thinking:

Tried it on updated dependencies BTW:

psalm/plugin-phpunit 0.12.2 Psalm plugin for PHPUnit
vimeo/psalm          3.17.2 A static analysis tool for finding errors in PHP applications

Ocramius avatar Oct 15 '20 16:10 Ocramius

it seems like the translation from setUp to __construct isn't really working as expected thinking

It seems it's much easier than that - Psalm just doesn't trigger afterCodebasePopulated in this use case. @muglug, should it do that?

weirdan avatar Oct 17 '20 13:10 weirdan

@weirdan is there a code reference? Perhaps somebody can send a patch, after having this knowledge...

Ocramius avatar Oct 26 '20 19:10 Ocramius

Psalm also randomly reports some of the MissingConstructor when running without arguments, which does not occur when running vendor/bin/psalm --no-diff.

MauricioFauth avatar Feb 28 '22 16:02 MauricioFauth

This doesn't seem related to this issue or even this repo. Please post a new issue in vimeo/psalm. If you can manage to reproduce on a small repo it would be even better

orklah avatar Feb 28 '22 18:02 orklah