Fail   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 57
c 0
b 0
f 0
wmc 2
lcom 0
cbo 3
rs 10
ccs 14
cts 14
cp 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A execute() 0 12 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Validation\Rules\Generic;
4
5
/**
6
 * Copyright 2015-2020 [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\Errors\ErrorCodes;
22
use Limoncello\Validation\Contracts\Execution\ContextInterface;
23
use Limoncello\Validation\I18n\Messages;
24
use Limoncello\Validation\Rules\ExecuteRule;
25
use function assert;
26
27
/**
28
 * @package Limoncello\Validation
29
 */
30
final class Fail extends ExecuteRule
31
{
32
    /**
33
     * Property key.
34
     */
35
    const PROPERTY_ERROR_CODE = self::PROPERTY_LAST + 1;
36
37
    /**
38
     * Property key.
39
     */
40
    const PROPERTY_ERROR_MESSAGE_TEMPLATE = self::PROPERTY_ERROR_CODE + 1;
41
42
    /**
43
     * Property key.
44
     */
45
    const PROPERTY_ERROR_MESSAGE_PARAMETERS = self::PROPERTY_ERROR_MESSAGE_TEMPLATE + 1;
46
47
    /**
48
     * @param int    $errorCode
49
     * @param string $messageTemplate
50
     * @param array  $messageParams
51
     */
52 17
    public function __construct(
53
        int $errorCode = ErrorCodes::INVALID_VALUE,
54
        string $messageTemplate = Messages::INVALID_VALUE,
55
        array $messageParams = []
56
    ) {
57 17
        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...
58
59 17
        parent::__construct([
60 17
            self::PROPERTY_ERROR_CODE               => $errorCode,
61 17
            self::PROPERTY_ERROR_MESSAGE_TEMPLATE   => $messageTemplate,
62 17
            self::PROPERTY_ERROR_MESSAGE_PARAMETERS => $messageParams,
63
        ]);
64
    }
65
66
    /**
67
     * @param mixed            $value
68
     * @param ContextInterface $context
69
     *
70
     * @return array
71
     *
72
     * @SuppressWarnings(PHPMD.StaticAccess)
73
     */
74 6
    public static function execute($value, ContextInterface $context): array
75
    {
76 6
        $properties = $context->getProperties();
77
78 6
        return static::createErrorReply(
79 6
            $context,
80 6
            $value,
81 6
            $properties->getProperty(self::PROPERTY_ERROR_CODE),
82 6
            $properties->getProperty(self::PROPERTY_ERROR_MESSAGE_TEMPLATE),
83 6
            $properties->getProperty(self::PROPERTY_ERROR_MESSAGE_PARAMETERS)
84
        );
85
    }
86
}
87