TileTreeValidator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 16.36 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C validate() 9 27 7

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
class TileTreeValidator implements ValidatorInterface
9
{
10
    /**
11
     * @var string[]
12
     */
13
    private $faces;
14
    /**
15
     * @var ValidatorInterface
16
     */
17
    private $faceValidator;
18
19
    /**
20
     * @param string[] $faces
21
     * @param ValidatorInterface $faceValidator
22
     */
23
    public function __construct(
24
        array $faces,
25
        ValidatorInterface $faceValidator = null
26
    ) {
27
        $this->faces = $faces;
28
        $this->faceValidator = $faceValidator;
29
    }
30
31
    /**
32
     * @inheritDoc
33
     *
34
     * @SuppressWarnings(PHPMD.ElseExpression)
35
     */
36
    public function validate($value, array $path = [])
37
    {
38
        if (!is_array($value)) {
39
            throw new ValidationException('Is empty', $path);
40
        }
41
        $missingFaces = $this->faces;
42
        $extraFaces = [];
43
        foreach ($value as $face => $grid) {
44
            if (in_array($face, $this->faces)) {
45
                $missingFaces = array_diff($missingFaces, [$face]);
46
            } else {
47
                $extraFaces[] = $face;
48
            }
49
            if ($this->faceValidator) {
50
                $facePath = array_merge($path, [$face]);
51
                $this->faceValidator->validate($grid, $facePath);
52
            }
53
        }
54 View Code Duplication
        if (!empty($missingFaces)) {
55
            $message = 'Following faces were not found: ' .
56
                implode(', ', $missingFaces);
57
            throw new ValidationException($message);
58
        }
59 View Code Duplication
        if (!empty($extraFaces)) {
60
            $message = 'Following extra faces were found: ' .
61
                implode(', ', $extraFaces);
62
            throw new ValidationException($message);
63
        }
64
    }
65
}
66