Passed
Push — master ( fc0f41...29bb39 )
by Viacheslav
15:22 queued 05:53
created

TypeBuilder::processLogicType()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 9
nc 12
nop 0
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 7
rs 8.8333
c 1
b 0
f 0
1
<?php
2
3
namespace Swaggest\PhpCodeBuilder\JsonSchema;
4
5
6
use Swaggest\JsonSchema\Constraint\Type;
7
use Swaggest\JsonSchema\Schema;
8
use Swaggest\PhpCodeBuilder\PhpAnyType;
9
use Swaggest\PhpCodeBuilder\PhpStdType;
10
use Swaggest\PhpCodeBuilder\Types\ArrayOf;
11
use Swaggest\PhpCodeBuilder\Types\OrType;
12
13
class TypeBuilder
14
{
15
    /** @var Schema */
16
    private $schema;
17
    /** @var string */
18
    private $path;
19
    /** @var PhpBuilder */
20
    private $phpBuilder;
21
    /** @var OrType */
22
    private $result;
23
24
    /**
25
     * TypeBuilder constructor.
26
     * @param Schema $schema
27
     * @param string $path
28
     * @param PhpBuilder $phpBuilder
29
     */
30 12
    public function __construct($schema, $path, PhpBuilder $phpBuilder)
31
    {
32 12
        $this->schema = $schema;
33 12
        $this->path = $path;
34 12
        $this->phpBuilder = $phpBuilder;
35 12
    }
36
37 12
    /**
38
     * @throws Exception
39 12
     * @throws \Swaggest\PhpCodeBuilder\Exception
40 12
     */
41 3
    private function processLogicType()
42 12
    {
43 4
        if ($this->schema->allOf !== null) {
44 12
            foreach ($this->schema->allOf as $i => $item) {
45 4
                $this->result->add($this->phpBuilder->getType($item, $this->path . '->allOf[' . $i . ']'));
46
            }
47
        }
48 12
49 7
        if ($this->schema->anyOf !== null) {
50 7
            foreach ($this->schema->anyOf as $i => $item) {
51
                $this->result->add($this->phpBuilder->getType($item, $this->path . '->anyOf[' . $i . ']'));
52
            }
53 12
        }
54
55 12
        if ($this->schema->oneOf !== null) {
56
            foreach ($this->schema->oneOf as $i => $item) {
57 12
                $this->result->add($this->phpBuilder->getType($item, $this->path . '->oneOf[' . $i . ']'));
58
            }
59 12
        }
60 12
    }
61 2
62 2
    /**
63 12
     * @throws Exception
64 12
     * @throws \Swaggest\PhpCodeBuilder\Exception
65 12
     */
66
    private function processAdditionalPatternProperties()
67
    {
68
        $schema = $this->schema;
69
70
        if ($schema->additionalProperties instanceof Schema) {
71
            $type = $this->phpBuilder->getType($schema->additionalProperties, $this->path . '->additionalProperties');
72 12
            if ($type !== PhpStdType::mixed()) {
73 12
                $this->result->add(new ArrayOf($type));
74 12
            }
75 12
        }
76
77 12
        if ($schema->patternProperties !== null) {
78 2
            foreach ($schema->patternProperties as $pattern => $property) {
79
                if ($property instanceof Schema) {
80
                    $type = $this->phpBuilder->getType($property, $this->path . "->patternProperties->{{$pattern}}");
81
                    if ($type !== PhpStdType::mixed()) {
82 12
                        $this->result->add(new ArrayOf($type));
83
                    }
84 12
                }
85
            }
86 12
        }
87
88
    }
89 12
90
91 12
    /**
92 4
     * @throws Exception
93
     * @throws \Swaggest\PhpCodeBuilder\Exception
94
     */
95
    private function processArrayType()
96
    {
97
        $schema = $this->schema;
98 12
99 3
        /** @var string $pathItems */
100 3
        $pathItems = Schema::names()->items;
0 ignored issues
show
Documentation Bug introduced by
It seems like Swaggest\JsonSchema\Schema::names()->items can also be of type string. However, the property $items is declared as type Swaggest\JsonSchema\Sche...a\SchemaContract[]|null. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
101 3
        if ($this->isSchema($schema->items)) {
102
            $items = array();
103
            $additionalItems = $schema->items;
104
        } elseif ($schema->items === null) { // items defaults to empty schema so everything is valid
105 12
            $items = array();
106
            $additionalItems = true;
107 8
        } else { // listed items
108
            $items = $schema->items;
109 8
            $additionalItems = $schema->additionalItems;
110
            $pathItems = Schema::names()->additionalItems;
0 ignored issues
show
Documentation Bug introduced by
It seems like Swaggest\JsonSchema\Sche...ames()->additionalItems can also be of type string. However, the property $additionalItems is declared as type Swaggest\JsonSchema\SchemaContract|boolean|null. Maybe add an additional type 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 mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
111 5
        }
112
113
        $itemsLen = is_array($items) ? count($items) : 0;
114 6
        $index = 0;
115
        if ($index < $itemsLen) {
116
        } else {
117 6
            if ($additionalItems instanceof Schema) {
118
                $this->result->add(new ArrayOf($this->phpBuilder->getType($additionalItems, $this->path . '->' . $pathItems)));
119
            }
120 7
        }
121
    }
122
123
    private function isSchema($var)
124
    {
125
        return $var instanceof Schema;
126
    }
127
128 4
    /**
129
     * @throws Exception
130
     * @throws \Swaggest\PhpCodeBuilder\Exception
131 2
     */
132
    private function processObjectType()
133
    {
134 7
        if ($this->schema->patternProperties !== null) {
135
            foreach ($this->schema->patternProperties as $pattern => $schema) {
136
                //$this->result->add(new ArrayOf($this->phpBuilder->getType($schema, $this->path . '->' . $pattern)));
137
            }
138
        }
139
140
141
        if ($this->schema->additionalProperties instanceof Schema) {
142
            $this->result->add(new ArrayOf($this->phpBuilder->getType(
143
                $this->schema->additionalProperties,
144 12
                $this->path . '->' . Schema::names()->additionalProperties)
0 ignored issues
show
Bug introduced by
Are you sure Swaggest\JsonSchema\Sche...)->additionalProperties of type Swaggest\JsonSchema\SchemaContract|boolean|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

144
                $this->path . '->' . /** @scrutinizer ignore-type */ Schema::names()->additionalProperties)
Loading history...
145
            ));
146 12
        }
