Passed
Push — master ( 689162...322ced )
by Damien
03:19
created

DoctrineHelper::getRealClassName()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 6
nop 1
dl 0
loc 21
rs 9.5222
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 $class
13
     *
14
     * @return string
15
     *
16
     * credits
17
     * https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php
18
     */
19
    public static function getRealClassName($class): string
20
    {
21
        $class = \is_object($class) ? \get_class($class) : $class;
22
23
        // __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0)
24
        // __PM__: Ocramius Proxy Manager (ODM >= 2.0)
25
        $positionCg = mb_strrpos($class, '\\__CG__\\');
26
        $positionPm = mb_strrpos($class, '\\__PM__\\');
27
        if ((false === $positionCg) &&
28
            (false === $positionPm)) {
29
            return $class;
30
        }
31
        if (false !== $positionCg) {
32
            return mb_substr($class, $positionCg + 8);
33
        }
34
        $className = ltrim($class, '\\');
35
36
        return mb_substr(
37
            $className,
38
            8 + $positionPm,
39
            mb_strrpos($className, '\\') - ($positionPm + 8)
40
        );
41
    }
42
}
43