Skip to content

Commit 7a6c521

Browse files
authored
refactor: move proxy to real class converter to ClassUtil (#14)
1 parent 685af2f commit 7a6c521

File tree

2 files changed

+47
-22
lines changed

2 files changed

+47
-22
lines changed

src/Transformer/ObjectToObjectMetadata/Implementation/ProxyResolvingObjectToObjectMetadataFactory.php

+2-22
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\ObjectToObjectMetadata;
1717
use Rekalogika\Mapper\Transformer\ObjectToObjectMetadata\ObjectToObjectMetadataFactoryInterface;
18+
use Rekalogika\Mapper\Util\ClassUtil;
1819

1920
/**
2021
* @internal
@@ -31,31 +32,10 @@ public function createObjectToObjectMetadata(
3132
string $targetClass,
3233
): ObjectToObjectMetadata {
3334
$metadata = $this->decorated->createObjectToObjectMetadata(
34-
$this->resolveRealClass($sourceClass),
35+
ClassUtil::determineRealClassFromPossibleProxy($sourceClass),
3536
$targetClass,
3637
);
3738

3839
return $metadata;
3940
}
40-
41-
/**
42-
* @param class-string $class
43-
* @return class-string
44-
*/
45-
private function resolveRealClass(string $class): string
46-
{
47-
$pos = strrpos($class, '\\__CG__\\');
48-
49-
if ($pos === false) {
50-
$pos = strrpos($class, '\\__PM__\\');
51-
}
52-
53-
if ($pos !== false) {
54-
$class = substr($class, $pos + 8);
55-
}
56-
57-
assert(class_exists($class), sprintf('Class "%s" does not exist', $class));
58-
59-
return $class;
60-
}
6141
}

src/Util/ClassUtil.php

+45
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Rekalogika\Mapper\Util;
1515

16+
use Rekalogika\Mapper\Exception\UnexpectedValueException;
1617
use Symfony\Component\VarExporter\Internal\Hydrator;
1718

1819
/**
@@ -24,6 +25,50 @@ private function __construct()
2425
{
2526
}
2627

28+
/**
29+
* @template T of object
30+
* @param class-string<T> $class
31+
* @return class-string<T>
32+
*/
33+
public static function determineRealClassFromPossibleProxy(string $class): string
34+
{
35+
$inputClass = $class;
36+
37+
$pos = strrpos($class, '\\__CG__\\');
38+
39+
if ($pos === false) {
40+
$pos = strrpos($class, '\\__PM__\\');
41+
}
42+
43+
if ($pos !== false) {
44+
$class = substr($class, $pos + 8);
45+
}
46+
47+
if (!class_exists($class)) {
48+
throw new UnexpectedValueException(sprintf(
49+
'Trying to resolve the real class from possible proxy class "%s", got "%s", but the class does not exist',
50+
$inputClass,
51+
$class
52+
));
53+
}
54+
55+
/** @psalm-suppress DocblockTypeContradiction */
56+
if (!is_a($inputClass, $class, true)) {
57+
/** @psalm-suppress NoValue */
58+
throw new UnexpectedValueException(sprintf(
59+
'Trying to resolve the real class from possible proxy class "%s", got "%s", but the proxy "%s" is not a subclass of "%s"',
60+
$inputClass,
61+
$class,
62+
$inputClass,
63+
$class,
64+
));
65+
}
66+
67+
/** @var class-string<T> $class */
68+
69+
return $class;
70+
}
71+
2772
/**
2873
* @param class-string|\ReflectionClass<object> $class
2974
* @return int

0 commit comments

Comments
 (0)