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

HasJoinRelation::joinRelation()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 46

Duplication

Lines 10
Ratio 21.74 %

Importance

Changes 0
Metric Value
dl 10
loc 46
rs 9.1781
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace SimpleCrud\Query\Traits;
5
6
use RuntimeException;
7
use SimpleCrud\Table;
8
9
trait HasJoinRelation
10
{
11
    public function joinRelation(Table $table2): self
12
    {
13
        $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...
14
15
        //Has One
16
        if ($field = $table1->getJoinField($table2)) {
17
            $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...
18
                ->join(
19
                    'LEFT',
20
                    (string) $table2,
21
                    sprintf('%s = %s', $field, $table2->id)
22
                );
23
24
        //Has many
25 View Code Duplication
        } elseif ($field = $table->getJoinField($table1)) {
0 ignored issues
show
Bug introduced by
The variable $table does not exist. Did you mean $table2?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
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...
26
            $this->query
27
                ->join(
28
                    'LEFT',
29
                    (string) $table2,
30
                    sprintf('%s = %s', $field, $table1->id)
31
                );
32
33
        //Has many to many
34
        } elseif ($joinTable = $table1->getJoinTable($table2)) {
35
            $field1 = $joinTable->getJoinField($table1);
36
            $field2 = $joinTable->getJoinField($table2);
37
38
            $this->query
39
                ->join(
40
                    'LEFT',
41
                    (string) $joinTable,
42
                    sprintf('%s = %s', $field1, $table1->id)
43
                )
44
                ->join(
45
                    'LEFT',
46
                    (string) $table2,
47
                    sprintf('%s = %s', $field2, $table2->id)
48
                );
49
        } else {
50
            throw new RuntimeException(
51
                sprintf('The tables %s and %s are not related', $table1, $table2)
52
            );
53
        }
54
55
        return $this;
56
    }
57
}
58