Completed
Push — master ( 22fd50...798002 )
by Neomerx
03:53
created

BaseTwoValueComparision   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 125
Duplicated Lines 15.2 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 19
loc 125
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A toBlock() 19 19 2
A getLowerValue() 0 4 1
A getUpperValue() 0 4 1
A getErrorCode() 0 4 1
A getErrorContext() 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 namespace Limoncello\Validation\Rules\Comparisons;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Validation\Contracts\Blocks\ExecutionBlockInterface;
20
use Limoncello\Validation\Contracts\Execution\ContextInterface;
21
use Limoncello\Validation\Contracts\Rules\ComparisionInterface;
22
use Limoncello\Validation\Rules\BaseRule;
23
use Limoncello\Validation\Rules\Generic\Fail;
24
use Limoncello\Validation\Rules\Generic\IfOperator;
25
use Limoncello\Validation\Rules\Generic\Success;
26
27
/**
28
 * @package Limoncello\Validation
29
 */
30
abstract class BaseTwoValueComparision extends BaseRule implements ComparisionInterface
31
{
32
    /**
33
     * Property key.
34
     */
35
    const PROPERTY_LOWER_VALUE = self::PROPERTY_LAST + 1;
36
37
    /**
38
     * Property key.
39
     */
40
    const PROPERTY_UPPER_VALUE = self::PROPERTY_LOWER_VALUE + 1;
41
42
    /**
43
     * @var mixed
44
     */
45
    private $lowerValue;
46
47
    /**
48
     * @var mixed
49
     */
50
    private $upperValue;
51
52
    /**
53
     * @var int
54
     */
55
    private $errorCode;
56
57
    /**
58
     * @var mixed
59
     */
60
    private $errorContext;
61
62
    /**
63
     * @param mixed $lowerValue
64
     * @param mixed $upperValue
65
     * @param int   $errorCode
66
     * @param mixed $errorContext
67
     */
68
    public function __construct($lowerValue, $upperValue, int $errorCode, $errorContext = null)
69
    {
70
        $this->lowerValue   = $lowerValue;
71
        $this->upperValue   = $upperValue;
72
        $this->errorCode    = $errorCode;
73
        $this->errorContext = $errorContext;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 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...
80
    {
81
        $operator = new IfOperator(
82
            [static::class, 'compare'],
83
            new Success(),
84
            new Fail($this->getErrorCode(), $this->getErrorContext()),
85
            [
86
                static::PROPERTY_LOWER_VALUE => $this->getLowerValue(),
87
                static::PROPERTY_UPPER_VALUE => $this->getUpperValue(),
88
            ]
89
        );
90
91
        $operator->setParent($this);
92
        if ($this->isCaptureEnabled() === true) {
93
            $operator->enableCapture();
94
        }
95
96
        return $operator->toBlock();
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getLowerValue()
103
    {
104
        return $this->lowerValue;
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110
    public function getUpperValue()
111
    {
112
        return $this->upperValue;
113
    }
114
115
    /**
116
     * @return int
117
     */
118
    protected function getErrorCode(): int
119
    {
120
        return $this->errorCode;
121
    }
122
123
    /**
124
     * @return mixed
125
     */
126
    public function getErrorContext()
127
    {
128
        return $this->errorContext;
129
    }
130
131
    /**
132
     * @param ContextInterface $context
133
     *
134
     * @return mixed
135
     */
136
    protected static function readLowerValue(ContextInterface $context)
137
    {
138
        $limit = $context->getProperties()->getProperty(static::PROPERTY_LOWER_VALUE);
139
140
        return $limit;
141
    }
142
143
    /**
144
     * @param ContextInterface $context
145
     *
146
     * @return mixed
147
     */
148
    protected static function readUpperValue(ContextInterface $context)
149
    {
150
        $limit = $context->getProperties()->getProperty(static::PROPERTY_UPPER_VALUE);
151
152
        return $limit;
153
    }
154
}
155