Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
parameters:
doctrine:
repositoryClass: Doctrine\ORM\EntityRepository
repositoryPattern: "/(.*)(Entity)(.*)/"
repositoryReplace: "$1Repository$3Repository"

services:
-
Expand All @@ -19,6 +21,8 @@ services:
class: PHPStan\Type\Doctrine\EntityManagerGetRepositoryDynamicReturnTypeExtension
arguments:
repositoryClass: %doctrine.repositoryClass%
repositoryPattern: %doctrine.repositoryPattern%
repositoryReplace: %doctrine.repositoryReplace%
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension
-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,17 @@ class EntityManagerGetRepositoryDynamicReturnTypeExtension implements \PHPStan\T
/** @var string */
private $repositoryClass;

public function __construct(string $repositoryClass)
/** @var string */
private $repositoryPattern;

/** @var string */
private $repositoryReplace;

public function __construct(string $repositoryClass, string $repositoryPattern, string $repositoryReplace)
{
$this->repositoryClass = $repositoryClass;
$this->repositoryPattern = $repositoryPattern;
$this->repositoryReplace = $repositoryReplace;
}

public function getClass(): string
Expand Down Expand Up @@ -47,7 +55,23 @@ public function getTypeFromMethodCall(
return new MixedType();
}

return new EntityRepositoryType($argType->getValue(), $this->repositoryClass);
$repositoryClass = preg_replace($this->repositoryPattern, $this->repositoryReplace, $argType->getValue());

if (!is_string($repositoryClass)) {
throw new \InvalidArgumentException(
sprintf(
'Given repositoryPattern("%s") or repositoryReplace("%s") is invalid.',
$this->repositoryPattern,
$this->repositoryReplace
)
);
}

if (!class_exists($repositoryClass)) {
$repositoryClass = $this->repositoryClass;
}

return new EntityRepositoryType($argType->getValue(), $repositoryClass);
}

}