Completed
Branch feature/issue-157 (4a7b0b)
by Mikaël
16:29
created

Struct::getObjectKeyWithVirtual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace WsdlToPhp\PackageGenerator\Container\Model;
4
5
use WsdlToPhp\PackageGenerator\Model\Struct as Model;
6
7
class Struct extends AbstractModel
8
{
9
    /**
10
     * Only for virtually-considered objects (in order to avoid duplucations in objects property)
11
     * @var array $virtualObjects
12
     */
13
    protected $virtualObjects = [];
14
    /**
15
     * @see \WsdlToPhp\PackageGenerator\Container\Model\Model::objectClass()
16
     * @return string
17
     */
18 786
    protected function objectClass()
19
    {
20 786
        return 'WsdlToPhp\PackageGenerator\Model\Struct';
21
    }
22
    /**
23
     * @param string $name
24
     * @return Model|null
25
     */
26 798
    public function getStructByName($name)
27
    {
28 798
        return $this->get($name);
29
    }
30
    /**
31
     * @param string $name
32
     * @param string $type
33
     * @return Model|null
34
     */
35 354
    public function getStructByNameAndType($name, $type)
36
    {
37 354
        return $this->getByType($name, $type);
38
    }
39
    /**
40
     * Adds a virtual struct
41
     * @param string $structName the original struct name
42
     * @param string $structType the original struct type
43
     * @return Struct
44
     */
45 294
    public function addVirtualStruct($structName, $structType = '')
46
    {
47 294
        return $this->addStruct($structName, false, $structType);
48
    }
49
    /**
50
     * Adds type to structs
51
     * @param string $structName the original struct name
52
     * @param bool $isStruct whether the Struct has to be generated or not
53
     * @param string $structType the original struct type
54
     * @return Struct
55
     */
56 306
    public function addStruct($structName, $isStruct = true, $structType = '')
57
    {
58 306
        if (null === (empty($structType) ? $this->get($structName) : $this->getByType($structName, $structType))) {
59 294
            $model = new Model($this->generator, $structName, $isStruct);
60 294
            $this->add($model->setInheritance($structType));
0 ignored issues
show
Compatibility introduced by
$model->setInheritance($structType) of type object<WsdlToPhp\Package...or\Model\AbstractModel> is not a sub-type of object<WsdlToPhp\PackageGenerator\Model\Struct>. It seems like you assume a child class of the class WsdlToPhp\PackageGenerator\Model\AbstractModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
61 147
        }
62 306
        return $this;
63
    }
64
    /**
65
     * Adds type to structs and its attribute
66
     * @param string $structName the original struct name
67
     * @param string $attributeName the attribute name
68
     * @param string $attributeType the attribute type
69
     * @return Struct
70
     */
71 306
    public function addStructWithAttribute($structName, $attributeName, $attributeType)
72
    {
73 306
        $this->addStruct($structName);
74 306
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
75 306
            $struct->addAttribute($attributeName, $attributeType);
76 153
        }
77 306
        return $this;
78
    }
79
    /**
80
     * Adds a union struct
81
     * @param string $structName
82
     * @param string[] $types
83
     * @return Struct
84
     */
85 24
    public function addUnionStruct($structName, $types)
86
    {
87 24
        $this->addVirtualStruct($structName);
88 24
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
89 24
            $struct->setTypes($types);
90 12
        }
91 24
        return $this;
92
    }
93
    /**
94
     * @see \WsdlToPhp\PackageGenerator\Model\AbstractModel::get()
95
     * @param string $value
96
     * @return Model|null
97
     */
98 798
    public function get($value)
99
    {
100 798
        return parent::get($value);
101
    }
102
    /**
103
     * @see parent::get()
104
     * @param string $value
105
     * @return mixed
106
     */
107 606
    public function getVirtual($value)
108
    {
109 606
        if (!is_string($value) && !is_int($value)) {
110
            throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object', var_export($value, true), get_class($this)), __LINE__);
111
        }
112 606
        $key = $this->getVirtualKey($value);
113 606
        return array_key_exists($key, $this->virtualObjects) ? $this->virtualObjects[$key] : null;
114
    }
115
    /**
116
     * @see parent::get()
117
     * @param string $value
118
     * @param string $type
119
     * @return mixed
120
     */
121 558
    public function getByType($value, $type)
122
    {
123 558
        if (!is_string($value) && !is_int($value)) {
124
            throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', var_export($value, true), get_class($this)), __LINE__);
125
        }
126 558
        if (!is_scalar($type)) {
127
            throw new \InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object', var_export($type, true)), __LINE__);
128
        }
129 558
        $key = $this->getTypeKey($value, $type);
130 558
        return array_key_exists($key, $this->objects) ? $this->objects[$key] : null;
131
    }
132
    /**
133
     * @param $object
134
     * @param $type
135
     * @return string
136
     */
137 768
    public function getObjectKeyWithType($object, $type)
138
    {
139 768
        return $this->getTypeKey($this->getObjectKey($object), $type);
140
    }
141
    /**
142
     * @param $object
143
     * @param $type
144
     * @return string
145
     */
146 768
    public function getObjectKeyWithVirtual($object)
147
    {
148 768
        return $this->getVirtualKey($this->getObjectKey($object));
149
    }
150
    /**
151
     * The key must not conflict with possible key values
152
     * @param $name
153
     * @param $type
154
     * @return string
155
     */
156 828
    public function getTypeKey($name, $type)
157
    {
158 828
        return sprintf('struct_name_%s-type_%s', $name, $type);
159
    }
160
    /**
161
     * The key must not conflict with possible key values
162
     * @param $type
163
     * @return string
164
     */
165 846
    public function getVirtualKey($name)
166
    {
167 846
        return sprintf('virtual_struct_name_%s', $name);
168
    }
169
    /**
170
     * By overriding this method, we ensure that each time a new object is stored, it is stored with our new key if the inheritance is defined.
171
     * @param Model $object
172
     * @return Struct
173
     */
174 792
    public function add($object)
175
    {
176 792
        $inheritance = $object->getInheritance();
177 792
        if (!empty($inheritance)) {
178 768
            $this->virtualObjects[$this->getObjectKeyWithVirtual($object, $object->getInheritance())] = $this->objects[$this->getObjectKeyWithType($object, $object->getInheritance())] = $object;
179 384
        } else {
180 786
            parent::add($object);
181
        }
182 792
        return $this;
183
    }
184
}
185