Completed
Pull Request — master (#23)
by John
02:43
created

SchemaFactory   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 163
Duplicated Lines 6.13 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 6
dl 10
loc 163
rs 9.84
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
F create() 10 82 24
B resolveTypes() 0 30 6
A setClassNameResolver() 0 4 1
A getTypeNameFromRefId() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the KleijnWeb\PhpApi\Descriptions package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\PhpApi\Descriptions\Description\Schema;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\ComplexType;
12
use KleijnWeb\PhpApi\Descriptions\Hydrator\ClassNameResolver;
13
14
/**
15
 * @author John Kleijn <[email protected]>
16
 */
17
class SchemaFactory
18
{
19
    /**
20
     * @var array
21
     */
22
    private $schemas = [];
23
24
    /**
25
     * @var ComplexType[]
26
     */
27
    private $complexTypes = [];
28
29
    /**
30
     * @var ObjectSchema[]
31
     */
32
    private $typedSchemas = [];
33
34
    /**
35
     * @var array
36
     */
37
    private $definitions = [];
38
39
    /**
40
     * @var ClassNameResolver
41
     */
42
    private $classNameResolver;
43
44
    /**
45
     * @param \stdClass|null $definition
46
     * @param string|null    $name
47
     *
48
     * @return Schema
49
     */
50
    public function create(\stdClass $definition = null, string $name = null): Schema
51
    {
52
        if (!$definition) {
53
            $definition       = (object)[];
54
            $definition->type = Schema::TYPE_ANY;
55
        }
56
        if (!isset($definition->type)) {
57
            $definition = clone $definition;
58
            if (isset($definition->allOf)) {
59
                foreach ($definition->allOf as $nested) {
60
                    if (isset($nested->type)) {
61
                        $definition->type = $nested->type;
62
                    }
63
                }
64
            }
65
            if (!isset($definition->type)) {
66
                $definition->type = Schema::TYPE_STRING;
67
            }
68
        }
69
        if (isset($definition->properties)) {
70
            $definition->type = 'object';
71
        }
72
73
        $index = var_export($definition, true);
74
75
        if (!isset($this->schemas[$index])) {
76
            if ($definition->type == Schema::TYPE_OBJECT) {
77
                $propertySchemas = (object)[];
78
79 View Code Duplication
                if (isset($definition->properties)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
                    foreach ($definition->properties as $attributeName => $propertyDefinition) {
81
                        $propertySchemas->$attributeName = $this->create($propertyDefinition);
82
                    }
83
                }
84
85
                if (isset($definition->allOf)) {
86
                    foreach ($definition->allOf as $nested) {
87
                        if (isset($nested->{'x-ref-id'})) {
88
                            continue;
89
                        }
90 View Code Duplication
                        if (isset($nested->properties)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
                            foreach ($nested->properties as $attributeName => $propertyDefinition) {
92
                                $propertySchemas->$attributeName = $this->create($propertyDefinition);
93
                            }
94
                        }
95
                    }
96
                    unset($definition->type);
97
                }
98
99
                if ($name === null && isset($definition->{'x-ref-id'})) {
100
                    $name = $this->getTypeNameFromRefId($definition);
101
                }
102
103
                $schema = new ObjectSchema($definition, $propertySchemas, null, $name);
104
105
                if (null !== $name) {
106
                    if (isset($this->typedSchemas[$name])) {
107
                        $a = var_export($schema, true);
0 ignored issues
show
Unused Code introduced by
$a is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
                        $b = var_export($this->typedSchemas[$name], true);
0 ignored issues
show
Unused Code introduced by
$b is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
                        $a2 = var_export($definition, true);
0 ignored issues
show
Unused Code introduced by
$a2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
110
                        $b2 = var_export($this->typedSchemas[$name]->getDefinition(), true);
0 ignored issues
show
Unused Code introduced by
$b2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
                        throw new \InvalidArgumentException("Type '$name' already exists");
112
                    }
113
                    $this->typedSchemas[$name] = $schema;
114
                }
115
116
            } elseif ($definition->type == Schema::TYPE_ARRAY) {
117
                $itemsSchema = isset($definition->items) ? $this->create($definition->items) : null;
118
                $schema      = new ArraySchema($definition, $itemsSchema);
119
            } elseif ($definition->type == Schema::TYPE_ANY) {
120
                $schema = new AnySchema($definition);
121
            } else {
122
                $schema = new ScalarSchema($definition);
123
            }
124
125
            $this->schemas[$index] = $schema;
126
127
            return $schema;
128
        }
129
130
        return $this->schemas[$index];
131
    }
132
133
    /**
134
     * @return ComplexType[]
135
     */
136
    public function resolveTypes(): array
137
    {
138
        foreach ($this->typedSchemas as $name => $schema) {
139
            $schema->setComplexType(new ComplexType(
140
                $name,
141
                $schema,
142
                $this->classNameResolver->resolve($name)
143
            ));
144
        }
145
146
        foreach ($this->typedSchemas as $name => $schema) {
147
            $definition = $schema->getDefinition();
148
149
            if (isset($definition->allOf)) {
150
                foreach ($definition->allOf as $partial) {
151
                    if (isset($partial->{'x-ref-id'})) {
152
                        $this->typedSchemas[$schema->getXType()]
153
                            ->getComplexType()
154
                            ->addParent(
155
                                $this->typedSchemas[$this->getTypeNameFromRefId($partial)]->getComplexType()
0 ignored issues
show
Bug introduced by
It seems like $this->typedSchemas[$thi...ial)]->getComplexType() can be null; however, addParent() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
156
                            );
157
                    }
158
                }
159
            }
160
        }
161
162
        return array_map(function(ObjectSchema $schema){
163
            return $schema->getComplexType();
164
        }, $this->typedSchemas);
165
    }
166
167
    /**
168
     * @param ClassNameResolver $classNameResolver
169
     */
170
    public function setClassNameResolver(ClassNameResolver $classNameResolver)
171
    {
172
        $this->classNameResolver = $classNameResolver;
173
    }
174
175
    private function getTypeNameFromRefId(\stdClass $definition)
176
    {
177
        return substr($definition->{'x-ref-id'}, strrpos($definition->{'x-ref-id'}, '/') + 1);
178
    }
179
}
180