Passed
Push — master ( 543cc3...82c6b4 )
by Gabriel
02:50 queued 12s
created

RecordsTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 106
rs 10
c 0
b 0
f 0
ccs 41
cts 44
cp 0.9318
wmc 17

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getUniqueFields() 0 7 2
A generateExistsParams() 0 16 3
A initUniqueFields() 0 11 4
A generateUniqueConditions() 0 8 2
A generateUniqueCondition() 0 7 2
A exists() 0 20 4
1
<?php
2
3
namespace Nip\Records\Traits\Unique;
4
5
use Nip\Records\AbstractModels\Record;
6
7
/**
8
 * Class RecordsTrait
9
 * @package Nip\Records\Traits\Unique
10
 */
11
trait RecordsTrait
12
{
13
    protected $uniqueFields = null;
14
15
    /**
16
     * @param Record $item
17
     * @return bool|false|Record
18
     */
19 1
    public function exists(Record $item)
20
    {
21 1
        $params = $this->generateExistsParams($item);
22
23 1
        if (!$params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
introduced by
$params is a non-empty array, thus ! $params is always false.
Loading history...
24
            return false;
25
        }
26
27 1
        $where = $params['where'];
28 1
        $uniqueWhere = [];
29 1
        foreach ($where as $key => $value) {
30 1
            if (strpos($key, 'UNQ') !== false) {
31 1
                $uniqueWhere[] = $value;
32 1
                unset($params['where'][$key]);
33
            }
34
        }
35
36 1
        $params['where'][] = implode(' OR ', $uniqueWhere);
37
38 1
        return $this->findOneByParams($params);
0 ignored issues
show
Bug introduced by
It seems like findOneByParams() 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

38
        return $this->/** @scrutinizer ignore-call */ findOneByParams($params);
Loading history...
39
    }
40
41
    /**
42
     * @param Record $item
43
     * @return array|bool
44
     */
45 2
    public function generateExistsParams(Record $item)
46
    {
47 2
        $conditions = $this->generateUniqueConditions($item);
48
49 2
        if (count($conditions) < 1) {
50
            return false;
51
        }
52
53 2
        $params = [];
54 2
        $params['where'] = $conditions;
55
56 2
        $pk = $this->getPrimaryKey();
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

56
        /** @scrutinizer ignore-call */ 
57
        $pk = $this->getPrimaryKey();
Loading history...
57 2
        if ($item->getPrimaryKey()) {
58
            $params['where'][] = ["$pk != ?", $item->getPrimaryKey()];
59
        }
60 2
        return $params;
61
    }
62
63
    /**
64
     * @param Record $item
65
     * @return array|bool
66
     */
67 2
    public function generateUniqueConditions(Record $item)
68
    {
69 2
        $uniqueFields = $this->getUniqueFields();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $uniqueFields is correct as $this->getUniqueFields() targeting Nip\Records\Traits\Uniqu...rait::getUniqueFields() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
70 2
        $conditions = [];
71 2
        foreach ($uniqueFields as $uniqueName => $fields) {
0 ignored issues
show
Bug introduced by
The expression $uniqueFields of type null is not traversable.
Loading history...
72 2
            $conditions[$uniqueName . '-UNQ'] = $this->generateUniqueCondition($item, $fields);
73
        }
74 2
        return $conditions;
75
    }
76
77
    /**
78
     * @param Record $item
79
     * @param $fields
80
     * @return string
81
     */
82 2
    protected function generateUniqueCondition(Record $item, $fields)
83
    {
84 2
        $conditions = [];
85 2
        foreach ($fields as $field) {
86 2
            $conditions[] = "`$field` = '{$item->{$field}}'";
87
        }
88 2
        return implode(' AND ', $conditions);
89
    }
90
91
    /**
92
     * @return null
93
     */
94 3
    public function getUniqueFields()
95
    {
96 3
        if ($this->uniqueFields === null) {
97 3
            $this->initUniqueFields();
98
        }
99
100 3
        return $this->uniqueFields;
101
    }
102
103
    /**
104
     * @return array|null
105
     */
106 3
    public function initUniqueFields()
107
    {
108 3
        $this->uniqueFields = [];
109 3
        $structure = $this->getTableStructure();
0 ignored issues
show
Bug introduced by
It seems like getTableStructure() 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
        $structure = $this->getTableStructure();
Loading history...
110 3
        foreach ($structure['indexes'] as $name => $index) {
111 3
            if ($index['unique'] && $name != 'PRIMARY') {
112 3
                $this->uniqueFields[$name] = $index['fields'];
113
            }
114
        }
115
116 3
        return $this->uniqueFields;
117
    }
118
}
119