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

Searchable::buildDocumentFromArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.0961

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 1
dl 0
loc 19
ccs 9
cts 11
cp 0.8182
crap 4.0961
rs 9.9
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;
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
4
use Carbon\Carbon;
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Triadev\Leopard\Contract\ElasticsearchManagerContract;
8
use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract;
9
use Triadev\Leopard\Facade\Leopard;
10
11
/**
12
 * Trait Searchable
13
 * @package Triadev\Leopard
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
14
 *
15
 * @property string $documentIndex
16
 * @property string $documentType
17
 * @property bool $syncDocument
18
 * @property array $searchable
19
 *
20
 * @method static ElasticsearchManagerContract search()
21
 * @methdo static ElasticsearchManagerContract suggest()
22
 * @method array buildDocument()
0 ignored issues
show
Coding Style introduced by
Tags must be grouped together in a doc comment
Loading history...
23
 * @method array toArray()
24
 * @method string getTable()
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
26
trait Searchable
27
{
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration for trait Searchable
Loading history...
28
    /** @var bool */
0 ignored issues
show
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...
29
    public $isDocument = false;
1 ignored issue
show
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
30
    
31
    /** @var float|null */
0 ignored issues
show
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...
32
    public $documentScore;
33
    
34
    /** @var int|null */
0 ignored issues
show
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...
35
    public $documentVersion;
36
    
37
    /**
38
     * Searchable boot model.
39
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
40 21
    public static function bootSearchable()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
41
    {
42
        static::saved(function (Model $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...
43
            /** @var Model|Searchable $model */
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
            if ($model->shouldSyncDocument()) {
45 1
                $model->repository()->save(/** @scrutinizer ignore-type */$model);
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...
46
            }
47 21
        });
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...
48
    
49
        static::deleted(function (Model $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...
50
            /** @var Model|Searchable $model */
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...
51
            if ($model->shouldSyncDocument()) {
52
                $model->repository()->delete(/** @scrutinizer ignore-type */$model);
1 ignored issue
show
Unused Code introduced by
The call to Illuminate\Database\Eloquent\Builder::delete() has too many arguments starting with $model. ( Ignorable by Annotation )

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

52
                $model->repository()->/** @scrutinizer ignore-call */ delete(/** @scrutinizer ignore-type */$model);

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...
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...
53
            }
54 21
        });
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...
55 21
    }
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 bootSearchable()
Loading history...
56
    
57
    /**
58
     * Repository
59
     *
60
     * @return ElasticsearchRepositoryContract
61
     */
62 1
    public function repository() : ElasticsearchRepositoryContract
63
    {
64 1
        return app(ElasticsearchRepositoryContract::class);
65
    }
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 repository()
Loading history...
66
    
67
    /**
68
     * Get document index
69
     *
70
     * @return string|null
71
     */
72 15
    public function getDocumentIndex(): ?string
73
    {
74 15
        if (property_exists($this, 'documentIndex')) {
75 14
            return $this->documentIndex;
76
        }
77
        
78 1
        return null;
79
    }
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 getDocumentIndex()
Loading history...
80
    
81
    /**
82
     * Get document type
83
     *
84
     * @return string
85
     */
86 13
    public function getDocumentType(): string
87
    {
88 13
        if (property_exists($this, 'documentType')) {
89 12
            return (string)$this->documentType;
90
        }
91
        
92 1
        return (string)$this->getTable();
93
    }
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 getDocumentType()
Loading history...
94
    
95
    /**
96
     * Should sync document
97
     *
98
     * @return bool
0 ignored issues
show
Coding Style introduced by
Expected "boolean" but found "bool" for function return type
Loading history...
99
     */
100 2
    public function shouldSyncDocument() : bool
101
    {
102 2
        if (property_exists($this, 'syncDocument')) {
103 2
            return (bool)$this->syncDocument;
104
        }
105
        
106 1
        return true;
107
    }
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 shouldSyncDocument()
Loading history...
108
    
109
    /**
110
     * Get document data
111
     *
112
     * @return array
113
     */
114 9
    public function getDocumentData() : array
115
    {
116 9
        if (method_exists($this, 'buildDocument')) {
117 7
            return $this->buildDocument();
118
        }
119
        
120 2
        if (property_exists($this, 'searchable') && is_array($this->searchable)) {
121 1
            return $this->buildDocumentFromArray($this->searchable);
122
        }
123
        
124 1
        return $this->toArray();
125
    }
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 getDocumentData()
Loading history...
126
    
127 1
    private function buildDocumentFromArray(array $searchable)
0 ignored issues
show
Coding Style introduced by
Private method name "Searchable::buildDocumentFromArray" must be prefixed with an underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
128
    {
129 1
        $document = [];
130
        
131 1
        foreach ($searchable as $value) {
132 1
            $result = $this->$value;
133
            
134 1
            if ($result instanceof Collection) {
135
                $result = $result->toArray();
136 1
            } elseif ($result instanceof Carbon) {
0 ignored issues
show
Coding Style introduced by
Usage of ELSEIF not allowed; use ELSE IF instead
Loading history...
137
                $result = $result->toDateTimeString();
138
            } else {
139 1
                $result = $this->$value;
140
            }
141
            
142 1
            $document[$value] = $result;
143
        }
144
        
145 1
        return $document;
146
    }
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 buildDocumentFromArray()
Loading history...
147
    
148
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $name should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $arguments should have a doc-comment as per coding-style.
Loading history...
149
     * @param $name
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
150
     * @param $arguments
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
151
     * @return mixed
152
     */
153 1
    public function __call($name, $arguments)
0 ignored issues
show
introduced by
Type hint "name" missing for
Loading history...
introduced by
Type hint "arguments" missing for
Loading history...
154
    {
155 1
        if ($name == 'search') {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
156
            return Leopard::search()->model($this);
1 ignored issue
show
Bug introduced by
$this of type Triadev\Leopard\Searchable is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Triadev\Leopard\Business\Dsl\Search::model(). ( Ignorable by Annotation )

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

156
            return Leopard::search()->model(/** @scrutinizer ignore-type */ $this);
Loading history...
157
        }
158
        
159 1
        if ($name == 'suggest') {
0 ignored issues
show
Coding Style introduced by
Operator == prohibited; use === instead
Loading history...
160
            return Leopard::suggest();
161
        }
162
        
163 1
        return parent::__call($name, $arguments);
164
    }
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 __call()
Loading history...
165
}
0 ignored issues
show
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...
166