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

Validator::validate()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 57

Duplication

Lines 16
Ratio 28.07 %

Code Coverage

Tests 27
CRAP Score 9.3086

Importance

Changes 0
Metric Value
dl 16
loc 57
ccs 27
cts 32
cp 0.8438
rs 7.3826
c 0
b 0
f 0
cc 9
nc 8
nop 1
crap 9.3086

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
}