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

DoctrineHelper   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A generateProxyClassName() 0 3 1
A getRealClassName() 0 21 5
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
    /**
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