Completed
Push — master ( 09bb07...3577e6 )
by Gabriel
04:08 queued 10s
created

SearchableRecordsTrait::findByQuery()   A

Complexity

Conditions 6
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 2
nop 2
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Records\Traits\Searchable;
4
5
use Nip\Database\Query\AbstractQuery as Query;
6
use Nip\Records\Navigator\Pagination\Paginator;
7
use Nip\Records\AbstractModels\Record;
8
use Nip\Records\Collections\Collection as RecordCollection;
9
10
/**
11
 * Trait SearchableRecordsTrait
12
 * @package Nip\Records\Traits\Searchable
13
 */
14
trait SearchableRecordsTrait
15
{
16
17
    /**
18
     * Returns paginated results
19
     * @param Paginator $paginator
20
     * @param array $params
21
     * @return mixed
22
     */
23
    public function paginate(Paginator $paginator, $params = [])
24
    {
25
        $query = $this->paramsToQuery($params);
0 ignored issues
show
Bug introduced by
It seems like paramsToQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

25
        /** @scrutinizer ignore-call */ 
26
        $query = $this->paramsToQuery($params);
Loading history...
26
27
        $countQuery = $this->getDB()->newSelect();
0 ignored issues
show
Bug introduced by
It seems like getDB() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        $countQuery = $this->/** @scrutinizer ignore-call */ getDB()->newSelect();
Loading history...
28
        $countQuery->count(['*', 'count']);
29
        $countQuery->from([$query, 'tbl']);
30
        $results = $countQuery->execute()->fetchResults();
31
        $count = $results[0]['count'];
32
33
        $paginator->setCount($count);
0 ignored issues
show
Bug introduced by
The method setCount() does not exist on Nip\Records\Navigator\Pagination\Paginator. ( Ignorable by Annotation )

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

33
        $paginator->/** @scrutinizer ignore-call */ 
34
                    setCount($count);

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...
34
35
        $params['limit'] = $paginator->getLimits();
0 ignored issues
show
Bug introduced by
The method getLimits() does not exist on Nip\Records\Navigator\Pagination\Paginator. Did you maybe mean getLimitStart()? ( Ignorable by Annotation )

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

35
        /** @scrutinizer ignore-call */ 
36
        $params['limit'] = $paginator->getLimits();

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...
36
37
        return $this->findByParams($params);
0 ignored issues
show
Bug introduced by
It seems like findByParams() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

37
        return $this->/** @scrutinizer ignore-call */ findByParams($params);
Loading history...
38
    }
39
40
    /**
41
     * @param array $params
42
     */
43 3
    protected function injectParams(&$params = [])
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

43
    protected function injectParams(/** @scrutinizer ignore-unused */ &$params = [])

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

Loading history...
44
    {
45 3
    }
46
47
    /**
48
     * Checks the registry before fetching from the database
49
     * @param mixed $primary
50
     * @return Record
51
     */
52
    public function findOne($primary)
53
    {
54
        $item = $this->getRegistry()->get($primary);
0 ignored issues
show
Bug introduced by
It seems like getRegistry() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

54
        $item = $this->/** @scrutinizer ignore-call */ getRegistry()->get($primary);
Loading history...
55
        if (!$item) {
56
            $all = $this->getRegistry()->get("all");
57
            if ($all) {
58
                $item = $all[$primary];
59
            }
60
            if (!$item) {
61
                $params['where'][] = ["`{$this->getTable()}`.`{$this->getPrimaryKey()}` = ?", $primary];
0 ignored issues
show
Bug introduced by
It seems like getPrimaryKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

61
                $params['where'][] = ["`{$this->getTable()}`.`{$this->/** @scrutinizer ignore-call */ getPrimaryKey()}` = ?", $primary];
Loading history...
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

61
                $params['where'][] = ["`{$this->/** @scrutinizer ignore-call */ getTable()}`.`{$this->getPrimaryKey()}` = ?", $primary];
Loading history...
Comprehensibility Best Practice introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.
Loading history...
62
                $item = $this->findOneByParams($params);
0 ignored issues
show
Bug introduced by
The method findOneByParams() does not exist on Nip\Records\Traits\Searc...\SearchableRecordsTrait. Did you maybe mean findOne()? ( Ignorable by Annotation )

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

62
                /** @scrutinizer ignore-call */ 
63
                $item = $this->findOneByParams($params);

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...
63
                if ($item) {
64
                    $this->getRegistry()->set($primary, $item);
65
                }
66
67
                return $item;
68
            }
69
        }
70
71
        return $item;
72
    }
73
74
    /**
75
     * @param Query $query
76
     * @param array $params
77
     * @return bool
78
     */
79
    public function findOneByQuery($query, $params = [])
80
    {
81
        $query->limit(1);
82
        $return = $this->findByQuery($query, $params);
83
        if (count($return) > 0) {
84
            return $return->rewind();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return->rewind() returns the type void which is incompatible with the documented return type boolean.
Loading history...
Bug introduced by
Are you sure the usage of $return->rewind() targeting Nip\Collections\AbstractCollection::rewind() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * @param $field
92
     * @param $value
93
     * @return mixed
94
     */
95
    public function findByField($field, $value)
96
    {
97
        $params['where'][] = ["$field " . (is_array($value) ? "IN" : "=") . " ?", $value];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.
Loading history...
98
99
        return $this->findByParams($params);
100
    }
101
102
    /**
103
     * @param Query $query
104
     * @param array $params
105
     * @return RecordCollection
106
     */
107
    public function findByQuery($query, $params = [])
108
    {
109
        $return = $this->newCollection();
0 ignored issues
show
Bug introduced by
It seems like newCollection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

109
        /** @scrutinizer ignore-call */ 
110
        $return = $this->newCollection();
Loading history...
110
111
        $results = $this->getDB()->execute($query);
112
        if ($results->numRows() > 0) {
113
            $pk = $this->getPrimaryKey();
114
            /** @noinspection PhpAssignmentInConditionInspection */
115
            while ($row = $results->fetchResult()) {
116
                $item = $this->getNew($row);
0 ignored issues
show
Bug introduced by
It seems like getNew() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

116
                /** @scrutinizer ignore-call */ 
117
                $item = $this->getNew($row);
Loading history...
117
                if (is_string($pk)) {
118
                    $this->getRegistry()->set($item->getPrimaryKey(), $item);
119
                }
120
                if (isset($params['indexKey']) && !empty($params['indexKey'])) {
121
                    $return->add($item, $params['indexKey']);
122
                } else {
123
                    $return->add($item);
124
                }
125
            }
126
        }
127
128
        return $return;
129
    }
130
}
131