Validator::validate()   B
last analyzed

Complexity

Conditions 9
Paths 8

Size

Total Lines 57

Duplication

Lines 16
Ratio 28.07 %

Code Coverage

Tests 32
CRAP Score 9

Importance

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

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 16
    public function __construct(ParserInterface $parser, ValidatorConfig $config)
22
    {
23 16
        $this->parser = $parser;
24 16
        $this->config = $config;
25 16
    }
26
27 16
    public function validate($value): Resolution
28
    {
29 16
        if ($value === null && $this->config->isAllowEmpty()) {
30 1
            return Resolution::createValid();
31
        }
32
33 15
        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 13
        $trimmedValue = trim($value);
40
41 13
        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 11
            $barcode = $this->parser->parse($trimmedValue);
51 8
        } catch (InvalidBarcodeException $exception) {
52 8
            return Resolution::createInvalid([
53 8
                ErrorCodes::INVALID_VALUE => sprintf(
54 8
                    'Value is invalid: %s',
55 8
                    $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
}