Completed
Pull Request — master (#10)
by Viacheslav
07:30
created

SchemaExporterInterface::process()   D

Complexity

Conditions 21
Paths 42

Size

Total Lines 105
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 50
CRAP Score 21.0033

Importance

Changes 0
Metric Value
cc 21
eloc 60
nc 42
nop 3
dl 0
loc 105
ccs 50
cts 51
cp 0.9804
crap 21.0033
rs 4.1666
c 0
b 0
f 0

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
2
3
namespace Swaggest\PhpCodeBuilder\JsonSchema;
4
5
use Swaggest\CodeBuilder\PlaceholderString;
6
use Swaggest\JsonSchema\Schema;
7
use Swaggest\JsonSchema\SchemaExporter;
8
use Swaggest\JsonSchema\Wrapper;
9
use Swaggest\PhpCodeBuilder\PhpClass;
10
use Swaggest\PhpCodeBuilder\PhpCode;
11
use Swaggest\PhpCodeBuilder\PhpFunction;
12
use Swaggest\PhpCodeBuilder\PhpInterface;
13
use Swaggest\PhpCodeBuilder\Types\TypeOf;
14
15
/**
16
 * Implements SchemaExporter if class has at least 5 intersecting properties with JsonSchema
17
 */
18
class SchemaExporterInterface implements PhpBuilderClassHook
19
{
20 2
    public function process(PhpClass $class, $path, $schema)
21
    {
22 2
        $schemaProperties = Schema::properties();
23
24 2
        $propertiesFound = array();
25 2
        foreach ($class->getProperties() as $property) {
26 2
            $schemaName = $property->getNamedVar()->getName();
27
            //$schemaName = $property->getMeta(PhpBuilder::PROPERTY_NAME);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
28
            /** @var Schema $propertySchema */
29 2
            $propertySchema = $property->getMeta(PhpBuilder::SCHEMA);
30
31 2
            $schemaProperty = $schemaProperties[$schemaName];
32 2
            if ($schemaProperty instanceof SchemaExporter) {
33
                $schemaProperty = $schemaProperty->exportSchema();
34
            }
35 2
            if ($schemaProperty !== null) {
36 2
                if (empty($schemaProperty->type)
37 2
                    || ($schemaProperty->type == $propertySchema->type)
38 2
                    || (is_array($schemaProperty->type) && in_array($propertySchema->type, $schemaProperty->type))
39
                ) {
40 2
                    $propertiesFound[] = $property->getNamedVar()->getName();
41
                }
42
            }
43
        }
44
45 2
        if (count($propertiesFound) > 5) {
46 2
            $func = new PhpFunction('exportSchema');
47 2
            $func->setResult(PhpClass::byFQN(Schema::class));
48 2
            $body = (new PhpCode())->addSnippet(new PlaceholderString(<<<PHP
49 2
\$schema = new :schema();
50
51
PHP
52 2
                , [':schema' => new TypeOf(PhpClass::byFQN(Schema::class))]
53
            ));
54
55 2
            $names = Schema::names();
56 2
            foreach ($propertiesFound as $name) {
57 2
                if ($name === $names->items
58 2
                    || $name === $names->additionalProperties
59 2
                    || $name === $names->additionalItems
60 2
                    || $name === $names->not
61 2
                    || $name === $names->if
62 2
                    || $name === $names->then
63 2
                    || $name === $names->else) {
64 2
                    $body->addSnippet(<<<PHP
65 2
if (\$this->$name !== null && \$this->$name instanceof SchemaExporter) {
66 2
    \$schema->$name = \$this->{$name}->exportSchema();
67
}
68
69
PHP
70
                    );
71 2
                    continue;
72
                }
73
74 2
                if ($name === $names->allOf || $name === $names->oneOf || $name === $names->anyOf) {
75 2
                    $body->addSnippet(<<<PHP
76 2
if (!empty(\$this->$name)) {
77 2
    foreach (\$this->$name as \$i => \$item) {
78
        if (\$item instanceof SchemaExporter) {
79 2
            \$schema->{$name}[\$i] = \$item->exportSchema();
80
        }
81
    }
82
}
83
84
PHP
85
                    );
86 2
                    continue;
87
                }
88
89
90 2
                if ($name === $names->properties) {
91 2
                    $body->addSnippet(<<<PHP
92 2
if (!empty(\$this->$name)) {
93 2
    foreach (\$this->$name as \$propertyName => \$propertySchema) {
94
        if (is_string(\$propertyName) && \$propertySchema instanceof SchemaExporter) {
95
            \$schema->setProperty(\$propertyName, \$propertySchema->exportSchema());
96
        }
97
    }
98
}
99
100
PHP
101
                    );
102 2
                    continue;
103
                }
104
105
106 2
                $body->addSnippet(<<<PHP
107 2
\$schema->$name = \$this->$name;
108
109
PHP
110
                );
111
112
            }
113
114 2
            $body->addSnippet(<<<'PHP'
115 2
$schema->__fromRef = $this->__fromRef;
116
$schema->setDocumentPath($this->getDocumentPath());
117
$schema->addMeta($this, 'origin');
118
return $schema;
119
PHP
120
            );
121
122 2
            $func->setBody($body);
123 2
            $class->addMethod($func);
124 2
            $class->addImplements(PhpInterface::byFQN(SchemaExporter::class));
125
        }
126
127 2
    }
128
129
}