Completed
Branch 7-dev (bf2895)
by Oscar
03:53
created

HasRelatedWith   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 109
Duplicated Lines 10.09 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 11
loc 109
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A relatedWith() 0 14 4
A applyTableRelation() 11 42 4
B applyRowRelation() 0 44 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Query\Traits;
5
6
use InvalidArgumentException;
7
use RuntimeException;
8
use SimpleCrud\Row;
9
use SimpleCrud\RowCollection;
10
use SimpleCrud\Table;
11
12
trait HasRelatedWith
13
{
14
    /**
15
     * @param Row|RowCollection|Table
16
     * @param mixed $related
17
     */
18
    public function relatedWith($related): self
19
    {
20
        if ($related instanceof Row || $related instanceof RowCollection) {
21
            return $this->applyRowRelation($related);
22
        }
23
24
        if ($related instanceof Table) {
25
            return $this->applyTableRelation($related);
26
        }
27
28
        throw new InvalidArgumentException(
29
            'Invalid argument type. Only instances of Row, Table and RowCollection are allowed'
30
        );
31
    }
32
33
    private function applyTableRelation(Table $table2): self
34
    {
35
        $table1 = $this->table;
0 ignored issues
show
Bug introduced by
The property table does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
37
        //Has one
38
        if ($field = $table1->getJoinField($table2)) {
39
            return $this->where("{$field} IS NOT NULL");
0 ignored issues
show
Bug introduced by
It seems like where() 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...
40
        }
41
42
        //Has many
43 View Code Duplication
        if ($field = $table2->getJoinField($table1)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
            $this->query
0 ignored issues
show
Bug introduced by
The property query does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
                ->join(
46
                    'LEFT',
47
                    (string) $table2,
48
                    sprintf('%s = %s', $field, $table1->id)
49
                )
50
                ->where(sprintf('%s IS NOT NULL', $field));
51
52
            return $this;
53
        }
54
55
        //Has many to many
56
        if ($joinTable = $table1->getJoinTable($table2)) {
57
            $field1 = $joinTable->getJoinField($table1);
58
            $field2 = $joinTable->getJoinField($table2);
59
60
            $this->query
61
                ->join(
62
                    'LEFT',
63
                    (string) $joinTable,
64
                    sprintf('%s = %s', $field1, $table1->id)
65
                )
66
                ->where(sprintf('%s IS NOT NULL', $field2));
67
68
            return $this;
69
        }
70
71
        throw new RuntimeException(
72
            sprintf('The tables %s and %s are not related', $table1, $table2)
73
        );
74
    }
75
76
    private function applyRowRelation($row): self
77
    {
78
        $table1 = $this->table;
79
        $table2 = $row->getTable();
80
81
        //Has one
82
        if ($field = $table1->getJoinField($table2)) {
83
            $this->query->whereEquals([(string) $field => $row->id ?: null]);
84
            return $this;
85
        }
86
87
        //Has many
88
        if ($field = $table2->getJoinField($table1)) {
89
            $this->query
90
                ->join(
91
                    'LEFT',
92
                    (string) $table2,
93
                    sprintf('%s = %s', $field, $table1->id)
94
                );
95
96
            $this->query->whereEquals([(string) $table2->id => $row->id ?: null]);
97
            return $this;
98
        }
99
100
        //Has many to many
101
        if ($joinTable = $table1->getJoinTable($table2)) {
102
            $field1 = $joinTable->getJoinField($table1);
103
            $field2 = $joinTable->getJoinField($table2);
104
105
            $this->query
106
                ->join(
107
                    'LEFT',
108
                    (string) $joinTable,
109
                    sprintf('%s = %s', $field1, $table1->id)
110
                );
111
112
            $this->query->whereEquals([(string) $field2 => $row->id ?: null]);
113
            return $this;
114
        }
115
116
        throw new RuntimeException(
117
            sprintf('The tables %s and %s are not related', $table1, $table2)
118
        );
119
    }
120
}
121