Skip to content

Commit

Permalink
Magic repository methods - test to use generic bound when the type is…
Browse files Browse the repository at this point in the history
… unknown
  • Loading branch information
ondrejmirtes committed Apr 5, 2023
1 parent b3a7b16 commit c1c32c9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
],
"require": {
"php": "^7.2 || ^8.0",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10.12"
},
"conflict": {
"doctrine/collections": "<1.0",
Expand Down
10 changes: 10 additions & 0 deletions tests/DoctrineIntegration/ORM/data/customRepositoryUsage-6.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@
"message": "Method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\Example::__construct() has parameter $anotherRepository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\MyRepository but does not specify its types: T",
"line": 22,
"ignorable": true
},
{
"message": "Property PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\UseAbstractRepository::$repository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\AbstractRepository does not specify its types: T",
"line": 119,
"ignorable": true
},
{
"message": "Method PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\UseAbstractRepository::__construct() has parameter $repository with generic class PHPStan\\DoctrineIntegration\\ORM\\CustomRepositoryUsage\\AbstractRepository but does not specify its types: T",
"line": 127,
"ignorable": true
}
]
38 changes: 38 additions & 0 deletions tests/DoctrineIntegration/ORM/data/customRepositoryUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,41 @@ public function findOneByBlabla(): int
return 1;
}
}

/**
* @template T of MyEntity
* @extends EntityRepository<T>
*/
class AbstractRepository extends EntityRepository
{

}

class UseAbstractRepository
{

/** @var AbstractRepository */
private $repository;

/** @var AbstractRepository<MyEntity> */
private $genericRepository;

/**
* @param AbstractRepository<MyEntity> $genericRepository
*/
public function __construct(
AbstractRepository $repository,
AbstractRepository $genericRepository
)
{
$this->repository = $repository;
$this->genericRepository = $genericRepository;
}

public function find(): void
{
$entity = $this->repository->findOneById(1);
$entity = $this->genericRepository->findOneById(1);
}

}

0 comments on commit c1c32c9

Please sign in to comment.