TileFaceValidator   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
wmc 9
dl 52
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 17 17 8
A __construct() 8 8 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 AmaTeam\Image\Projection\Framework\Validation\Tile;
4
5
use AmaTeam\Image\Projection\Framework\Validation\ValidationException;
6
use AmaTeam\Image\Projection\Framework\Validation\ValidatorInterface;
7
8 View Code Duplication
class TileFaceValidator implements ValidatorInterface
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...
9
{
10
    /**
11
     * @var int|null
12
     */
13
    private $height;
14
    /**
15
     * @var bool
16
     */
17
    private $allowedEmpty = false;
18
    /**
19
     * @var ValidatorInterface|null
20
     */
21
    private $rowValidator = null;
22
23
    /**
24
     * @param int|null $height
25
     * @param bool $allowedEmpty
26
     * @param ValidatorInterface|null $rowValidator
27
     *
28
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
29
     */
30
    public function __construct(
31
        $height = null,
32
        $allowedEmpty = false,
33
        $rowValidator = null
34
    ) {
35
        $this->height = $height;
36
        $this->allowedEmpty = $allowedEmpty;
37
        $this->rowValidator = $rowValidator;
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function validate($value, array $path = [])
44
    {
45
        if (!is_array($value)) {
46
            throw new ValidationException('Is not an array', $path);
47
        }
48
        if (!$this->allowedEmpty && empty($value)) {
49
            throw new ValidationException('Is empty', $path);
50
        }
51
        if (is_int($this->height) && sizeof($value) !== $this->height) {
52
            $template = 'Height is %d (%d expected)';
53
            $message = sprintf($template, sizeof($value), $this->height);
54
            throw new ValidationException($message, $path);
55
        }
56
        if ($this->rowValidator) {
57
            foreach ($value as $y => $row) {
58
                $rowPath = array_merge($path, [$y]);
59
                $this->rowValidator->validate($row, $rowPath);
60
            }
61
        }
62
    }
63
}
64