GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

HydratorDecoratorFactory::getShortClassName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Lib\DB\ORM;
6
7
use GuzzleHttp\Psr7\UriResolver;
8
9
class HydratorDecoratorFactory
10
{
11
    protected $decorators = [];
12
13 3
    public function getShortClassName($fqcn)
14
    {
15 3
        $classArray = explode('\\', $fqcn);
16
17 3
        return array_pop($classArray);
18
    }
19
20 2
    public function getHydratorForClass($classOrObject, array $args = [])
21
    {
22 2
        if (!\is_string($classOrObject) && \is_object($classOrObject)) {
23 1
            $classOrObject = \get_class($classOrObject);
24
        }
25
26 2
        if (!isset($this->decorators[$classOrObject])) {
27 2
            $this->createHydratorForClass($classOrObject, $args);
28
        }
29
30 2
        return $this->decorators[$this->getShortClassName($classOrObject)];
31
    }
32
33 3
    public function createHydratorForClass($class, array $args = [])
34
    {
35 3
        $shortClass = $this->getShortClassName($class);
36 3
        $hydratorClass = $shortClass.'HydratorDecorator';
37 3
        $dir = UriResolver::removeDotSegments(__DIR__.'/../../../../var/cache/orm/hydrators/');
38 3
        $hydratorFile = $dir.$hydratorClass.'.php';
39
40 3
        if (!file_exists($hydratorFile) || !is_readable($hydratorFile)) {
41 1
            $this->createHydratorFile($class, $hydratorClass, $hydratorFile);
42
        }
43
44 3
        if (!class_exists($hydratorClass)) {
45
            include $hydratorFile;
46
        }
47
48 3
        $this->decorators[$shortClass] = new $hydratorClass(...$args);
49 3
    }
50
51 2
    public function createHydratorFile($entityClass, $hydratorClass, $hydratorFile)
52
    {
53 2
        $dir = UriResolver::removeDotSegments(\dirname($hydratorFile));
54 2
        if (!is_dir($dir)) {
55 2
            mkdir($dir, 0777, true);
56
        }
57
58
        $classHeading = <<<EOD
59
<?php
60
61
use Lib\\Access\\Accessor;
62
63 2
class $hydratorClass extends $entityClass
64
EOD;
65
        $classBody = <<<'EOD'
66 2
{
67
    protected $accessor;
68
69
    public function hydrate(array $data = [])
70
    {
71
        $props = get_object_vars($this);
72
        $camelized = $this->camelizeData($data);
73
74
        foreach (array_keys($props) as $property) {
75
            if ('accessor' === $property) {
76
                continue;
77
            }
78
            if (array_key_exists($property, $camelized)) {
79
                $this->$property = $camelized[$property];
80
            }
81
        }
82
    }
83
84
    public function camelizeData(array $data)
85
    {
86
        $camelized = [];
87
        foreach ($data as $key => $value) {
88
            $camelized[$this->camelize($key)] = $value;
89
        }
90
        return $camelized;
91
    }
92
93
    public function camelize(string $string)
94
    {
95
        if (!$this->accessor || !($this->accessor instanceof Accessor)) {
96
            $this->accessor = new Accessor();
97
        }
98
        return $this->accessor->camelize($string);
99
    }
100
}
101
102
EOD;
103
        $class = <<<EOD
104 2
$classHeading
105 2
$classBody
106
EOD;
107
108 2
        return file_put_contents($hydratorFile, $class);
109
    }
110
}
111