Skip to content

Fix: honour setUp() and #[Before] from traits for MissingConstructor - #260

Merged
alies-dev merged 17 commits into
psalm:masterfrom
alies-dev:fix/252-trait-initializers
Nov 10, 2026
Merged

alies-dev merged 17 commits into
psalm:masterfrom
alies-dev:fix/252-trait-initializers

Conversation

@alies-dev

Copy link
Copy Markdown
Contributor

Properties initialised in setUp() or a #[Before] method of a trait the test case uses are reported as MissingConstructor.

Before

trait Init {
    private int $x;

    protected function setUp(): void {
        $this->x = 1;
    }
}

final class MyTestCase extends TestCase { // MissingConstructor: NS\MyTestCase has an uninitialized property NS\Init::$x, but no constructor
    use Init;

    public function testSomething(): void {
        $this->assertSame(1, $this->x);
    }
}

After

final class MyTestCase extends TestCase { // no issue

Fixes #252

@alies-dev alies-dev changed the title Fix: honour setUp() and #[Before] from traits for MissingConstructor Fix: honour setUp() and #[Before] from traits for MissingConstructor Nov 10, 2026
Psalm has resolved parents, traits, aliases and insteadof into declaring_method_ids by AfterCodebasePopulated, so a setUp() not declared by TestCase or a #[Before] method of any declaring class is an initializer. This replaces the manual trait walk, the setUp alias special case and the descendant loop.
# Conflicts:
#	tests/acceptance/DeadCode.feature
# Conflicts:
#	tests/acceptance/Initializers.feature
@alies-dev
alies-dev merged commit 438142e into psalm:master Nov 10, 2026
8 checks passed
@alies-dev
alies-dev deleted the fix/252-trait-initializers branch November 10, 2026 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MissingConstructor when setUp() or #[Before] comes from a trait

1 participant