Completed
Push — master ( b843f5...1528d8 )
by Neomerx
03:51
created

BaseTwoValueComparision   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 147
Duplicated Lines 19.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 29
loc 147
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6
rs 10
ccs 34
cts 34
cp 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A toBlock() 19 19 2
A getLowerValue() 0 4 1
A getUpperValue() 0 4 1
A getErrorCode() 0 4 1
A getMessageTemplate() 0 4 1
A getMessageParameters() 0 4 1
A readLowerValue() 0 6 1
A readUpperValue() 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 declare(strict_types=1);
2
3
namespace Limoncello\Validation\Rules\Comparisons;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
22
use Limoncello\Validation\Contracts\Execution\ContextInterface;
23
use Limoncello\Validation\Contracts\Rules\ComparisionInterface;
24
use Limoncello\Validation\Rules\BaseRule;
25
use Limoncello\Validation\Rules\Generic\Fail;
26
use Limoncello\Validation\Rules\Generic\IfOperator;
27
use Limoncello\Validation\Rules\Generic\Success;
28
29
/**
30
 * @package Limoncello\Validation
31
 */
32
abstract class BaseTwoValueComparision extends BaseRule implements ComparisionInterface
33
{
34
    /**
35
     * Property key.
36
     */
37
    const PROPERTY_LOWER_VALUE = self::PROPERTY_LAST + 1;
38
39
    /**
40
     * Property key.
41
     */
42
    const PROPERTY_UPPER_VALUE = self::PROPERTY_LOWER_VALUE + 1;
43
44
    /**
45
     * Property key.
46
     */
47
    const PROPERTY_TWO_VALUE_LAST = self::PROPERTY_UPPER_VALUE;
48
49
    /**
50
     * @var mixed
51
     */
52
    private $lowerValue;
53
54
    /**
55
     * @var mixed
56
     */
57
    private $upperValue;
58
59
    /**
60
     * @var int
61
     */
62
    private $errorCode;
63
64
    /**
65
     * @var string
66
     */
67
    private $messageTemplate;
68
69
    /**
70
     * @var array
71
     */
72
    private $messageParams;
73
74
    /**
75
     * @param mixed  $lowerValue
76
     * @param mixed  $upperValue
77
     * @param int    $errorCode
78
     * @param string $messageTemplate
79
     * @param array  $messageParams
80
     */
81 5 View Code Duplication
    public function __construct($lowerValue, $upperValue, int $errorCode, string $messageTemplate, array $messageParams)
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...
82
    {
83 5
        assert($this->checkEachValueConvertibleToString($messageParams));
0 ignored issues
show
Documentation introduced by
$messageParams is of type array, but the function expects a object<Limoncello\Validation\Rules\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
85 5
        $this->lowerValue      = $lowerValue;
86 5
        $this->upperValue      = $upperValue;
87 5
        $this->errorCode       = $errorCode;
88 5
        $this->messageTemplate = $messageTemplate;
89 5
        $this->messageParams   = $messageParams;
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 5 View Code Duplication
    public function toBlock(): ExecutionBlockInterface
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...
96
    {
97 5
        $operator = new IfOperator(
98 5
            [static::class, 'compare'],
99 5
            new Success(),
100 5
            new Fail($this->getErrorCode(), $this->getMessageTemplate(), $this->getMessageParameters()),
101
            [
102 5
                static::PROPERTY_LOWER_VALUE => $this->getLowerValue(),
103 5
                static::PROPERTY_UPPER_VALUE => $this->getUpperValue(),
104
            ]
105
        );
106
107 5
        $operator->setParent($this);
108 5
        if ($this->isCaptureEnabled() === true) {
109 1
            $operator->enableCapture();
110
        }
111
112 5
        return $operator->toBlock();
113
    }
114
115
    /**
116
     * @return mixed
117
     */
118 5
    public function getLowerValue()
119
    {
120 5
        return $this->lowerValue;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126 5
    public function getUpperValue()
127
    {
128 5
        return $this->upperValue;
129
    }
130
131
    /**
132
     * @return int
133
     */
134 5
    protected function getErrorCode(): int
135
    {
136 5
        return $this->errorCode;
137
    }
138
139
    /**
140
     * @return string
141
     */
142 5
    protected function getMessageTemplate(): string
143
    {
144 5
        return $this->messageTemplate;
145
    }
146
147
    /**
148
     * @return array
149
     */
150 5
    public function getMessageParameters(): array
151
    {
152 5
        return $this->messageParams;
153
    }
154
155
    /**
156
     * @param ContextInterface $context
157
     *
158
     * @return mixed
159
     */
160 4
    protected static function readLowerValue(ContextInterface $context)
161
    {
162 4
        $limit = $context->getProperties()->getProperty(static::PROPERTY_LOWER_VALUE);
163
164 4
        return $limit;
165
    }
166
167
    /**
168
     * @param ContextInterface $context
169
     *
170
     * @return mixed
171
     */
172 4
    protected static function readUpperValue(ContextInterface $context)
173
    {
174 4
        $limit = $context->getProperties()->getProperty(static::PROPERTY_UPPER_VALUE);
175
176 4
        return $limit;
177
    }
178
}
179