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 ( 6776ef...c935c0 )
by Cees-Jan
07:09
created

Hydrator   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 225
Duplicated Lines 22.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 50
loc 225
wmc 23
lcom 1
cbo 5
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setUpAnnotations() 0 10 3
A hydrate() 12 12 1
A hydrateFQCN() 0 7 1
A hydrateApplyAnnotations() 13 13 3
A extract() 12 12 1
A extractFQCN() 0 6 1
A extractApplyAnnotations() 13 13 3
B getAnnotation() 0 31 4
A buildAsyncFromSync() 0 10 1
A getHydrator() 0 18 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
3
namespace ApiClients\Foundation\Hydrator;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use GeneratedHydrator\Configuration;
7
use ReflectionClass;
8
use ApiClients\Foundation\Resource\ResourceInterface;
9
use Zend\Hydrator\HydratorInterface;
10
11
class Hydrator
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $options;
17
18
    /**
19
     * @var array
20
     */
21
    protected $hydrators = [];
22
23
    /**
24
     * @var array
25
     */
26
    protected $annotations = [];
27
28
    /**
29
     * @var HandlerInterface[]
30
     */
31
    protected $annotationHandlers = [];
32
33
    /**
34
     * @var AnnotationReader
35
     */
36
    protected $annotationReader;
37
38
    /**
39
     * @param array $options
40
     */
41
    public function __construct(array $options)
42
    {
43
        $this->options = $options;
44
        $this->annotationReader = new AnnotationReader();
45
46
        $this->setUpAnnotations();
47
    }
48
49
    protected function setUpAnnotations()
50
    {
51
        if (!isset($this->options[Options::ANNOTATIONS])) {
52
            return;
53
        }
54
55
        foreach ($this->options[Options::ANNOTATIONS] as $annotationClass => $handler) {
56
            $this->annotationHandlers[$annotationClass] = new $handler($this);
57
        }
58
    }
59
60
    /**
61
     * @param string $class
62
     * @param array $json
63
     * @return ResourceInterface
64
     */
65 View Code Duplication
    public function hydrate(string $class, array $json): ResourceInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $fullClassName = implode(
68
            '\\',
69
            [
70
                $this->options[Options::NAMESPACE],
71
                $this->options[Options::NAMESPACE_SUFFIX],
72
                $class,
73
            ]
74
        );
75
        return $this->hydrateFQCN($fullClassName, $json);
76
    }
77
78
    /**
79
     * @param string $class
80
     * @param array $json
81
     * @return ResourceInterface
82
     */
83
    public function hydrateFQCN(string $class, array $json): ResourceInterface
84
    {
85
        $hydrator = $this->getHydrator($class);
86
        $object = new $class();
87
        $json = $this->hydrateApplyAnnotations($json, $object);
88
        return $hydrator->hydrate($json, $object);
89
    }
90
91
    /**
92
     * @param array $json
93
     * @param ResourceInterface $object
94
     * @return array
95
     */
96 View Code Duplication
    protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        foreach ($this->annotationHandlers as $annotationClass => $handler) {
99
            $annotation = $this->getAnnotation($object, $annotationClass);
100
            if ($annotation === null) {
101
                continue;
102
            }
103
104
            $json = $handler->hydrate($annotation, $json, $object);
105
        }
106
107
        return $json;
108
    }
109
110
    /**
111
     * @param string $class
112
     * @param ResourceInterface $object
113
     * @return array
114
     */
115 View Code Duplication
    public function extract(string $class, ResourceInterface $object): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
116
    {
117
        $fullClassName = implode(
118
            '\\',
119
            [
120
                $this->options[Options::NAMESPACE],
121
                $this->options[Options::NAMESPACE_SUFFIX],
122
                $class,
123
            ]
124
        );
125
        return $this->extractFQCN($fullClassName, $object);
126
    }
127
128
    /**
129
     * Takes a fully qualified class name and extracts the data for that class from the given $object
130
     * @param string $class
131
     * @param ResourceInterface $object
132
     * @return array
133
     */
134
    public function extractFQCN(string $class, ResourceInterface $object): array
135
    {
136
        $json = $this->getHydrator($class)->extract($object);
137
        $json = $this->extractApplyAnnotations($object, $json);
138
        return $json;
139
    }
140
141
    /**
142
     * @param array $json
143
     * @param ResourceInterface $object
144
     * @return array
145
     */
146 View Code Duplication
    protected function extractApplyAnnotations(ResourceInterface $object, array $json): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
147
    {
148
        foreach ($this->annotationHandlers as $annotationClass => $handler) {
149
            $annotation = $this->getAnnotation($object, $annotationClass);
150
            if ($annotation === null) {
151
                continue;
152
            }
153
154
            $json = $handler->extract($annotation, $object, $json);
155
        }
156
157
        return $json;
158
    }
159
160
    /**
161
     * @param ResourceInterface $object
162
     * @param string $annotationClass
163
     * @return null|AnnotationInterface
164
     */
165
    protected function getAnnotation(ResourceInterface $object, string $annotationClass)
166
    {
167
        $class = get_class($object);
168
        if (isset($this->annotations[$class][$annotationClass])) {
169
            return $this->annotations[$class][$annotationClass];
170
        }
171
172
        if (!isset($this->annotations[$class])) {
173
            $this->annotations[$class] = [];
174
        }
175
176
        $this->annotations[$class][$annotationClass] = $this->annotationReader
177
            ->getClassAnnotation(
178
                new ReflectionClass($object),
179
                $annotationClass
180
            )
181
        ;
182
183
        if (get_class($this->annotations[$class][$annotationClass]) === $annotationClass) {
184
            return $this->annotations[$class][$annotationClass];
185
        }
186
187
        $this->annotations[$class][$annotationClass] = $this->annotationReader
188
            ->getClassAnnotation(
189
                new ReflectionClass(get_parent_class($object)),
190
                $annotationClass
191
            )
192
        ;
193
194
        return $this->annotations[$class][$annotationClass];
195
    }
196
197
    /**
198
     * @param string $resource
199
     * @param ResourceInterface $object
200
     * @return ResourceInterface
201
     */
202
    public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface
203
    {
204
        return $this->hydrateFQCN(
205
            $this->options[Options::NAMESPACE] . '\\Async\\' . $resource,
206
            $this->extractFQCN(
207
                $this->options[Options::NAMESPACE] . '\\Sync\\' . $resource,
208
                $object
209
            )
210
        );
211
    }
212
213
    /**
214
     * @param string $class
215
     * @return HydratorInterface
216
     */
217
    protected function getHydrator(string $class): HydratorInterface
218
    {
219
        if (isset($this->hydrators[$class])) {
220
            return $this->hydrators[$class];
221
        }
222
223
        $config = new Configuration($class);
224
        if (isset($this->options[Options::RESOURCE_CACHE_DIR])) {
225
            $config->setGeneratedClassesTargetDir($this->options[Options::RESOURCE_CACHE_DIR]);
226
        }
227
        if (isset($this->options[Options::RESOURCE_NAMESPACE])) {
228
            $config->setGeneratedClassesNamespace($this->options[Options::RESOURCE_NAMESPACE]);
229
        }
230
        $hydrator = $config->createFactory()->getHydratorClass();
231
        $this->hydrators[$class] = new $hydrator;
232
233
        return $this->hydrators[$class];
234
    }
235
}
236