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.
Completed
Push — master ( daa81e...27aa0b )
by Benjamin
02:25
created

HydratorDecoratorFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 36%

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
ccs 9
cts 25
cp 0.36
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createHydratorForClass() 0 13 3
A createHydratorFile() 0 57 2
A getHydratorForClass() 0 11 4
1
<?php
2
3
namespace Lib\Db\Orm;
4
5
use GuzzleHttp\Psr7\UriResolver;
6
7
class HydratorDecoratorFactory
8
{
9
    protected $decorators = [];
10
11
    public function getHydratorForClass($classOrObject)
12
    {
13
        if (!is_string($classOrObject) && is_object($classOrObject)) {
14
            $classOrObject = get_class($classOrObject);
15
        }
16
17
        if (!isset($this->decorators[$classOrObject])) {
18
            $this->createHydratorForClass($classOrObject);
19
        }
20
21
        return $this->decorators[$classOrObject];
22
    }
23
24
    public function createHydratorForClass($class)
25
    {
26
        $shortClass    = array_pop(explode('\\', $class));
0 ignored issues
show
Bug introduced by
explode('\', $class) cannot be passed to array_pop() as the parameter $array expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        $shortClass    = array_pop(/** @scrutinizer ignore-type */ explode('\\', $class));
Loading history...
27
        $hydratorClass = $class.'HydratorDecorator';
28
        $dir           = UriResolver::removeDotSegments(__DIR__.'../../../../var/cache/orm/hydrators/');
29
        $hydratorFile  = $dir.$hydratorClass.'.php';
30
31
        if (!file_exists($hydratorFile) || !is_readable($hydratorFile)) {
32
            $this->createHydratorFile($shortClass, $hydratorClass, $hydratorFile);
33
        }
34
35
        include_once($hydratorFile);
36
        $this->decorators[$class] = new $hydratorClass();
37
    }
38
39 1
    public function createHydratorFile($entityClass, $hydratorClass, $hydratorFile)
40
    {
41 1
        $dir = UriResolver::removeDotSegments(dirname($hydratorFile));
42 1
        if (!is_dir($dir)) {
43 1
            mkdir($dir, 0777, true);
44
        }
45
46
        $classHeading = <<<EOD
47
<?php
48
49
use Lib\\Access\\Accessor;
50
51 1
class $hydratorClass extends $entityClass
52
EOD;
53
        $classBody = <<<'EOD'
54 1
{
55
    protected $accessor;
56
57
    public function hydrate(array $data = [])
58
    {
59
        $props = get_object_vars($this);
60
        $camelized = $this->camelizeData($data);
61
62
        foreach (array_keys($props) as $property) {
63
            if ('accessor' === $property) {
64
                continue;
65
            }
66
            if (array_key_exists($property, $camelized)) {
67
                $this->$property = $camelized[$property];
68
            }
69
        }
70
    }
71
72
    public function camelizeData(array $data)
73
    {
74
        $camelized = [];
75
        foreach ($data as $key => $value) {
76
            $camelized[$this->camelize($key)] = $value;
77
        }
78
        return $camelized;
79
    }
80
81
    public function camelize(string $string)
82
    {
83
        if (!$this->accessor || !($this->accessor instanceof Accessor)) {
84
            $this->accessor = new Accessor();
85
        }
86
        return $this->accessor->camelize($string);
87
    }
88
}
89
90
EOD;
91
        $class = <<<EOD
92 1
$classHeading
93 1
$classBody
94
EOD;
95 1
        return file_put_contents($hydratorFile, $class);
96
    }
97
}
98