Completed
Push — master ( b7ef6a...58a002 )
by Kirill
03:12
created

ModeProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 76
Duplicated Lines 26.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 26.92%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 20
loc 76
ccs 7
cts 26
cp 0.2692
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A or() 10 10 2
A and() 10 10 2
B createGroup() 0 20 7
A mode() 0 6 1

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
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Query;
11
12
use RDS\Hydrogen\Criteria\CriterionInterface;
13
use RDS\Hydrogen\Criteria\Having;
14
use RDS\Hydrogen\Criteria\HavingGroup;
15
use RDS\Hydrogen\Criteria\Where;
16
use RDS\Hydrogen\Criteria\WhereGroup;
17
use RDS\Hydrogen\Query;
18
19
/**
20
 * Trait ModeProvider
21
 * @property-read Query|$this $or
22
 * @property-read Query|$this $and
23
 * @mixin Query
24
 */
25
trait ModeProvider
26
{
27
    /**
28
     * - AND if true
29
     * - OR if false
30
     *
31
     * @var bool
32
     */
33
    protected $conjunction = true;
34
35
    /**
36
     * @param \Closure|null $group
37
     * @return Query|$this|self
38
     */
39 19 View Code Duplication
    public function or(\Closure $group = null): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
40
    {
41 19
        $this->conjunction = false;
42
43 19
        if ($group !== null) {
44
            $this->add($this->createGroup(__FUNCTION__, $group));
0 ignored issues
show
Bug introduced by
It seems like add() 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...
45
        }
46
47 19
        return $this;
48
    }
49
50
    /**
51
     * @param \Closure|null $group
52
     * @return Query|$this|self
53
     */
54 View Code Duplication
    public function and(\Closure $group = null): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
55
    {
56
        $this->conjunction = true;
57
58
        if ($group !== null) {
59
            $this->add($this->createGroup(__FUNCTION__, $group));
0 ignored issues
show
Bug introduced by
It seems like add() 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...
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * @param string $fn
67
     * @param \Closure $group
68
     * @return CriterionInterface
69
     */
70
    private function createGroup(string $fn, \Closure $group): CriterionInterface
71
    {
72
        $latest = $this->criteria->count()
0 ignored issues
show
Bug introduced by
The property criteria 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...
73
            ? \get_class($this->criteria->top())
74
            : null;
75
76
        switch($latest) {
77
            case Where::class:
78
            case WhereGroup::class:
79
                return new WhereGroup($this, $group, $this->mode());
80
81
            case Having::class:
82
            case HavingGroup::class:
83
                return new HavingGroup($this, $group, $this->mode());
84
        }
85
86
        $error = 'Operator "%s" can be added only after Where or Having clauses, but %s given';
87
        $given = $latest ? \class_basename($latest) : 'none';
88
        throw new \LogicException(\sprintf($error, \strtoupper($fn), $given));
89
    }
90
91
    /**
92
     * @return bool
93
     */
94
    protected function mode(): bool
95
    {
96 43
        return \tap($this->conjunction, function () {
97 43
            $this->conjunction = true;
98 43
        });
99
    }
100
}
101