IntegerValidatorTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 12.9 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 73.68%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 8
loc 62
ccs 14
cts 19
cp 0.7368
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B mustBeInteger() 0 35 6
A mustBeIntegerOrEmpty() 8 8 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
namespace Imedia\Ammit\UI\Resolver\Validator\Implementation\Pure;
4
5
use Imedia\Ammit\UI\Resolver\Validator\InvalidArgumentException;
6
use Imedia\Ammit\UI\Resolver\UIValidationEngine;
7
use Imedia\Ammit\UI\Resolver\Validator\UIValidatorInterface;
8
9
/**
10
 * @author Guillaume MOREL <[email protected]>
11
 */
12
trait IntegerValidatorTrait
13
{
14
    /** @var UIValidationEngine */
15
    protected $validationEngine;
16
17
    /**
18
     * Exceptions are caught in order to be processed later
19
     * @param mixed $value Integer ?
20
     *
21
     * @return int|null Value casted into int or -1 or null
22
     */
23 View Code Duplication
    public function mustBeIntegerOrEmpty($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null)
0 ignored issues
show
Duplication introduced by
This method 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...
24
    {
25
        if ($value === '') {
26
            return null;
27
        }
28
29
        return $this->mustBeInteger($value, $propertyPath, $parentValidator, $exceptionMessage);
30
    }
31
32
    /**
33
     * Exceptions are caught in order to be processed later
34
     * @param mixed $value Integer ?
35
     *
36
     * @return int Value casted into int or -1
37
     */
38
    public function mustBeInteger($value, string $propertyPath = null, UIValidatorInterface $parentValidator = null, string $exceptionMessage = null): int
39
    {
40 1
        if (is_numeric($value)) {
41 1
            $value = (int) $value;
42
        }
43
44 1
        $this->validationEngine->validateFieldValue(
45 1
            $parentValidator ?: $this,
46 1
            function() use ($value, $propertyPath, $exceptionMessage) {
47 1
                if (is_int($value)) {
48 1
                    return;
49
                }
50
51 1
                if (null === $exceptionMessage) {
52
                    $exceptionMessage = sprintf(
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $exceptionMessage, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
53
                        'Value "%s" is not an integer.',
54
                        $value
55
                    );
56
                }
57
58 1
                throw new InvalidArgumentException(
59
                    $exceptionMessage,
60 1
                    0,
61
                    $propertyPath,
62
                    $value
63
                );
64 1
            }
65
        );
66
67 1
        if (!is_int($value)) {
68 1
            return -1;
69
        }
70
71 1
        return $value;
72
    }
73
}
74