Passed
Push — feature/optimize ( 219eef...0e0305 )
by Fu
03:34
created

ParserFieldsSearchTrait::parserFieldsSearch()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 2
nop 0
dl 0
loc 26
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
4
namespace Modules\Core\Traits\Criteria;
5
6
7
use Exception;
8
9
trait ParserFieldsSearchTrait
10
{
11
    /**
12
     * @throws \Exception
13
     */
14
    protected function parserFieldsSearch()
15
    {
16
        $this->parserSearchFields();
0 ignored issues
show
Bug introduced by
The method parserSearchFields() does not exist on Modules\Core\Traits\Crit...ParserFieldsSearchTrait. Did you maybe mean parserFieldsSearch()? ( Ignorable by Annotation )

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

16
        $this->/** @scrutinizer ignore-call */ 
17
               parserSearchFields();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
17
18
        $fields = $this->fieldsSearchable;
0 ignored issues
show
Bug introduced by
The property fieldsSearchable does not exist on Modules\Core\Traits\Crit...ParserFieldsSearchTrait. Did you mean fields?
Loading history...
19
20
        if (!is_null($this->searchFields) && is_array($this->searchFields)) {
21
22
            $this->parserOriginalFields();
23
24
            $fields = [];
25
26
            foreach ($this->originalFields as $field => $condition) {
27
                if (is_numeric($field)) {
28
                    $field = $condition;
29
                    $condition = "=";
30
                }
31
                if (in_array($field, (array) $this->searchFields)) {
32
                    $fields[$field] = $condition;
33
                }
34
            }
35
36
            $this->assertFieldsNotAccepted($fields);
37
        }
38
39
        $this->fields = $fields;
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
    }
41
42
    protected function parserOriginalFields()
43
    {
44
        $this->acceptedConditions = config('repository.criteria.acceptedConditions', ['=', 'like']);
0 ignored issues
show
Bug Best Practice introduced by
The property acceptedConditions does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
46
        $this->originalFields = $this->fieldsSearchable;
0 ignored issues
show
Bug Best Practice introduced by
The property originalFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The property fieldsSearchable does not exist on Modules\Core\Traits\Crit...ParserFieldsSearchTrait. Did you mean fields?
Loading history...
47
48
        foreach ($this->searchFields as $index => $field) {
49
            $field_parts = explode(':', $field);
50
            $temporaryIndex = array_search($field_parts[0], $this->originalFields);
51
52
            if (count($field_parts) == 2) {
53
                if (in_array($field_parts[1], $this->acceptedConditions)) {
54
                    unset($this->originalFields[$temporaryIndex]);
55
                    $field = $field_parts[0];
56
                    $condition = $field_parts[1];
57
                    $this->originalFields[$field] = $condition;
58
                    $this->searchFields[$index] = $field;
0 ignored issues
show
Bug Best Practice introduced by
The property searchFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
59
                }
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param array $fields
66
     * @throws \Exception
67
     */
68
    protected function assertFieldsNotAccepted($fields)
69
    {
70
        if (count($fields) == 0) {
71
            throw new Exception((string) trans('repository::criteria.fields_not_accepted', ['field' => implode(',', (array) $this->searchFields)]));
72
        }
73
    }
74
}