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

ObjectFactory   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 78
Duplicated Lines 20.51 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 4
dl 16
loc 78
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C create() 16 51 13
A supports() 0 4 1
instantiate() 0 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\Hydrator\Processors\Factory;
10
11
use KleijnWeb\PhpApi\Descriptions\Description\ComplexType;
12
use KleijnWeb\PhpApi\Descriptions\Description\Schema\ObjectSchema;
13
use KleijnWeb\PhpApi\Descriptions\Description\Schema\Schema;
14
use KleijnWeb\PhpApi\Descriptions\Hydrator\ProcessorBuilder;
15
use KleijnWeb\PhpApi\Descriptions\Hydrator\Processors\Object\ObjectProcessor;
16
17
/**
18
 * @author John Kleijn <[email protected]>
19
 */
20
abstract class ObjectFactory implements Factory
21
{
22
    /**
23
     * @param Schema           $schema
24
     * @param ProcessorBuilder $builder
25
     *
26
     * @return ObjectProcessor|null
27
     */
28
    public function create(Schema $schema, ProcessorBuilder $builder)
29
    {
30
        if (!$this->supports($schema)) {
31
            return null;
32
        }
33
34
        /** @var ObjectSchema $objectSchema */
35
        $objectSchema = $schema;
36
        $processor    = $this->instantiate($objectSchema, $builder);
37
38
        // TODO: Handle multiple roots
39
        if ($objectSchema->hasComplexType()) {
40
            $root = $objectSchema->getComplexType();
41
42
            // Recurse up to find root
43
            while ($parents = $root->getParents()) {
44
                $root = $parents[0];
45
            }
46
47
            // Add schema of all children up to root
48
            $descend = function (ComplexType $complexType) use (&$descend, $builder, $processor) {
49
50 View Code Duplication
                foreach ($complexType->getSchema()->getPropertySchemas() as $propertyName => $propertySchema) {
0 ignored issues
show
Bug introduced by
The expression $complexType->getSchema()->getPropertySchemas() of type object<stdClass> is not traversable.
Loading history...
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...
51
                    if ($propertySchema instanceof ObjectSchema && $propertySchema->getXType() !== null && !$propertySchema->hasComplexType()) {
52
                        throw new \LogicException("ComplexType never resolved");
53
                    }
54
                    $propertyProcessor = $builder->build($propertySchema);
55
56
                    $processor->setPropertyProcessor($propertyName, $propertyProcessor);
57
                }
58
59
                foreach ($complexType->getChildren() as $child) {
60
                    $descend($child);
61
                }
62
            };
63
64
            $descend($root);
65
        }
66
67
68 View Code Duplication
        foreach ($objectSchema->getPropertySchemas() as $propertyName => $propertySchema) {
0 ignored issues
show
Bug introduced by
The expression $objectSchema->getPropertySchemas() of type object<stdClass> is not traversable.
Loading history...
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...
69
            if ($propertySchema instanceof ObjectSchema && $propertySchema->getXType() !== null && !$propertySchema->hasComplexType()) {
70
                throw new \LogicException("ComplexType never resolved");
71
            }
72
            $propertyProcessor = $builder->build($propertySchema);
73
74
            $processor->setPropertyProcessor($propertyName, $propertyProcessor);
75
        }
76
77
        return $processor;
78
    }
79
80
    /**
81
     * @param Schema $schema
82
     *
83
     * @return bool
84
     */
85
    public function supports(Schema $schema)
86
    {
87
        return $schema instanceof ObjectSchema;
88
    }
89
90
    /**
91
     * @param ObjectSchema     $schema
92
     * @param ProcessorBuilder $builder
93
     *
94
     * @return ObjectProcessor
95
     */
96
    abstract protected function instantiate(ObjectSchema $schema, ProcessorBuilder $builder): ObjectProcessor;
97
}
98