Completed
Push — master ( 0eeb04...4002ae )
by Oscar
02:31
created

ExtendedSelectionTrait   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 96
Duplicated Lines 3.13 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 11
c 5
b 1
f 0
lcom 1
cbo 4
dl 3
loc 96
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A from() 0 6 1
A field() 0 6 1
B relatedWith() 3 33 5
A fieldsToString() 0 4 2
A fromToString() 0 4 2

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
3
namespace SimpleCrud\Queries\Mysql;
4
5
use SimpleCrud\AbstractRow;
6
use SimpleCrud\SimpleCrudException;
7
use SimpleCrud\Scheme\Scheme;
8
9
/**
10
 * Extended trait.
11
 *
12
 * @property \SimpleCrud\Table $table
13
 */
14
trait ExtendedSelectionTrait
15
{
16
    use SelectionTrait;
17
18
    protected $from = [];
19
    protected $fields = [];
20
21
    /**
22
     * Adds new extra table to the query.
23
     *
24
     * @param string $table
25
     *
26
     * @return self
27
     */
28
    public function from($table)
29
    {
30
        $this->from[] = $table;
31
32
        return $this;
33
    }
34
35
    /**
36
     * Adds a new extra field to the query.
37
     *
38
     * @param string $field
39
     *
40
     * @return self
41
     */
42
    public function field($field)
43
    {
44
        $this->fields[] = $field;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Adds a WHERE according with the relation of other table.
51
     *
52
     * @param AbstractRow $row
53
     *
54
     * @return self
55
     */
56
    public function relatedWith(AbstractRow $row)
57
    {
58
        $table = $row->getTable();
59
        $scheme = $this->table->getScheme();
60
61 View Code Duplication
        if (!isset($scheme['relations'][$table->name])) {
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...
62
            throw new SimpleCrudException(sprintf('The tables %s and %s are no related', $table->name, $this->table->name));
63
        }
64
65
        $relation = $scheme['relations'][$table->name];
66
67
        switch ($relation[0]) {
68
            case Scheme::HAS_ONE:
69
                return $this->by($relation[1], $row->id);
70
71
            case Scheme::HAS_MANY:
72
                return $this->byId($relation[1], $row->{$relation[1]});
0 ignored issues
show
Unused Code introduced by
The call to ExtendedSelectionTrait::byId() has too many arguments starting with $row->{$relation[1]}.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
74
            case Scheme::HAS_MANY_TO_MANY:
75
                $this->from($relation[1]);
76
                $this->from($table->name);
77
78
                $this->fields[] = sprintf('`%s`.`%s`', $relation[1], $relation[3]);
79
                $this->where(sprintf('`%s`.`%s` = `%s`.`id`', $relation[1], $relation[2], $this->table->name));
80
                $this->where(sprintf('`%s`.`%s` = `%s`.`id`', $relation[1], $relation[3], $table->name));
81
                $this->where(sprintf('`%s`.`id` IN (:%s)', $table->name, $relation[3]), [':'.$relation[3] => $row->id]);
82
83
                return $this;
84
85
            default:
86
                throw new SimpleCrudException(sprintf('Invalid relation type between %s and %s', $table->name, $this->table->name));
87
        }
88
    }
89
90
    /**
91
     * add extra fields to the code.
92
     *
93
     * @return string
94
     */
95
    protected function fieldsToString($prepend = ', ')
96
    {
97
        return $this->fields ? $prepend.implode(', ', $this->fields) : '';
98
    }
99
100
    /**
101
     * add extra fields to the code.
102
     *
103
     * @return string
104
     */
105
    protected function fromToString($prepend = ', ')
106
    {
107
        return $this->from ? $prepend.'`'.implode('`, `', $this->from).'`' : '';
108
    }
109
}
110