Completed
Push — master ( 96468a...a7f65d )
by Philip
02:55
created

AbstractParametrizedValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 0
cbo 1
dl 6
loc 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateParameterCount() 3 5 2
A validateMinParameterCount() 3 5 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
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Valdi\Validator;
13
14
use Valdi\ValidationException;
15
16
/**
17
 * Validator for parametrized validators.
18
 */
19
abstract class AbstractParametrizedValidator implements ValidatorInterface {
20
21
    /**
22
     * Throws an exception if the parameters don't fullfill the expected
23
     * parameter count.
24
     *
25
     * @param string $name
26
     * the name of the validator
27
     * @param integer $parameterAmount
28
     * the amount of expected parameters
29
     * @param string[] $parameters
30
     * the parameters
31
     */
32
    protected function validateParameterCount($name, $parameterAmount, array $parameters) {
33 View Code Duplication
        if (count($parameters) !== $parameterAmount) {
0 ignored issues
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...
34
            throw new ValidationException('"' . $name . '" expects ' . $parameterAmount . ' parameter.');
35
        }
36
    }
37
38
    /**
39
     * Throws an exception if the parameters don't fullfill the expected
40
     * minimum parameter count.
41
     *
42
     * @param string $name
43
     * the name of the validator
44
     * @param integer $parameterAmount
45
     * the amount of expected parameters
46
     * @param string[] $parameters
47
     * the parameters
48
     */
49
    protected function validateMinParameterCount($name, $parameterAmount, array $parameters) {
50 View Code Duplication
        if (count($parameters) < $parameterAmount) {
0 ignored issues
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...
51
            throw new ValidationException('"' . $name . '" expects at least ' . $parameterAmount . ' parameter.');
52
        }
53
    }
54
}
55