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.

LaravelModelSerializer::toArray()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
namespace Solvire\API\Serializers;
3
4
use Illuminate\Database\Eloquent\Model;
5
use Solvire\API\Serializers\DataFields\SplitPointField;
6
use Solvire\API\Serializers\DataFields\SerializerField;
7
use Solvire\API\Serializers\DataFields\TransformerField;
8
9
/**
10
 * Map a Laravel 5.x model to a serializer
11
 *
12
 * @author solvire <[email protected]>
13
 * @package Serializers
14
 * @namespace Solvire\API\Serializers
15
 */
16
abstract class LaravelModelSerializer extends BaseSerializer
17
{
18
19
    /**
20
     *
21
     * @var Model
22
     */
23
    protected $model = null;
24
25
    /**
26
     *
27
     * @param Model $model            
28
     * @return \Solvire\API\Serializers\LaravelModelSerializer
29
     */
30 1
    public function setModel(Model $model)
31
    {
32 1
        $this->model = $model;
33 1
        return $this;
34
    }
35
36
    /**
37
     *
38
     * @return Model
39
     */
40 1
    public function getModel()
41
    {
42 1
        return $this->model;
43
    }
44
45 1
    public function jsonSerialize()
46
    {
47 1
        return $this->toArray();
48
    }
49
50
    /**
51
     * is update = when updating the database
52
     *
53
     * @param string $isUpdate            
54
     * @return multitype:NULL
0 ignored issues
show
Documentation introduced by
The doc-type multitype:NULL could not be parsed: Unknown type name "multitype:NULL" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
55
     */
56 1
    public function toArray($isUpdate = false)
0 ignored issues
show
Unused Code introduced by
The parameter $isUpdate is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58 1
        $ret = [];
59 1
        $dfc = $this->getDataFieldCollection();
60 1
        foreach ($dfc as $name => $dataField) {
61
            
62
            // write only fields should not be displayed 
63 1
            if($dataField->writeOnly())
64 1
                continue;
65
            
66 1
            $ret[$name] = $dataField->getData();
67 1
        }
68
        
69 1
        return $ret;
70
    }
71
72
    /**
73
     *
74
     * @param array $data            
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
     */
76 1
    public function loadData($model)
77
    {
78
        
79
        // make sure that this is a laravel model
80 1
        if (! $model instanceof Model)
81 1
            throw new \RuntimeException("the object must be of type Illuminate\Database\Eloquent\Model ");
82
            
83
        // loop through all the set fields in the collection
84
        // if they have a matching name then load them up from the model
85
        // if they have a matching columnName then find the value and load that up instead
86 1
        $dfc = $this->getDataFieldCollection();
87 1
        foreach ($dfc as $name => $dataField) {
88 1
            $col = $dataField->columnName();
89
            
90
            // i don't think laravel has a point type data field
91
            // we need to check to see if it's a point field split and combine it
92 1
            if ($dataField instanceof SplitPointField) {
93 1
                $this->loadSplitPoint($dataField, $model);
94 1
                continue;
95
            }
96
            
97
            
98
            // if this is a child serializer object then pass it down
99
            // we assume that since we are in laravel environment that we will have subs of the same
100
            // also assuming that the name of the column is the function name 
101 1
            if($dataField instanceof SerializerField) {
102
                
103
                // this might be better as a callback instead 
104
                // get the field name to call
105 1
                $dataField->setData($model);
106 1
                continue;
107
                
108
            }
109
            
110
            // if this is a child transformer object then pass it down
111
            // I need more use cases to see where htis will break 
112 1
            if($dataField instanceof TransformerField) {
113
                $dataField->setData($model);
114
                continue;
115
            }
116
            
117
            
118
            // if we have a column name then use that first
119 1
            $data = ($col) ? $model->$col : $model->$name;
120
            
121 1
            if (($data === null) && ! $dataField->allowNull())
122 1
                continue;
123
            
124 1
            if (($data === '') && ! $dataField->allowEmpty())
125 1
                continue;
126
            
127 1
            $dataField->setData($data);
128 1
        }
129
        
130 1
        return $this;
131
    }
132
133 1
    public function loadSplitPoint($dataField, $model)
134
    {
135 1
        $lat = $dataField->getLatitudeColumn();
136 1
        $lon = $dataField->getLongitudeColumn();
137 1
        $dataField->setData([
138 1
            'latitude' => $model->$lat,
139
            'longitude' => $model->$lon
140 1
        ]);
141 1
    }
142
}
143