Completed
Push — master ( d8465d...85c396 )
by Stefano
03:17
created

ValidatedDescriptionHandler::__invoke()   D

Complexity

Conditions 10
Paths 1

Size

Total Lines 39
Code Lines 21

Duplication

Lines 25
Ratio 64.1 %

Code Coverage

Tests 23
CRAP Score 10.3248

Importance

Changes 0
Metric Value
dl 25
loc 39
ccs 23
cts 27
cp 0.8519
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 21
nc 1
nop 1
crap 10.3248

How to fix   Complexity   

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 namespace GuzzleHttp\Command\Guzzle\Handler;
2
3
use GuzzleHttp\Command\CommandInterface;
4
use GuzzleHttp\Command\Exception\CommandException;
5
use GuzzleHttp\Command\Guzzle\DescriptionInterface;
6
use GuzzleHttp\Command\Guzzle\SchemaValidator;
7
8
/**
9
 * Handler used to validate command input against a service description.
10
 *
11
 * @author Stefano Kowalke <[email protected]>
12
 */
13
class ValidatedDescriptionHandler
14
{
15
    /** @var SchemaValidator $validator */
16
    private $validator;
17
18
    /** @var DescriptionInterface $description */
19
    private $description;
20
21
    /**
22
     * ValidatedDescriptionHandler constructor.
23
     *
24
     * @param DescriptionInterface $description
25
     * @param SchemaValidator|null $schemaValidator
26
     */
27 3
    public function __construct(DescriptionInterface $description, SchemaValidator $schemaValidator = null)
28
    {
29 3
        $this->description = $description;
30 3
        $this->validator = $schemaValidator ?: new SchemaValidator();
31 3
    }
32
33
    /**
34
     * @param callable $handler
35
     * @return \Closure
36
     */
37
    public function __invoke(callable $handler)
38
    {
39 3
        return function (CommandInterface $command) use ($handler) {
40 3
            $errors = [];
41 3
            $operation = $this->description->getOperation($command->getName());
42
43 3 View Code Duplication
            foreach ($operation->getParams() as $name => $schema) {
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...
44 1
                $value = $command[$name];
45 1
                if (! $this->validator->validate($schema, $value)) {
46 1
                    $errors = array_merge($errors, $this->validator->getErrors());
47 1
                } elseif ($value !== $command[$name]) {
48
                    // Update the config value if it changed and no validation
49
                    // errors were encountered
50
                    $command[$name] = $value;
51
                }
52 3
            }
53
54 3
            if ($params = $operation->getAdditionalParameters()) {
55 1 View Code Duplication
                foreach ($command->toArray() as $name => $value) {
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...
56
                    // It's only additional if it isn't defined in the schema
57 1
                    if (! $operation->hasParam($name)) {
58
                        // Always set the name so that error messages are useful
59 1
                        $params->setName($name);
60 1
                        if (! $this->validator->validate($params, $value)) {
61 1
                            $errors = array_merge($errors, $this->validator->getErrors());
62 1
                        } elseif ($value !== $command[$name]) {
63
                            $command[$name] = $value;
64
                        }
65 1
                    }
66 1
                }
67 1
            }
68
69 3
            if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70 2
                throw new CommandException('Validation errors: ' . implode("\n", $errors), $command);
71
            }
72
73 1
            return $handler($command);
74 3
        };
75
    }
76
}
77