Completed
Push — master ( fb0370...84a128 )
by Ronaldo
03:03
created

AbstractTransformer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 32
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadItem() 0 7 1
A loadCollection() 0 9 2
1
<?php
2
3
namespace WSW\SiftScience\Transformers;
4
5
use League\Fractal\Serializer\ArraySerializer;
6
use League\Fractal\TransformerAbstract;
7
8
/**
9
 * Class AbstractTransformer
10
 *
11
 * @package WSW\SiftScience\Transformers
12
 * @author Ronaldo Matos Rodrigues <[email protected]>
13
 */
14
abstract class AbstractTransformer extends TransformerAbstract
15
{
16
    /**
17
     * @param mixed $data
18
     * @param AbstractTransformer $transformer
19
     *
20
     * @return array
21
     */
22
    protected function loadItem($data = null, AbstractTransformer $transformer = null)
23
    {
24
        return $this->getCurrentScope()
25
                    ->getManager()
26
                    ->createData($this->item($data, $transformer))
0 ignored issues
show
Bug introduced by
It seems like $transformer defined by parameter $transformer on line 22 can be null; however, League\Fractal\TransformerAbstract::item() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
27
                    ->toArray();
28
    }
29
30
    /**
31
     * @param mixed $data
32
     * @param AbstractTransformer $transformer
33
     *
34
     * @return array
35
     */
36
    protected function loadCollection($data = null, AbstractTransformer $transformer = null)
37
    {
38
        $result = $this->getCurrentScope()
39
                    ->getManager()
40
                    ->setSerializer(new ArraySerializer)
41
                    ->createData($this->collection($data, $transformer))->toArray();
0 ignored issues
show
Bug introduced by
It seems like $transformer defined by parameter $transformer on line 36 can be null; however, League\Fractal\TransformerAbstract::collection() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
42
43
        return (isset($result['data'])) ? $result['data'] : $result;
44
    }
45
}
46