Completed
Pull Request — master (#130)
by Michaël
02:22
created

ValidatedDescriptionHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 40 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 4
dl 26
loc 65
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
D __invoke() 26 40 10

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 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 4
    public function __construct(DescriptionInterface $description, SchemaValidator $schemaValidator = null)
28
    {
29 4
        $this->description = $description;
30 4
        $this->validator = $schemaValidator ?: new SchemaValidator();
31 4
    }
32
33
    /**
34
     * @param callable $handler
35
     * @return \Closure
36
     */
37
    public function __invoke(callable $handler)
38
    {
39 4
        return function (CommandInterface $command) use ($handler) {
40 4
            $errors = [];
41 4
            $operation = $this->description->getOperation($command->getName());
42
43 4 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 2
                $value = $schema->filter($command[$name]);
45
46 2
                if (! $this->validator->validate($schema, $value)) {
47 1
                    $errors = array_merge($errors, $this->validator->getErrors());
48 2
                } elseif ($value !== $command[$name]) {
49
                    // Update the config value if it changed and no validation
50
                    // errors were encountered
51 1
                    $command[$name] = $value;
52 1
                }
53 4
            }
54
55 4
            if ($params = $operation->getAdditionalParameters()) {
56 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...
57
                    // It's only additional if it isn't defined in the schema
58 1
                    if (! $operation->hasParam($name)) {
59
                        // Always set the name so that error messages are useful
60 1
                        $params->setName($name);
61 1
                        if (! $this->validator->validate($params, $value)) {
62 1
                            $errors = array_merge($errors, $this->validator->getErrors());
63 1
                        } elseif ($value !== $command[$name]) {
64
                            $command[$name] = $value;
65
                        }
66 1
                    }
67 1
                }
68 1
            }
69
70 4
            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...
71 2
                throw new CommandException('Validation errors: ' . implode("\n", $errors), $command);
72
            }
73
74 2
            return $handler($command);
75 4
        };
76
    }
77
}
78