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

ObjectFactory::create()   C

Complexity

Conditions 13
Paths 7

Size

Total Lines 51

Duplication

Lines 16
Ratio 31.37 %

Importance

Changes 0
Metric Value
dl 16
loc 51
rs 6.6166
c 0
b 0
f 0
cc 13
nc 7
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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