Passed
Push — dev ( f1f1e7...329389 )
by Fike
03:26
created

TileTreeValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 16.98 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
C validate() 9 27 7
A __construct() 0 6 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
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
    public function validate($value, array $path = [])
35
    {
36
        if (!is_array($value)) {
37
            throw new ValidationException('Is empty', $path);
38
        }
39
        $missingFaces = $this->faces;
40
        $extraFaces = [];
41
        foreach ($value as $face => $grid) {
42
            if (in_array($face, $this->faces)) {
43
                $missingFaces = array_diff($missingFaces, [$face]);
44
            } else {
45
                $extraFaces[] = $face;
46
            }
47
            if ($this->faceValidator) {
48
                $facePath = array_merge($path, [$face]);
49
                $this->faceValidator->validate($grid, $facePath);
50
            }
51
        }
52 View Code Duplication
        if (!empty($missingFaces)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            $message = 'Following faces were not found: ' .
54
                implode(', ', $missingFaces);
55
            throw new ValidationException($message);
56
        }
57 View Code Duplication
        if (!empty($extraFaces)) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
58
            $message = 'Following extra faces were found: ' .
59
                implode(', ', $extraFaces);
60
            throw new ValidationException($message);
61
        }
62
    }
63
}
64