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

BaseOneValueComparision::getMessageParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 0
crap 1
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
use function assert;
29
30
/**
31
 * @package Limoncello\Validation
32
 *
33
 * @SuppressWarnings(PHPMD.NumberOfChildren)
34
 */
35
abstract class BaseOneValueComparision extends BaseRule implements ComparisionInterface
36
{
37
    /**
38
     * Property key.
39
     */
40
    const PROPERTY_VALUE = self::PROPERTY_LAST + 1;
41
42
    /**
43
     * Property key.
44
     */
45
    const PROPERTY_ONE_VALUE_LAST = self::PROPERTY_VALUE;
46
47
    /**
48
     * @var mixed
49
     */
50
    private $value;
51
52
    /**
53
     * @var int
54
     */
55
    private $errorCode;
56
57
    /**
58
     * @var string
59
     */
60
    private $messageTemplate;
61
62
    /**
63
     * @var array
64
     */
65
    private $messageParams;
66
67
    /**
68
     * @param mixed  $value
69
     * @param int    $errorCode
70
     * @param string $messageTemplate
71
     * @param array  $messageParams
72
     */
73 6 View Code Duplication
    public function __construct($value, 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...
74
    {
75 6
        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...
76
77 6
        $this->value           = $value;
78 6
        $this->errorCode       = $errorCode;
79 6
        $this->messageTemplate = $messageTemplate;
80 6
        $this->messageParams   = $messageParams;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 6 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...
87
    {
88 6
        $operator = new IfOperator(
89 6
            [static::class, 'compare'],
90 6
            new Success(),
91 6
            new Fail($this->getErrorCode(), $this->getMessageTemplate(), $this->getMessageParameters()),
92 6
            [static::PROPERTY_VALUE => $this->getValue()]
93
        );
94
95 6
        $operator->setParent($this);
96 6
        if ($this->isCaptureEnabled() === true) {
97 2
            $operator->enableCapture();
98
        }
99
100 6
        return $operator->toBlock();
101
    }
102
103
    /**
104
     * @return mixed
105
     */
106 6
    public function getValue()
107
    {
108 6
        return $this->value;
109
    }
110
111
    /**
112
     * @return int
113
     */
114 6
    protected function getErrorCode(): int
115
    {
116 6
        return $this->errorCode;
117
    }
118
119
    /**
120
     * @return string
121
     */
122 6
    protected function getMessageTemplate(): string
123
    {
124 6
        return $this->messageTemplate;
125
    }
126
127
    /**
128
     * @return mixed
129
     */
130 6
    public function getMessageParameters(): array
131
    {
132 6
        return $this->messageParams;
133
    }
134
135
    /**
136
     * @param ContextInterface $context
137
     *
138
     * @return mixed
139
     */
140 5
    protected static function readValue(ContextInterface $context)
141
    {
142 5
        $limit = $context->getProperties()->getProperty(static::PROPERTY_VALUE);
143
144 5
        return $limit;
145
    }
146
}
147