Skip to content

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

Closed

Description

@kenjis

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.

Activity

  1. paulbalandan commented on Feb 15, 2024

    @paulbalandan
    Collaborator

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

  2. kenjis commented on Feb 15, 2024

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

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

    $monster = $this->fabricator->make();
  3. paulbalandan commented on Feb 16, 2024

    @paulbalandan
    Collaborator

    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
        }
  4. paulbalandan commented on Jun 15, 2026

    @paulbalandan
    Collaborator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions