Passed
Pull Request — develop (#234)
by Mikaël
49:21 queued 46:19
created

Struct::getObjectKeyWithVirtual()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageGenerator\Container\Model;
6
7
use InvalidArgumentException;
8
use WsdlToPhp\PackageGenerator\Model\Struct as Model;
9
10
final class Struct extends AbstractModel
11
{
12
    /**
13
     * Only for virtually-considered objects (in order to avoid duplications in objects property).
14
     */
15
    protected array $virtualObjects = [];
16
17 486
    public function getStructByName(string $name): ?Model
18
    {
19 486
        return $this->get($name);
20
    }
21
22 166
    public function getStructByNameAndType(string $name, string $type): ?Model
23
    {
24 166
        return $this->getByType($name, $type);
25
    }
26
27 112
    public function addVirtualStruct(string $structName, string $structType = ''): self
28
    {
29 112
        return $this->addStruct($structName, false, $structType);
30
    }
31
32 116
    public function addStruct(string $structName, bool $isStruct = true, string $structType = ''): self
33
    {
34 116
        if (null === (empty($structType) ? $this->get($structName) : $this->getByType($structName, $structType))) {
35 112
            $model = new Model($this->generator, $structName, $isStruct);
36 112
            $this->add($model->setInheritance($structType));
37
        }
38
39 116
        return $this;
40
    }
41
42 116
    public function addStructWithAttribute(string $structName, string $attributeName, string $attributeType): self
43
    {
44 116
        $this->addStruct($structName);
45 116
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
46 116
            $struct->addAttribute($attributeName, $attributeType);
47
        }
48
49 116
        return $this;
50
    }
51
52 20
    public function addUnionStruct(string $structName, array $types): self
53
    {
54 20
        $this->addVirtualStruct($structName);
55 20
        if (($struct = $this->getStructByName($structName)) instanceof Model) {
56 20
            $struct->setTypes($types);
57
        }
58
59 20
        return $this;
60
    }
61
62 370
    public function getVirtual(string $value): ?Model
63
    {
64 370
        if (!is_scalar($value)) {
0 ignored issues
show
introduced by
The condition is_scalar($value) is always true.
Loading history...
65
            throw new InvalidArgumentException(sprintf('Value "%s" can\'t be used to get an object from "%s"', is_object($value) ? get_class($value) : var_export($value, true), __CLASS__), __LINE__);
66
        }
67
68 370
        $key = $this->getVirtualKey($value);
69
70 370
        return array_key_exists($key, $this->virtualObjects) ? $this->virtualObjects[$key] : null;
71
    }
72
73 222
    public function getByType(string $value, string $type): ?Model
74
    {
75 222
        $key = $this->getTypeKey($value, $type);
76
77 222
        return array_key_exists($key, $this->objects) ? $this->objects[$key] : null;
78
    }
79
80 312
    public function getObjectKeyWithType(object $object, string $type): string
81
    {
82 312
        return $this->getTypeKey($this->getObjectKey($object), $type);
83
    }
84
85 312
    public function getObjectKeyWithVirtual(object $object): string
86
    {
87 312
        return $this->getVirtualKey($this->getObjectKey($object));
88
    }
89
90
    /**
91
     * The key must not conflict with possible key values.
92
     */
93 342
    public function getTypeKey(string $name, string $type): string
94
    {
95 342
        return sprintf('struct_name_%s-type_%s', $name, $type);
96
    }
97
98
    /**
99
     * The key must not conflict with possible key values.
100
     */
101 466
    public function getVirtualKey(string $name): string
102
    {
103 466
        return sprintf('virtual_struct_name_%s', $name);
104
    }
105
106
    /**
107
     * 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.
108
     *
109
     * @param Model $object
110
     *
111
     * @return Struct
112
     */
113 320
    public function add(object $object): self
114
    {
115 320
        $inheritance = $object->getInheritance();
116 320
        if (!empty($inheritance)) {
117 312
            $this->virtualObjects[$this->getObjectKeyWithVirtual($object)] = $this->objects[$this->getObjectKeyWithType($object, $object->getInheritance())] = $object;
118
        } else {
119 318
            parent::add($object);
120
        }
121
122 320
        return $this;
123
    }
124
125 318
    protected function objectClass(): string
126
    {
127 318
        return Model::class;
128
    }
129
}
130