Skip to content

Commit

Permalink
Merge pull request #164 from franmomu/proxy_generic
Browse files Browse the repository at this point in the history
Define `Proxy` as generic
  • Loading branch information
greg0ire authored Apr 5, 2021
2 parents 12a05f8 + f6575c2 commit 210e9f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
11 changes: 11 additions & 0 deletions lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,10 @@ protected function getCacheKey(string $realClassName): string

/**
* Gets the real class name of a class name that could be a proxy.
*
* @template T of object
* @psalm-param class-string<Proxy<T>>|class-string<T> $class
* @psalm-return class-string<T>
*/
private function getRealClass(string $class): string
{
Expand All @@ -485,14 +489,21 @@ private function getRealClass(string $class): string
private function createDefaultProxyClassNameResolver(): void
{
$this->proxyClassNameResolver = new class implements ProxyClassNameResolver {
/**
* @template T of object
* @psalm-param class-string<Proxy<T>>|class-string<T> $className
* @psalm-return class-string<T>
*/
public function resolveClassName(string $className): string
{
$pos = strrpos($className, '\\' . Proxy::MARKER . '\\');

if ($pos === false) {
/** @psalm-var class-string<T> */
return $className;
}

/** @psalm-var class-string<T> */
return substr($className, $pos + Proxy::MARKER_LENGTH + 2);
}
};
Expand Down
7 changes: 5 additions & 2 deletions lib/Doctrine/Persistence/Mapping/ProxyClassNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Doctrine\Persistence\Mapping;

use Doctrine\Persistence\Proxy;

interface ProxyClassNameResolver
{
/**
* @psalm-param class-string $className
* @psalm-return class-string
* @template T of object
* @psalm-param class-string<Proxy<T>>|class-string<T> $className
* @psalm-return class-string<T>
*/
public function resolveClassName(string $className): string;
}
2 changes: 2 additions & 0 deletions lib/Doctrine/Persistence/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

/**
* Interface for proxy classes.
*
* @template T of object
*/
interface Proxy
{
Expand Down
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ parameters:
message: "#^Parameter \\#3 \\$nsSeparator of class Doctrine\\\\Persistence\\\\Mapping\\\\Driver\\\\SymfonyFileLocator constructor expects string, null given\\.$#"
count: 1
path: tests/Doctrine/Tests/Persistence/Mapping/SymfonyFileLocatorTest.php

-
# Remove it when https://github.com/phpstan/phpstan/issues/4803 is solved
message: "#^Method Doctrine\\\\Persistence\\\\Mapping\\\\AbstractClassMetadataFactory\\:\\:getRealClass\\(\\) should return class\\-string\\<T of object\\> but returns class\\-string\\<Doctrine\\\\Persistence\\\\Proxy\\<T of object\\>\\|T of object\\>\\.$#"
count: 1
path: lib/Doctrine/Persistence/Mapping/AbstractClassMetadataFactory.php

0 comments on commit 210e9f5

Please sign in to comment.