Test Failed
Pull Request — master (#2)
by
unknown
05:23
created

Validator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.11%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 16
loc 76
ccs 31
cts 36
cp 0.8611
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B validate() 16 57 9

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
declare(strict_types=1);
4
5
namespace Lamoda\GS1Parser\Validator;
6
7
use Lamoda\GS1Parser\Exception\InvalidBarcodeException;
8
use Lamoda\GS1Parser\Parser\ParserInterface;
9
10
final class Validator implements ValidatorInterface
11
{
12
    /**
13
     * @var ParserInterface
14
     */
15
    private $parser;
16
    /**
17
     * @var ValidatorConfig
18
     */
19
    private $config;
20
21 8
    public function __construct(ParserInterface $parser, ValidatorConfig $config)
22
    {
23 8
        $this->parser = $parser;
24 8
        $this->config = $config;
25 8
    }
26
27 8
    public function validate($value): Resolution
28
    {
29 8
        if ($value === null && $this->config->isAllowEmpty()) {
30 1
            return Resolution::createValid();
31
        }
32
33 7
        if (!is_string($value)) {
34 2
            return Resolution::createInvalid([
35 2
                ErrorCodes::VALUE_IS_NOT_STRING => 'Value is not a string',
36
            ]);
37
        }
38
39 5
        $trimmedValue = trim($value);
40
41 5
        if ($trimmedValue === '') {
42 2
            return $this->config->isAllowEmpty() ?
43 1
                Resolution::createValid() :
44 1
                Resolution::createInvalid([
45 2
                    ErrorCodes::VALUE_EMPTY => 'Value is empty',
46
                ]);
47
        }
48
49
        try {
50 3
            $barcode = $this->parser->parse($trimmedValue);
51
        } catch (InvalidBarcodeException $exception) {
0 ignored issues
show
Bug introduced by
The class Lamoda\GS1Parser\Exception\InvalidBarcodeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
52
            return Resolution::createInvalid([
53
                ErrorCodes::INVALID_VALUE => sprintf(
54
                    'Value is invalid: %s',
55
                    $exception->getCode()
56
                ),
57
            ]);
58
        }
59
60 3
        $ais = array_keys($barcode->ais());
61 3
        $requiredAis = array_diff($this->config->getRequiredAIs(), $ais);
62 3
        $forbiddenAIs = array_intersect($this->config->getForbiddenAIs(), $ais);
63
64 3 View Code Duplication
        if (count($requiredAis) > 0) {
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...
65 1
            return Resolution::createInvalid([
66 1
                ErrorCodes::MISSING_AIS => sprintf(
67 1
                    'AIs are missing: "%s"',
68 1
                    implode('", "', $requiredAis)
69
                ),
70
            ]);
71
        }
72
73 2 View Code Duplication
        if (count($forbiddenAIs) > 0) {
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...
74 1
            return Resolution::createInvalid([
75 1
                ErrorCodes::FORBIDDEN_AIS => sprintf(
76 1
                    'AIs are forbidden: "%s"',
77 1
                    implode('", "', $forbiddenAIs)
78
                ),
79
            ]);
80
        }
81
82 1
        return Resolution::createValid();
83
    }
84
85
}