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 ( 746a07...6b648b )
by
unknown
7s
created

Scope::toArray()   B

↳ Parent: Scope

Complexity

Conditions 9
Paths 63

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 54
Code Lines 25

Code Coverage

Tests 30
CRAP Score 9

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 54
ccs 30
cts 30
cp 1
rs 7.255
cc 9
eloc 25
nc 63
nop 0
crap 9

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the League\Fractal package.
5
 *
6
 * (c) Phil Sturgeon <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 **/
11
12
namespace League\Fractal;
13
14
use InvalidArgumentException;
15
use League\Fractal\Resource\Collection;
16
use League\Fractal\Resource\Item;
17
use League\Fractal\Resource\NullResource;
18
use League\Fractal\Resource\ResourceInterface;
19
use League\Fractal\Serializer\SerializerAbstract;
20
21
/**
22
 * Scope
23
 *
24
 * The scope class acts as a tracker, relating a specific resource in a specific
25
 * context. For example, the same resource could be attached to multiple scopes.
26
 * There are root scopes, parent scopes and child scopes.
27
 */
28
class Scope
29
{
30
    /**
31
     * @var array
32
     */
33
    protected $availableIncludes = [];
34
35
    /**
36
     * @var string
37
     */
38
    protected $scopeIdentifier;
39
40
    /**
41
     * @var \League\Fractal\Manager
42
     */
43
    protected $manager;
44
45
    /**
46
     * @var ResourceInterface
47
     */
48
    protected $resource;
49
50
    /**
51
     * @var array
52
     */
53
    protected $parentScopes = [];
54
55
    /**
56
     * Create a new scope instance.
57
     *
58
     * @param Manager           $manager
59
     * @param ResourceInterface $resource
60
     * @param string            $scopeIdentifier
61
     *
62
     * @return void
63
     */
64 59
    public function __construct(Manager $manager, ResourceInterface $resource, $scopeIdentifier = null)
65
    {
66 59
        $this->manager = $manager;
67 59
        $this->resource = $resource;
68 59
        $this->scopeIdentifier = $scopeIdentifier;
69 59
    }
70
71
    /**
72
     * Embed a scope as a child of the current scope.
73
     *
74
     * @internal
75
     *
76
     * @param string            $scopeIdentifier
77
     * @param ResourceInterface $resource
78
     *
79
     * @return \League\Fractal\Scope
80
     */
81 30
    public function embedChildScope($scopeIdentifier, $resource)
82
    {
83 30
        return $this->manager->createData($resource, $scopeIdentifier, $this);
84
    }
85
86
    /**
87
     * Get the current identifier.
88
     *
89
     * @return string
90
     */
91 30
    public function getScopeIdentifier()
92
    {
93 30
        return $this->scopeIdentifier;
94
    }
95
96
    /**
97
     * Get the unique identifier for this scope.
98
     *
99
     * @param string $appendIdentifier
100
     *
101
     * @return string
102
     */
103 26
    public function getIdentifier($appendIdentifier = null)
104
    {
105 26
        $identifierParts = array_merge($this->parentScopes, [$this->scopeIdentifier, $appendIdentifier]);
106
107 26
        return implode('.', array_filter($identifierParts));
108
    }
109
110
    /**
111
     * Getter for parentScopes.
112
     *
113
     * @return mixed
114
     */
115 31
    public function getParentScopes()
116
    {
117 31
        return $this->parentScopes;
118
    }
119
120
    /**
121
     * Getter for resource.
122
     *
123
     * @return ResourceInterface
124
     */
125 1
    public function getResource()
126
    {
127 1
        return $this->resource;
128
    }
129
130
    /**
131
     * Getter for manager.
132
     *
133
     * @return \League\Fractal\Manager
134
     */
135 26
    public function getManager()
136
    {
137 26
        return $this->manager;
138 1
    }
139
140
    /**
141
     * Is Requested.
142
     *
143
     * Check if - in relation to the current scope - this specific segment is allowed.
144
     * That means, if a.b.c is requested and the current scope is a.b, then c is allowed. If the current
145
     * scope is a then c is not allowed, even if it is there and potentially transformable.
146
     *
147
     * @internal
148
     *
149
     * @param string $checkScopeSegment
150
     *
151
     * @return bool Returns the new number of elements in the array.
152
     */
153 36 View Code Duplication
    public function isRequested($checkScopeSegment)
0 ignored issues
show
Duplication introduced by Phil Sturgeon
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...
154
    {
155 36
        if ($this->parentScopes) {
156 16
            $scopeArray = array_slice($this->parentScopes, 1);
157 16
            array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment);
158 16
        } else {
159 36
            $scopeArray = [$checkScopeSegment];
160
        }
161
162 36
        $scopeString = implode('.', (array) $scopeArray);
163
164 36
        return in_array($scopeString, $this->manager->getRequestedIncludes());
165
    }
