Skip to content

[Bug]: Property @var not resolved when a trait in another file assigns a parent's property #99

Closed

Description

@fdaviddpt

Bug report

When a trait in its own file assigns a property declared in a parent class from another namespace, the class name in the property's @var is not resolved. The right object is rejected.

Sorry for the number of issues lately: we are trying to run TypePHP on a large codebase (PHPStan level 9, ~23k files), and each one comes from that work. Thanks for the quick replies so far.

Three files plus a runner, default config (no typephp.php, so src/** is checked):

composer.json

{"require": {"typephp/typephp": "0.10.26"}}

src/Base.php

<?php

namespace Model;

class User {}

class Base
{
    /** @var User|null */
    public ?object $owner = null;
}

src/SetOwner.php

<?php

namespace App;

use Model\User;

trait SetOwner
{
    public function setOwner(): void
    {
        $this->owner = new User();
    }
}

src/Account.php

<?php

namespace App;

class Account extends \Model\Base
{
    use SetOwner;
}

run.php

<?php

require __DIR__ . "/vendor/autoload.php";
require __DIR__ . "/src/Base.php";
require __DIR__ . "/src/SetOwner.php";
require __DIR__ . "/src/Account.php";

(new App\Account())->setOwner();
echo "OK\n";
Fatal error: Uncaught TypePHP\Exception\TypeError: Property App\Account::$owner must be of type (User | null), Model\User given
Variant Result
The 3 files above fails
Trait and Account in the same file OK
Assignment written in Account, no trait OK
Everything in one namespace OK
Everything in one file OK

Tested on v0.10.26 and on main (2ec0426).

Code snippet that reproduces the problem

No playground link: the bug needs several files, and the same code in a single file passes.

Expected output

OK: User in src/Base.php is Model\User.

Context: our entities declare field properties in generated base classes and assign them from traits in feature modules, so property checks reject most of them.

Activity

  1. self-assigned this
    on Oct 3, 2026
  2. rcalicdan commented on Oct 3, 2026

    @rcalicdan
    Member

    No worries at all and no need to apologize.. This bug report is extremly important to resolve many hidden edge cases and make this library better for everyone.. If you encounter more bugs dont hesitate to report more so we can help each other..

  3. linked a pull request that will close this issueNext minor released #100on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingnext minor releasefeature for next minor release

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions