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

Paths::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\Path;
7
use phootwork\collection\Map;
8
use phootwork\lang\Arrayable;
9
use phootwork\lang\Text;
10
use phootwork\collection\CollectionUtils;
11
12
class Paths implements Arrayable, \Iterator
13
{
14
    use ExtensionPart;
15
16
    /** @var Map */
17
    private $paths;
18
19 10
    public function __construct($contents = [])
20
    {
21 10
        $this->parse($contents === null ? [] : $contents);
22 10
    }
23
24 10
    private function parse($contents)
25
    {
26 10
        $data = CollectionUtils::toMap($contents);
27
28
        // paths
29 10
        $this->paths = new Map();
30 10
        foreach ($data as $p => $path) {
31 6
            if (!Text::create($p)->startsWith('x-')) {
32 6
                $this->paths->set($p, new Path($p, $path));
33 6
            }
34 10
        }
35
36
        // extensions
37 10
        $this->parseExtensions($data);
38 10
    }
39
40 7
    public function toArray()
41
    {
42 7
        $paths = clone $this->paths;
43
// 		$paths->setAll($this->getExtensions());
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44 7
        return $paths->toArray();
45
    }
46
47 1
    public function size()
48
    {
49 1
        return $this->paths->size();
50
    }
51
52
    /**
53
     * Returns whether a path with the given name exists.
54
     * 
55
     * @param string $path
56
     *
57
     * @return bool
58
     */
59 1
    public function has($path)
60
    {
61 1
        return $this->paths->has($path);
62
    }
63
64
    /**
65
     * Returns whether the given path exists.
66
     * 
67
     * @param Path $path
68
     *
69
     * @return bool
70
     */
71 1
    public function contains(Path $path)
72
    {
73 1
        return $this->paths->contains($path);
74
    }
75
76
    /**
77
     * Returns the path info for the given path.
78
     * 
79
     * @param string $path
80
     *
81
     * @return Path
82
     */
83 2
    public function get($path)
84
    {
85 2
        if (!$this->paths->has($path)) {
86
            $this->paths->set($path, new Path($path));
87
        }
88
89 2
        return $this->paths->get($path);
90
    }
91
92
    /**
93
     * Sets the path.
94
     * 
95
     * @param Path $path
96
     *
97
     * @return $this
98
     */
99 1
    public function add(Path $path)
100
    {
101 1
        $this->paths->set($path->getPath(), $path);
102
103 1
        return $this;
104
    }
105
106
    /**
107
     * Removes the given path.
108
     * 
109
     * @param string $path
110
     */
111 1
    public function remove($path)
112
    {
113 1
        $this->paths->remove($path);
114
115 1
        return $this;
116
    }
117
118
    public function current()
119
    {
120
        return $this->paths->current();
121
    }
122
123
    public function key()
124
    {
125
        return $this->paths->key();
126
    }
127
128
    public function next()
129
    {
130
        return $this->paths->next();
131
    }
132
133
    public function rewind()
134
    {
135
        return $this->paths->rewind();
136
    }
137
138
    public function valid()
139
    {
140
        return $this->paths->valid();
141
    }
142
}
143