Passed
Push — master ( 356dd9...ecadea )
by Damien
02:16
created

DoctrineHelper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRealClassName() 0 21 5
A getDoctrineType() 0 3 2
1
<?php
2
3
namespace DH\DoctrineAuditBundle\Helper;
4
5
use Doctrine\DBAL\Types\Type;
6
use Doctrine\DBAL\Types\Types;
7
use Doctrine\Persistence\Proxy;
8
9
class DoctrineHelper
10
{
11
    /**
12
     * Gets the real class name of a class name that could be a proxy.
13
     *
14
     * @param object|string $class
15
     *
16
     * @return string
17
     *
18
     * credits
19
     * https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php
20
     */
21
    public static function getRealClassName($class): string
22
    {
23
        $class = \is_object($class) ? \get_class($class) : $class;
24
25
        // __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0)
26
        // __PM__: Ocramius Proxy Manager (ODM >= 2.0)
27
        $positionCg = mb_strrpos($class, '\\__CG__\\');
28
        $positionPm = mb_strrpos($class, '\\__PM__\\');
29
        if ((false === $positionCg) &&
30
            (false === $positionPm)) {
31
            return $class;
32
        }
33
        if (false !== $positionCg) {
34
            return mb_substr($class, $positionCg + 8);
35
        }
36
        $className = ltrim($class, '\\');
37
38
        return mb_substr(
39
            $className,
40
            8 + $positionPm,
41
            mb_strrpos($className, '\\') - ($positionPm + 8)
42
        );
43
    }
44
45
    public static function getDoctrineType(string $type): string
46
    {
47
        return \constant((class_exists(Types::class, false) ? Types::class : Type::class).'::'.$type);
48
    }
49
}
50