ForbiddenConstraint   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 0
dl 53
loc 53
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 4 4 1
A check() 3 3 1
A getMessage() 10 10 2
A getOptions() 5 5 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
2
3
namespace Weew\Validator\Constraints;
4
5
use Weew\Validator\IConstraint;
6
use Weew\Validator\IValidationData;
7
8
/**
9
 * Check if the value is not in the list of forbidden values.
10
 */
11 View Code Duplication
class ForbiddenConstraint implements IConstraint {
0 ignored issues
show
Duplication introduced by
This class 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...
12
    /**
13
     * @var array
14
     */
15
    protected $forbidden;
16
17
    /**
18
     * @var string
19
     */
20
    protected $message;
21
22
    /**
23
     * @param array $forbidden
24
     * @param string $message
25
     */
26
    public function __construct(array $forbidden, $message = null) {
27
        $this->forbidden = $forbidden;
28
        $this->message = $message;
29
    }
30
31
    /**
32
     * @param $value
33
     * @param IValidationData $data
34
     *
35
     * @return bool
36
     */
37
    public function check($value, IValidationData $data = null) {
38
        return ! array_contains($this->forbidden, $value);
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    public function getMessage() {
45
        if ($this->message !== null) {
46
            return $this->message;
47
        }
48
49
        return s(
50
            'Invalid value, forbidden values are "%s".',
51
            implode(', ', $this->forbidden)
52
        );
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getOptions() {
59
        return [
60
            'forbidden' => $this->forbidden,
61
        ];
62
    }
63
}
64