Completed
Push — master ( 4ac004...470d43 )
by Gabriel
06:41
created

RecordsTrait::generateExistsParams()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 13
cts 15
cp 0.8667
rs 9.2088
c 0
b 0
f 0
cc 5
nc 7
nop 1
crap 5.0592
1
<?php
2
3
namespace Nip\Records\Traits\Unique;
4
5
use Nip\Database\Query\Condition\Condition;
6
use Nip\Records\AbstractModels\Record;
7
8
/**
9
 * Class RecordsTrait
10
 * @package Nip\Records\Traits\Unique
11
 */
12
trait RecordsTrait
13
{
14
    protected $uniqueFields = null;
15
16
    /**
17
     * @param Record $item
18
     * @return bool|false|Record
19
     */
20
    public function exists(Record $item)
21
    {
22
        $params = $this->generateExistsParams($item);
23
24
        if (!$params) {
25
            return false;
26
        }
27
        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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
28
    }
29
30
    /**
31
     * @param Record $item
32
     * @return array|bool
33
     */
34 1
    public function generateExistsParams(Record $item)
35
    {
36 1
        $params = [];
37 1
        $params['where'] = [];
38
39 1
        $uniqueFields = $this->getUniqueFields();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $uniqueFields is correct as $this->getUniqueFields() (which targets 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...
40
41 1
        if (!$uniqueFields) {
42
            return false;
43
        }
44
45 1
        foreach ($uniqueFields as $uniqueName => $fields) {
46 1
            $conditions = [];
47 1
            foreach ($fields as $field) {
48 1
                $conditions[] = "`$field` = '{$item->{$field}}'";
49
            }
50 1
            $params['where'][$uniqueName . '-UNQ'] = implode(' AND ', $conditions);
51
        }
52
53 1
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
54 1
        if ($item->getPrimaryKey()) {
55
            $params['where'][] = ["$pk != ?", $item->getPrimaryKey()];
56
        }
57 1
        return $params;
58
    }
59
60
    /**
61
     * @return null
62
     */
63 2
    public function getUniqueFields()
64
    {
65 2
        if ($this->uniqueFields === null) {
66 2
            $this->initUniqueFields();
67
        }
68
69 2
        return $this->uniqueFields;
70
    }
71
72
    /**
73
     * @return array|null
74
     */
75 2
    public function initUniqueFields()
76
    {
77 2
        $this->uniqueFields = [];
78 2
        $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?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79 2
        foreach ($structure['indexes'] as $name => $index) {
80 2
            if ($index['unique'] && $name != 'PRIMARY') {
81 2
                $this->uniqueFields[$name] = $index['fields'];
82
            }
83
        }
84
85 2
        return $this->uniqueFields;
86
    }
87
}
88