Blank   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 0
dl 0
loc 67
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B apply() 0 16 11
A required() 0 4 1
A notEmpty() 0 4 1
A notEmptyList() 0 4 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * 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, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2018 LibreWorks contributors
19
 * @license   Apache-2.0
20
 */
21
namespace Caridea\Validate\Rule;
22
23
/**
24
 * Rules for empty values
25
 *
26
 * @copyright 2015-2018 LibreWorks contributors
27
 * @license   Apache-2.0
28
 */
29
class Blank implements \Caridea\Validate\Rule
30
{
31
    /**
32
     * @var string The operator type
33
     */
34
    private $operator;
35
36
    /**
37
     * Creates a new EmptyRule.
38
     *
39
     * @param string $operator The operator type
40
     */
41 3
    protected function __construct(string $operator)
42
    {
43 3
        $this->operator = $operator;
44 3
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 3
    public function apply($value, $data = []): ?array
50
    {
51 3
        switch ($this->operator) {
52 3
            case "required":
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
53 1
                return $value === null || $value === '' ? ['REQUIRED'] : null;
54 2
            case "empty":
55 1
                return $value === '' ? ['CANNOT_BE_EMPTY'] : null;
56 1
            case "list":
57 1
                if (empty($value)) {
58 1
                    return ['CANNOT_BE_EMPTY'];
59 1
                } elseif (!is_array($value) && !($value instanceof \Countable)) {
60 1
                    return ['FORMAT_ERROR'];
61
                }
62 1
                return count($value) === 0 ? ['CANNOT_BE_EMPTY'] : null;
63
        }
64
    }
65
66
    /**
67
     * Gets a rule that requires values to be non-null and not empty string.
68
     *
69
     * @return \Caridea\Validate\Rule\Blank the created rule
70
     */
71 1
    public static function required(): Blank
72
    {
73 1
        return new Blank('required');
74
    }
75
76
    /**
77
     * Gets a rule that requires strings to be non-empty.
78
     *
79
     * @return \Caridea\Validate\Rule\Blank
80
     */
81 1
    public static function notEmpty(): Blank
82
    {
83 1
        return new Blank('empty');
84
    }
85
86
    /**
87
     * Gets a rule that requires an array or `Countable` to be non-empty.
88
     *
89
     * @return \Caridea\Validate\Rule\Blank
90
     */
91 1
    public static function notEmptyList(): Blank
92
    {
93 1
        return new Blank('list');
94
    }
95
}
96