Completed
Pull Request — master (#3)
by Guilh
02:22
created

Responses::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 8
ccs 3
cts 5
cp 0.6
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.2559
1
<?php
2
3
namespace gossi\swagger\collections;
4
5
use gossi\swagger\parts\ExtensionPart;
6
use gossi\swagger\Response;
7
use phootwork\collection\CollectionUtils;
8
use phootwork\collection\Map;
9
use phootwork\lang\Arrayable;
10
use phootwork\lang\Text;
11
12
class Responses implements Arrayable, \Iterator
13
{
14
    use ExtensionPart;
15
16
    /** @var Map */
17
    private $responses;
18
19 12
    public function __construct($contents = [])
20
    {
21 12
        $this->parse($contents === null ? [] : $contents);
22 12
    }
23
24 12
    private function parse($contents)
25
    {
26 12
        $data = CollectionUtils::toMap($contents);
27
28
        // responses
29 12
        $this->responses = new Map();
30 12
        foreach ($data as $r => $response) {
31 6
            if (!Text::create($r)->startsWith('x-')) {
32 6
                $this->responses->set($r, new Response($r, $response));
33 6
            }
34 12
        }
35
36
        // extensions
37 12
        $this->parseExtensions($data);
38 12
    }
39
40 7
    public function toArray()
41
    {
42 7
        $responses = clone $this->responses;
43 7
        $responses->setAll($this->getExtensions());
44
45 7
        return $responses->toArray();
46
    }
47
48 1
    public function size()
49
    {
50 1
        return $this->responses->size();
51
    }
52
53
    /**
54
     * Returns whether the given response exists.
55
     * 
56
     * @param string $code
57
     *
58
     * @return bool
59
     */
60 1
    public function has($code)
61
    {
62 1
        return $this->responses->has($code);
63
    }
64
65
    /**
66
     * Returns whether the given response exists.
67
     * 
68
     * @param Response $response
69
     *
70
     * @return bool
71
     */
72 8
    public function contains(Response $response)
73
    {
74 8
        return $this->responses->contains($response);
75
    }
76
77
    /**
78
     * Returns the reponse info for the given code.
79
     * 
80
     * @param string $code
81
     *
82
     * @return Response
83
     */
84 2
    public function get($code)
85
    {
86 2
        if (!$this->responses->has($code)) {
87
            $this->responses->set($code, new Response($code));
88
        }
89
90 2
        return $this->responses->get($code);
91
    }
92
93
    /**
94
     * Sets the response.
95
     * 
96
     * @param Response $code
0 ignored issues
show
Bug introduced by
There is no parameter named $code. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
97
     */
98 1
    public function add(Response $response)
99
    {
100 1
        $this->responses->set($response->getCode(), $response);
101 1
    }
102
103
    /**
104
     * Removes the given repsonse.
105
     * 
106
     * @param string $code
107
     */
108 1
    public function remove($code)
109
    {
110 1
        $this->responses->remove($code);
111 1
    }
112
113
    public function current()
114
    {
115
        return $this->responses->current();
116
    }
117
118
    public function key()
119
    {
120
        return $this->responses->key();
121
    }
122
123
    public function next()
124
    {
125
        return $this->responses->next();
126
    }
127
128
    public function rewind()
129
    {
130
        return $this->responses->rewind();
131
    }
132
133
    public function valid()
134
    {
135
        return $this->responses->valid();
136
    }
137
}
138