Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Model::find() returns incorrect type #25

Open
kenjis opened this issue Feb 15, 2024 · 3 comments · May be fixed by #26
Open

bug: Model::find() returns incorrect type #25

kenjis opened this issue Feb 15, 2024 · 3 comments · May be fixed by #26
Labels
bug Something isn't working

Comments

@kenjis
Copy link

kenjis commented Feb 15, 2024

PHP Version

8.2

PHPStan CodeIgniter Version

v1.4.3.70400

PHPStan Version

1.10.58

What happened?

Model::find() returns incorrect type.
See codeigniter4projects/playground#218 (comment)

Minimum Reproduction Script

    public function testMakesMonsterWithDungeon()
    {
        $monster = $this->fabricator->make();
        $dungeon = model(DungeonModel::class)->find($monster->dungeon_id);

        $this->assertInstanceOf(Dungeon::class, $dungeon); // Line 49
    }
 ------ ---------------------------------------------------------------------------------------------------------- 
  Line   tests/database/FakerTest.php                                                                              
 ------ ---------------------------------------------------------------------------------------------------------- 
  49     Call to method PHPUnit\Framework\Assert::assertInstanceOf() with 'App\\Entities\\Dungeon' and array<int,  
         App\Entities\Dungeon> will always evaluate to false.                                                      
         💡 Because the type is coming from a PHPDoc, you can turn off this check by setting                       
            treatPhpDocTypesAsCertain: false in your phpstan.neon.dist.                                            
 ------ ---------------------------------------------------------------------------------------------------------- 

Expected Output

The $dungeon type is App\\Entities\\Dungeon.

@kenjis kenjis added the bug Something isn't working label Feb 15, 2024
@paulbalandan
Copy link
Collaborator

What is the dumped value of \PHPStan\dumpType($monster->dungeon_id);

@kenjis
Copy link
Author

kenjis commented Feb 15, 2024

 ------ ------------------------------ 
  Line   tests/database/FakerTest.php  
 ------ ------------------------------ 
  47     Dumped type: *ERROR*          
 ------ ------------------------------ 

This is an issue on Fabricator::make(). It returns array|object.

$monster = $this->fabricator->make();

@paulbalandan
Copy link
Collaborator

paulbalandan commented Feb 16, 2024

Yes, because PHPStan does not know what really $monster is, the fetch of ->dungeon_id results in error.

A workaround is to add exhaustive tests.

    public function testMakesMonsterWithDungeon()
    {
        $monster = $this->fabricator->make();
        $this->assertIsObject($monster);
        $this->assertTrue(property_exists('dungeon_id', $monster));
        $this->assertIsInt($monster->dungeon_id); // or assertIsString, whatever the case for this

        $dungeon = model(DungeonModel::class)->find($monster->dungeon_id);
        $this->assertInstanceOf(Dungeon::class, $dungeon); // this should be fine, but if using strict-rules might still error
    }

@paulbalandan paulbalandan linked a pull request Feb 16, 2024 that will close this issue
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants