DoctrineHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 43
rs 10
wmc 8

3 Methods

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