166
167
    /**
168
     * Is Excluded.
169
     *
170
     * Check if - in relation to the current scope - this specific segment should
171
     * be excluded. That means, if a.b.c is excluded and the current scope is a.b,
172
     * then c will not be allowed in the transformation whether it appears in
173
     * the list of default or available, requested includes.
174
     *
175
     * @internal
176
     *
177
     * @param string $checkScopeSegment
178
     *
179
     * @return bool
180
     */
181 26 View Code Duplication
    public function isExcluded($checkScopeSegment)
0 ignored issues
show
Duplication introduced by Jason Daly
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...
182
    {
183 26
        if ($this->parentScopes) {
0 ignored issues
show
Bug Best Practice introduced by Jason Daly
The expression $this->parentScopes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
184 3
            $scopeArray = array_slice($this->parentScopes, 1);
185 3
            array_push($scopeArray, $this->scopeIdentifier, $checkScopeSegment);
186 3
        } else {
187 26
            $scopeArray = [$checkScopeSegment];
188
        }
189
190 26
        $scopeString = implode('.', (array) $scopeArray);
191
192 26
        return in_array($scopeString, $this->manager->getRequestedExcludes());
193
    }
194
195
    /**
196
     * Push Parent Scope.
197
     *
198
     * Push a scope identifier into parentScopes
199
     *
200
     * @internal
201
     *
202
     * @param string $identifierSegment
203
     *
204
     * @return int Returns the new number of elements in the array.
205
     */
206 1
    public function pushParentScope($identifierSegment)
207
    {
208 1
        return array_push($this->parentScopes, $identifierSegment);
209
    }
210
211
    /**
212
     * Set parent scopes.
213
     *
214
     * @internal
215
     *
216
     * @param string[] $parentScopes Value to set.
217
     *
218
     * @return $this
219
     */
220 30
    public function setParentScopes($parentScopes)
221
    {
222 30
        $this->parentScopes = $parentScopes;
223
224 30
        return $this;
225
    }
226
227
    /**
228
     * Convert the current data for this scope to an array.
229
     *
230
     * @return array
231
     */
232 49
    public function toArray()
233
    {
234 49
        list($rawData, $rawIncludedData) = $this->executeResourceTransformers();
235
236 48
        $serializer = $this->manager->getSerializer();
237
238 48
        $data = $this->serializeResource($serializer, $rawData);
239
240
        // If the serializer wants the includes to be side-loaded then we'll
241
        // serialize the included data and merge it with the data.
242 47
        if ($serializer->sideloadIncludes()) {
243 29
            $includedData = $serializer->includedData($this->resource, $rawIncludedData);
244
245
            // If the serializer wants to inject additional information
246
            // about the included resources, it can do so now.
247 29
            $data = $serializer->injectData($data, $rawIncludedData);
248
249 29
            if ($this->isRootScope()) {
250
                // If the serializer wants to have a final word about all
251
                // the objects that are sideloaded, it can do so now.
252 29
                $includedData = $serializer->filterIncludes(
253 29
                    $includedData,
254
                    $data
255 29
                );
256 29
            }
257
258 29
            $data = array_merge($data, $includedData);
259 29
        }
260
261 47
        if ($this->resource instanceof Collection) {
262 26
            if ($this->resource->hasCursor()) {
263 1
                $pagination = $serializer->cursor($this->resource->getCursor());
264 26
            } elseif ($this->resource->hasPaginator()) {
265 4
                $pagination = $serializer->paginator($this->resource->getPaginator());
266 4
            }
267
268 26
            if (! empty($pagination)) {
269 5
                $this->resource->setMetaValue(key($pagination), current($pagination));
270 5
            }
271 26
        }
272
273
        // Pull out all of OUR metadata and any custom meta data to merge with the main level data
274 47
        $meta = $serializer->meta($this->resource->getMeta());
275
276
        // in case of returning NullResource we should return null and not to go with array_merge
277 47
        if (is_null($data)) {
278 2
            if (!empty($meta)) {
279 1
                return $meta;
280
            }
281 1
            return null;
282
        }
283
284 46
        return array_merge($data, $meta);
285
    }
