Completed
Push — master ( 68efd7...e19da4 )
by Billie
09:36
created

ValidationServiceImplementation   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 44
loc 44
wmc 4
lcom 1
cbo 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A validate() 15 15 2

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
declare(strict_types = 1);
3
namespace PurpleBooth\GitGithubLint;
4
5
use PurpleBooth\GitGithubLint\Status\Status;
6
use PurpleBooth\GitGithubLint\Validator\Validator;
7
8
/**
9
 * Validate against a number of validators against a message and return the most high priority status for that message
10
 *
11
 * @package PurpleBooth\GitGithubLint
12
 */
13 View Code Duplication
class ValidationServiceImplementation implements ValidationService
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...
14
{
15
    /**
16
     * @var array
17
     */
18
    private $validators;
19
20
    /**
21
     * ValidationServiceImplementation constructor.
22
     *
23
     * @param array $validators
24
     */
25
    public function __construct(array $validators)
26
    {
27
        $this->validators = $validators;
28
29
        if (count($validators) < 1) {
30
            new \LogicException("You need to provide the validation service with at least 1 validator");
31
        }
32
    }
33
34
    /**
35
     * Test a message against a number of validators
36
     *
37
     * @param Message $message
38
     *
39
     * @return Status
40
     */
41
    public function validate(Message $message) : Status
42
    {
43
        $statuses = [];
44
45
        /** @var Validator $validator */
46
        foreach ($this->validators as $validator) {
47
            $statuses[] = $validator->validate($message);
48
        }
49
50
        usort($statuses, function (Status $statusA, Status $statusB) {
51
            return $statusA->getWeight() <=> $statusB->getWeight();
52
        });
53
54
        return array_pop($statuses);
55
    }
56
}
57