Completed
Pull Request — master (#99)
by Viacheslav
08:50
created

ObjectItemTrait::getAllPatternPropertyNames()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 10
cc 4
nc 2
nop 0
crap 20
1
<?php
2
3
namespace Swaggest\JsonSchema\Structure;
4
5
use Swaggest\JsonSchema\MagicMapTrait;
6
7
/**
8
 * Trait ObjectItemTrait
9
 * @package Swaggest\JsonSchema\Structure
10
 * @see ObjectItemContract
11
 */
12
trait ObjectItemTrait
13
{
14
    use MagicMapTrait;
15
16
    /** @var ObjectItemContract[] */
17
    protected $__nestedObjects;
18
    protected $__documentPath;
19
    /** @var null|string[] */
20
    protected $__fromRef;
21 2
22 2
    protected $__hasResolvedValue = false;
23 2
    protected $__resolvedValue;
24
25
    public function setResolvedValue($value)
26
    {
27
        $this->__hasResolvedValue = true;
28 5
        $this->__resolvedValue = $value;
29
        return $this;
30 5
    }
31 5
32 5
    public function getResolvedValue()
33 5
    {
34 5
        return $this->__resolvedValue;
35 5
    }
36 3
37
    public function hasResolvedValue()
38
    {
39 5
        return $this->__hasResolvedValue;
40 5
    }
41 5
42
    public function getNestedObject($className)
43
    {
44 1012
        if (isset($this->__nestedObjects[$className])) {
45
            return $this->__nestedObjects[$className];
46 1012
        }
47 1012
        return null;
48
    }
49
50
    public function setNestedProperty($propertyName, $value, Egg $nestedEgg)
51
    {
52
        $nestedName = $nestedEgg->name;
53
        /** @var null $nested */
54
        $nested = &$this->__nestedObjects[$nestedName];
55
        if (null === $nested) {
0 ignored issues
show
introduced by
The condition null === $nested is always true.
Loading history...
56
            $nested = $nestedEgg->classSchema->makeObjectItem();
57
            $this->__nestedObjects[$nestedName] = $nested;
58
            if ($nestedName !== $nestedEgg->classSchema->getObjectItemClass()) {
59
                $this->$nestedName = $nested;
60
            }
61
        }
62 99
        $nested->$propertyName = $value;
63
        $this->__arrayOfData[$propertyName] = &$nested->$propertyName;
64 99
    }
65 99
66
    protected $__additionalPropertyNames;
67
68
    public function addAdditionalPropertyName($name)
69
    {
70
        $this->__additionalPropertyNames[$name] = true;
71
    }
72
73
    /**
74
     * @return string[]
75
     */
76
    public function getAdditionalPropertyNames()
77
    {
78
        if (null === $this->__additionalPropertyNames) {
79
            return [];
80 785
        }
81
        return array_keys($this->__additionalPropertyNames);
82 785
    }
83 1
84 1
    /**
85 1
     * @var string[][]
86 1
     */
87
    protected $__patternPropertyNames;
88
89 1
    public function addPatternPropertyName($pattern, $name)
90
    {
91 784
        $this->__patternPropertyNames[$pattern][$name] = true;
92
    }
93
94
    /**
95
     * @param string $pattern
96
     * @return null|string[]
97
     */
98
    public function getPatternPropertyNames($pattern)
99
    {
100
        if (isset($this->__patternPropertyNames[$pattern])) {
101
            return array_keys($this->__patternPropertyNames[$pattern]);
102
        }
103 3250
        return null;
104
    }
105 3250
106 3250
    /**
107
     * @return string[]
108
     */
109
    public function getAllPatternPropertyNames()
110
    {
111
        $result = array();
112
        if (isset($this->__patternPropertyNames)) {
113
            foreach ($this->__patternPropertyNames as $patternProperties) {
114
                if (is_array($patternProperties)) {
115
                    $result = array_merge($result, array_keys($patternProperties));
116
                }
117
            }
118
        }
119
        return $result;
120
    }
121
122
    public function jsonSerialize()
123
    {
124
        if ($this->__nestedObjects) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->__nestedObjects of type Swaggest\JsonSchema\Structure\ObjectItemContract[] 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...
125 448
            $result = $this->__arrayOfData;
126
            foreach ($this->__nestedObjects as $object) {
127 448
                foreach ($object->toArray() as $key => $value) {
128
                    $result[$key] = $value;
129
                }
130
            }
131
            return (object)$result;
132
        } else {
133
            return (object)$this->__arrayOfData;
134
        }
135 422
    }
136
137 422
    /**
138 422
     * @return string
139
     */
140 48
    public function getDocumentPath()
141 48
    {
142
        return $this->__documentPath;
143
    }
144 422
145
    public function setDocumentPath($path)
146
    {
147
        $this->__documentPath = $path;
148 3316
        return $this;
149 3316
    }
150 3225
151 3225
    /**
152 391
     * @return string|null
153 391
     * @deprecated use ObjectItemContract::getFromRefs
154 389
     * @see ObjectItemContract::getFromRefs
155
     * @todo remove
156
     * @see ObjectItemContract::getFromRef
157
     */
158
    public function getFromRef()
159 3316
    {
160
        return null === $this->__fromRef ? null : $this->__fromRef[0];
161
    }
162
163
    /**
164
     * @return string[]|null
165
     * @see ObjectItemContract::getFromRef
166
     */
167
    public function getFromRefs()
168
    {
169
        return $this->__fromRef;
170
    }
171
172
    /**
173
     * @param false|string $ref
174
     * @return $this
175
     * @see ObjectItemContract::setFromRef
176
     */
177
    public function setFromRef($ref)
178
    {
179
        if (null === $this->__fromRef) {
180
            $this->__fromRef = array($ref);
0 ignored issues
show
Documentation Bug introduced by
array($ref) is of type array<integer,false|string>, but the property $__fromRef was declared to be of type null|string[]. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
181
        } else {
182
            if (false !== $this->__fromRef[0]) {
0 ignored issues
show
introduced by
The condition false !== $this->__fromRef[0] is always true.
Loading history...
183
                $this->__fromRef[] = $ref;
184
            }
185
        }
186
        return $this;
187
    }
188
189
    private $__refPath;
190
191
    protected function getFromRefPath()
192
    {
193
        if ($this->__refPath === null) {
194
            $this->__refPath = '';
195
            if ($this->__fromRef) {
196
                foreach ($this->__fromRef as $ref) {
197
                    if ($ref) {
198
                        $this->__refPath = '->$ref[' . strtr($ref, array('~' => '~1', ':' => '~2')) . ']' . $this->__refPath;
199
                    }
200
                }
201
            }
202
        }
203
        return $this->__refPath;
204
    }
205
}
206