147 10
148 10
    }
149
150 12
    private function typeSwitch($type)
151
    {
152
        switch ($type) {
153
            case Type::INTEGER:
154
                return PhpStdType::int();
155
156
            case Type::NUMBER:
157 12
                return PhpStdType::float();
158
159 12
            case TYPE::BOOLEAN:
160 12
                return PhpStdType::bool();
161
162
            case Type::STRING:
163
                return PhpStdType::string();
164 12
165 4
            /*
166
            case Type::OBJECT:
167
                return PhpStdType::object();
168
            */
169
170 12
            case Type::ARR:
171 12
                return PhpStdType::arr();
172 12
173 12
            case Type::NULL:
174
                return PhpStdType::null();
175 12
176
            default:
177
                return null;
178
        }
179 12
    }
180 8
181
    /**
182
     * @throws Exception
183 12
     * @throws \Swaggest\PhpCodeBuilder\Exception
184
     */
185
    private function processNamedClass()
186
    {
187
        if ($this->schema->properties !== null) {
188
            $class = $this->phpBuilder->getClass($this->schema, $this->path);
189
            $this->result->add($class);
190
        }
191
    }
192
193
    /**
194
     * @return PhpAnyType
195
     * @throws Exception
196
     * @throws \Swaggest\PhpCodeBuilder\Exception
197
     */
198
    public function build()
199
    {
200
        $this->result = new OrType();
201
        if ($this->schema === null) {
202
            throw new Exception('Null schema');
203
        }
204
205
        if ($fromRefs = $this->schema->getFromRefs()) {
206
            $this->path = $fromRefs[count($fromRefs) - 1];
207
            //$this->result->add($this->phpBuilder->getType($this->schema->ref->getData(), $this->schema->ref->ref));
208
        }
209
210
211
        $this->processNamedClass();
212
        $this->processLogicType();
213
        $this->processArrayType();
214
        $this->processObjectType();
215
        $this->processAdditionalPatternProperties();
216
217
        if (is_array($this->schema->type)) {
218
            foreach ($this->schema->type as $type) {
219
                $this->result->add($this->typeSwitch($type));
220
            }
221
        } elseif ($this->schema->type) {
222
            $this->result->add($this->typeSwitch($this->schema->type));
223
        }
224
225
        return $this->result->simplify();
226
227
    }
228
}