Scope::serializeResource()   A
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 17
ccs 10
cts 10
cp 1
crap 5
rs 9.6111
1
<?php namespace Pz\Doctrine\Rest\Fractal;
2
3
use League\Fractal\Resource\Collection;
4
use League\Fractal\Resource\Item;
5
use League\Fractal\Serializer\JsonApiSerializer;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pz\Doctrine\Rest\Fractal\JsonApiSerializer. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use League\Fractal\Serializer\SerializerAbstract;
7
8
class Scope extends \League\Fractal\Scope
9
{
10
    /**
11
     * @var bool
12
     */
13
    protected $isRelationships = false;
14
15
    /**
16
     * @param null|bool $value
17
     *
18
     * @return bool|null
19
     */
20 19
    public function isRelationships($value = null)
21
    {
22 19
        if ($value !== null) {
23 19
            $this->isRelationships = $value;
24
        }
25
26 19
        return $this->isRelationships;
27
    }
28
29
    /**
30
     * @param JsonApiSerializer|SerializerAbstract $serializer
31
     * @param mixed                                $data
32
     *
33
     * @return array
34
     */
35 19
    protected function serializeResource(SerializerAbstract $serializer, $data)
36
    {
37 19
        $includeAttributes = true;
38 19
        $resourceKey = $this->resource->getResourceKey();
39 19
        if ($this->isRelationships() && $this->isRootScope()) {
40 4
            $includeAttributes = false;
41
        }
42
43 19
        if ($this->resource instanceof Collection) {
44 13
            return $serializer->collection($resourceKey, $data, $includeAttributes);
0 ignored issues
show
Unused Code introduced by
The call to League\Fractal\Serialize...rAbstract::collection() has too many arguments starting with $includeAttributes. ( Ignorable by Annotation )

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

44
            return $serializer->/** @scrutinizer ignore-call */ collection($resourceKey, $data, $includeAttributes);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
45
        }
46
47 10
        if ($this->resource instanceof Item) {
48 10
            return $serializer->item($resourceKey, $data, $includeAttributes);
0 ignored issues
show
Unused Code introduced by
The call to League\Fractal\Serialize...ializerAbstract::item() has too many arguments starting with $includeAttributes. ( Ignorable by Annotation )

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

48
            return $serializer->/** @scrutinizer ignore-call */ item($resourceKey, $data, $includeAttributes);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
49
        }
50
51 3
        return $serializer->null();
52
    }
53
}
54