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

DoctrineHelper::getDoctrineType()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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