Passed
Branch master (8cb5b2)
by Christopher
04:33 queued 12s
created

EloquentFiller::loadRelationsAttributesRecursive()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 47.1143

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 26
nop 1
dl 0
loc 30
ccs 4
cts 18
cp 0.2222
crap 47.1143
rs 8.0555
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
The PHP open tag does not have a corresponding PHP close tag
Loading history...
2
namespace Triadev\Leopard\Business\Filler;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use Illuminate\Database\Eloquent\Model;
5
use Illuminate\Database\Eloquent\Relations\Relation;
6
use Illuminate\Support\Collection;
7
use Triadev\Leopard\Contract\FillerContract;
8
use Triadev\Leopard\Model\SearchResult;
9
use Triadev\Leopard\Searchable;
10
11
class EloquentFiller implements FillerContract
0 ignored issues
show
Coding Style Documentation introduced by
Missing class doc comment
Loading history...
12
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for class EloquentFiller
Loading history...
13
    /**
14
     * Fill
15
     *
16
     * @param Model $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 8 spaces after parameter type; 1 found
Loading history...
17
     * @param SearchResult $searchResult
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
18
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
19 1
    public function fill(Model $model, SearchResult $searchResult)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 0 found
Loading history...
20
    {
21 1
        $searchResult->setHits(
22
            $searchResult->getHits()->map(function (array $hit) use ($model) {
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
23 1
                return $this->fillModel($model, $hit);
24 1
            })
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
25
        );
26 1
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end fill()
Loading history...
27
    
28
    /**
29
     * Fill model
30
     *
31
     * @param Model $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
32
     * @param array $hit
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 1
Loading history...
33
     * @return Model
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
34
     */
35 1
    public function fillModel(Model $model, array $hit = []) : Model
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$hit" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$hit"; expected 0 but found 1
Loading history...
36
    {
37 1
        $source = array_get($hit, '_source', []);
38
        
39 1
        if ($id = array_get($hit, '_id', null)) {
0 ignored issues
show
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
40 1
            $source[$model->getKeyName()] = is_numeric($id) ? intval($id) : $id;
0 ignored issues
show
Coding Style introduced by
Inline IF statements are not allowed
Loading history...
41
        }
42
        
43
        /** @var Model|Searchable $instance */
0 ignored issues
show
Coding Style introduced by
Inline doc block comments are not allowed; use "/* Comment */" or "// Comment" instead
Loading history...
Coding Style introduced by
Block comments must be started with /*
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
44 1
        $instance = $this->newFromBuilderRecursive($model, $source);
45
        
46 1
        $instance->documentScore = array_get($hit, '_score');
47 1
        $instance->isDocument = true;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
48
        
49 1
        if ($version = array_get($hit, '_version')) {
0 ignored issues
show
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
50
            $instance->documentVersion = $version;
51
        }
52
        
53 1
        return $instance;
54
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end fillModel()
Loading history...
55
    
56
    /**
57
     * Fill a model with form an elastic hit.
58
     *
59
     * @param Model $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     * @param array $attributes
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     *
62
     * @return Model
63
     */
64 1
    public function newFromBuilderRecursive(Model $model, array $attributes = []) : Model
0 ignored issues
show
Coding Style introduced by
Incorrect spacing between argument "$attributes" and equals sign; expected 0 but found 1
Loading history...
Coding Style introduced by
Incorrect spacing between default value and equals sign for argument "$attributes"; expected 0 but found 1
Loading history...
65
    {
66 1
        $instance = $model->newInstance([], $exists = true);
0 ignored issues
show
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
67
        
68 1
        $instance->unguard();
69
        
70 1
        $instance->fill($attributes);
71 1
        $instance->reguard();
72
        
73 1
        $this->loadRelationsAttributesRecursive($instance);
74
        
75 1
        return $instance;
76
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end newFromBuilderRecursive()
Loading history...
77
    
78
    /**
79
     * Get the relations attributes from a model.
80
     *
81
     * @param \Illuminate\Database\Eloquent\Model $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
83 1
    protected function loadRelationsAttributesRecursive(Model $model)
1 ignored issue
show
Coding Style introduced by
Function's nesting level (6) exceeds 5; consider refactoring the function
Loading history...
84
    {
85 1
        foreach ($model->getAttributes() as $key => $value) {
86 1
            if (!method_exists($model, $key)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
87 1
                continue;
88
            }
89
            
90
            try {
91
                if ((new \ReflectionMethod($model, $key))->class != Model::class) {
0 ignored issues
show
Coding Style introduced by
Operator != prohibited; use !== instead
Loading history...
92
                    $relation = $model->$key();
93
                    if ($relation instanceof Relation) {
94
                        if ($value === null) {
95
                            $models = null;
96
                        } else {
97
                            if (!$multiLevelRelation = $this->isMultiLevelArray($value)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
98
                                $value = [$value];
99
                            }
100
                
101
                            $models = $this->hydrateRecursive($relation->getModel(), $value);
102
                            if (!$multiLevelRelation) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
103
                                $models = $models->first();
104
                            }
105
                        }
106
            
107
                        unset($model[$key]);
108
                        $model->setRelation($key, $models);
109
                    }
110
                }
111
            } catch (\ReflectionException $e) {
112
                continue;
113
            }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end try"
Loading history...
114
        }
0 ignored issues
show
Coding Style introduced by
End comment for long condition not found; expected "//end foreach"
Loading history...
115 1
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end loadRelationsAttributesRecursive()
Loading history...
116
    
117
    /**
118
     * Create a collection of models from plain arrays recursive.
119
     *
120
     * @param Model    $model
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
121
     * @param array    $items
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter type; 4 found
Loading history...
122
     *
123
     * @return Collection
124
     */
125
    protected function hydrateRecursive(Model $model, array $items)
126
    {
127
        return $model->newCollection(array_map(function ($item) use ($model) {
1 ignored issue
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
128
            return $this->newFromBuilderRecursive($model, $item);
129
        }, $items));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
130
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end hydrateRecursive()
Loading history...
131
    
132
    /**
133
     * Check if an array is multi-level array like [[id], [id], [id]].
134
     *
135
     * For detect if a relation field is single model or collections.
136
     *
137
     * @param array $array
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     *
139
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
140
     */
141
    private function isMultiLevelArray(array $array)
0 ignored issues
show
Coding Style introduced by
Private method name "EloquentFiller::isMultiLevelArray" must be prefixed with an underscore
Loading history...
142
    {
143
        foreach ($array as $key => $value) {
144
            if (!is_array($value)) {
0 ignored issues
show
Coding Style introduced by
There must be a single space after a NOT operator; 0 found
Loading history...
145
                return false;
146
            }
147
        }
148
        
149
        return true;
150
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end isMultiLevelArray()
Loading history...
151
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
152