286
287
    /**
288
     * Convert the current data for this scope to JSON.
289
     *
290
     * @return string
291
     */
292 34
    public function toJson()
293
    {
294 34
        return json_encode($this->toArray());
295
    }
296
297
    /**
298
     * Execute the resources transformer and return the data and included data.
299
     *
300
     * @internal
301
     *
302
     * @return array
303
     */
304 48
    protected function executeResourceTransformers()
305
    {
306 48
        $transformer = $this->resource->getTransformer();
307 48
        $data = $this->resource->getData();
308
309 48
        $transformedData = $includedData = [];
310
311 48
        if ($this->resource instanceof Item) {
312 32
            list($transformedData, $includedData[]) = $this->fireTransformer($transformer, $data);
313 48
        } elseif ($this->resource instanceof Collection) {
314 26
            foreach ($data as $value) {
315 25
                list($transformedData[], $includedData[]) = $this->fireTransformer($transformer, $value);
316 26
            }
317 32
        } elseif ($this->resource instanceof NullResource) {
318 5
            $transformedData = null;
319 5
            $includedData = [];
320 5
        } else {
321 2
            throw new InvalidArgumentException(
322
                'Argument $resource should be an instance of League\Fractal\Resource\Item'
323
                .' or League\Fractal\Resource\Collection'
324 2
            );
325
        }
326
327 46
        return [$transformedData, $includedData];
328
    }
329
330
    /**
331
     * Serialize a resource
332
     *
333
     * @internal
334
     *
335
     * @param SerializerAbstract $serializer
336
     * @param mixed              $data
337
     *
338
     * @return array
339
     */
340 46
    protected function serializeResource(SerializerAbstract $serializer, $data)
341
    {
342 46
        $resourceKey = $this->resource->getResourceKey();
343
344 46
        if ($this->resource instanceof Collection) {
345 26
            return $serializer->collection($resourceKey, $data);
346
        }
347
348 34
        if ($this->resource instanceof Item) {
349 32
            return $serializer->item($resourceKey, $data);
350
        }
351
352 5
        return $serializer->null();
353
    }
354
355
    /**
356
     * Fire the main transformer.
357
     *
358
     * @internal
359
     *
360
     * @param TransformerAbstract|callable $transformer
361
     * @param mixed                        $data
362
     *
363
     * @return array
364
     */
365 44
    protected function fireTransformer($transformer, $data)
366
    {
367 44
        $includedData = [];
368
369 44
        if (is_callable($transformer)) {
370 4
            $transformedData = call_user_func($transformer, $data);
371 4
        } else {
372 40
            $transformer->setCurrentScope($this);
373 40
            $transformedData = $transformer->transform($data);
374
        }
375
376 44
        if ($this->transformerHasIncludes($transformer)) {
377 38
            $includedData = $this->fireIncludedTransformers($transformer, $data);
378 38
            $transformedData = $this->manager->getSerializer()->mergeIncludes($transformedData, $includedData);
379 38
        }
380
381 44
        return [$transformedData, $includedData];
382
    }
383
384
    /**
385
     * Fire the included transformers.
386
     *
387
     * @internal
388
     *
389
     * @param \League\Fractal\TransformerAbstract $transformer
390
     * @param mixed                               $data
391
     *
392
     * @return array
393
     */
394 38
    protected function fireIncludedTransformers($transformer, $data)
395
    {
396 38
        $this->availableIncludes = $transformer->getAvailableIncludes();
397
398 38
        return $transformer->processIncludedResources($this, $data) ?: [];
399
    }
400
401
    /**
402
     * Determine if a transformer has any available includes.
403
     *
404
     * @internal
405
     *
406
     * @param TransformerAbstract|callable $transformer
407
     *
408
     * @return bool
409
     */
410 44
    protected function transformerHasIncludes($transformer)
411
    {
412 44
        if (! $transformer instanceof TransformerAbstract) {
413 4
            return false;
414
        }
415
416 40
        $defaultIncludes = $transformer->getDefaultIncludes();
417 40
        $availableIncludes = $transformer->getAvailableIncludes();
418
419 40
        return ! empty($defaultIncludes) || ! empty($availableIncludes);
420
    }
421
422
    /**
423
     * Check, if this is the root scope.
424
     *
425
     * @return bool
426
     */
427 29
    protected function isRootScope()
428
    {
429 29
        return empty($this->parentScopes);
430
    }
431
}
432