Passed
Pull Request — master (#143)
by Federico
02:19
created

DoctrineHelper::getRealClassName()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 1
dl 0
loc 18
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Helper;
4
5
use Doctrine\Common\Persistence\Proxy;
6
7
class DoctrineHelper
8
{
9
    /**
10
     * Gets the real class name of a class name that could be a proxy.
11
     *
12
     * @param object|string $subject
13
     *
14
     * @return string
15
     *
16
     * credits
17
     * https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php
18
     */
19
    /**
20
     * Get the real class name of a class name that could be a proxy.
21
     */
22
    public static function getRealClassName($class): string
23
    {
24
        $class = \is_object($class) ? \get_class($class) : $class;
25
26
        // __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0)
27
        // __PM__: Ocramius Proxy Manager (ODM >= 2.0)
28
        if ((false === $positionCg = strrpos($class, '\\__CG__\\')) &&
29
            (false === $positionPm = strrpos($class, '\\__PM__\\'))) {
30
            return $class;
31
        }
32
        if (false !== $positionCg) {
33
            return substr($class, $positionCg + 8);
34
        }
35
        $className = ltrim($class, '\\');
36
        return substr(
37
            $className,
38
            8 + $positionPm,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $positionPm does not seem to be defined for all execution paths leading up to this point.
Loading history...
39
            strrpos($className, '\\') - ($positionPm + 8)
40
        );
41
    }
42
43
    /**
44
     * Given a class name and a proxy namespace returns the proxy name.
45
     *
46
     * @param string $className
47
     * @param string $proxyNamespace
48
     *
49
     * @return string
50
     */
51
    public static function generateProxyClassName($className, $proxyNamespace): string
52
    {
53
        return rtrim($proxyNamespace, '\\').'\\'.Proxy::MARKER.'\\'.ltrim($className, '\\');
54
    }
55
}
56