Completed
Push — master ( 5d8de1...1d5edb )
by Veaceslav
22:14 queued 20:21
created

HeadersConstraint   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 14.02 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
c 1
b 0
f 1
lcom 1
cbo 3
dl 15
loc 107
ccs 29
cts 29
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B matches() 15 24 6
A failureDescription() 0 4 1
A additionalFailureDescription() 0 4 1
A toString() 0 4 1
A normalizeJsonSchema() 0 4 1
A normalizeHeaderValue() 0 8 2

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
 * This file is part of Rebilly.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @see http://rebilly.com
9
 */
10
11
namespace Rebilly\OpenAPI\PhpUnit;
12
13
use PHPUnit_Framework_Constraint as Constraint;
14
use Rebilly\OpenAPI\JsonSchema\Validator;
15
use Rebilly\OpenAPI\UnexpectedValueException;
16
17
/**
18
 * Constraint that asserts that the headers list matches the expected defined schemas.
19
 */
20
final class HeadersConstraint extends Constraint
21
{
22
    /**
23
     * @var array
24
     */
25
    private $errors = [];
26
27
    /**
28
     * @var array
29
     */
30
    private $expectedHeadersSchemas;
31
32
    /**
33
     * @var Validator
34
     */
35
    private $validator;
36
37
    /**
38
     * @param array $expectedHeadersSchemas
39
     */
40 4
    public function __construct(array $expectedHeadersSchemas) {
41 4
        parent::__construct();
42
43 4
        $this->expectedHeadersSchemas = array_map([$this, 'normalizeJsonSchema'], $expectedHeadersSchemas);
44 4
        $this->validator = new Validator('undefined');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 4
    protected function matches($actualHeaders)
51
    {
52 4
        if (!is_array($actualHeaders)) {
53 1
            throw new UnexpectedValueException('Array expected');
54
        }
55
56 3 View Code Duplication
        foreach ($this->expectedHeadersSchemas as $name => $expectedSchema) {
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 3
            if (isset($actualHeaders[$name])) {
58 2
                $errors = $this->validator->validate(
59 2
                    $this->normalizeHeaderValue($actualHeaders[$name]),
60
                    $expectedSchema,
61
                    $name
62
                );
63 2
                $this->errors = array_merge($this->errors, $errors);
64 1
            } elseif (isset($expectedSchema->required) && $expectedSchema->required) {
65 1
                $this->errors[] = [
66 1
                    'property' => $name,
67 3
                    'message' => "Missing required header ({$name})",
68
                ];
69
            }
70
        }
71
72 3
        return empty($this->errors);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    protected function failureDescription($other)
79
    {
80 2
        return json_encode($other) . ' ' . $this->toString();
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 2
    protected function additionalFailureDescription($other)
87
    {
88 2
        return $this->validator->serializeErrors($this->errors);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 2
    public function toString()
95
    {
96 2
        return 'matches an specified headers schemas';
97
    }
98
99
    /**
100
     * Ensure schema is object.
101
     *
102
     * @param object|array $schema
103
     *
104
     * @return object
105
     */
106 4
    private static function normalizeJsonSchema($schema)
107
    {
108 4
        return (object) $schema;
109
    }
110
111
    /**
112
     * Ensure header value is array.
113
     *
114
     * @param mixed $value
115
     *
116
     * @return array
117
     */
118 2
    private static function normalizeHeaderValue($value)
119
    {
120 2
        if (is_string($value)) {
121 2
            return preg_split('#\s*,\s*#', $value, -1, PREG_SPLIT_NO_EMPTY);
122
        } else {
123 1
            return (array) $value;
124
        }
125
    }
126
}
127