Completed
Pull Request — master (#1)
by Viacheslav
02:50
created

SchemaExporterInterface::process()   C

Complexity

Conditions 9
Paths 12

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 9

Importance

Changes 0
Metric Value
cc 9
eloc 32
nc 12
nop 3
dl 0
loc 52
ccs 26
cts 26
cp 1
crap 9
rs 6.5703
c 0
b 0
f 0

How to fix   Long Method   

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\PhpCodeBuilder\PhpClass;
9
use Swaggest\PhpCodeBuilder\PhpCode;
10
use Swaggest\PhpCodeBuilder\PhpFunction;
11
use Swaggest\PhpCodeBuilder\PhpInterface;
12
use Swaggest\PhpCodeBuilder\Types\TypeOf;
13
14
/**
15
 * Implements SchemaExporter if class has at least 5 intersecting properties with JsonSchema
16
 */
17
class SchemaExporterInterface implements PhpBuilderClassHook
18
{
19 2
    public function process(PhpClass $class, $path, $schema)
20
    {
21 2
        $schemaProperties = Schema::properties();
22
23 2
        $propertiesFound = array();
24 2
        foreach ($class->getProperties() as $property) {
25 2
            $schemaName = $property->getNamedVar()->getName();
26
            //$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...
27
            /** @var Schema $propertySchema */
28 2
            $propertySchema = $property->getMeta(PhpBuilder::SCHEMA);
29
30 2
            $schemaProperty = $schemaProperties[$schemaName];
31 2
            if ($schemaProperty !== null) {
32 2
                if (empty($schemaProperty->type)
33 2
                    || ($schemaProperty->type == $propertySchema->type)
34 2
                    || (is_array($schemaProperty->type) && in_array($propertySchema->type, $schemaProperty->type))
35
                ) {
36 2
                    $propertiesFound[] = $property->getNamedVar()->getName();
37
                }
38
            }
39
        }
40
41 2
        if (count($propertiesFound) > 5) {
42 2
            $func = new PhpFunction('exportSchema');
43 2
            $func->setResult(PhpClass::byFQN(Schema::class));
44 2
            $body = (new PhpCode())->addSnippet(new PlaceholderString(<<<PHP
45 2
\$schema = new :schema();
46
47
PHP
48 2
                , [':schema' => new TypeOf(PhpClass::byFQN(Schema::class))]
49
            ));
50
51 2
            foreach ($propertiesFound as $name) {
52 2
                $body->addSnippet(<<<PHP
53 2
\$schema->$name = \$this->$name;
54
55
PHP
56
                );
57
58
            }
59
60 2
            $body->addSnippet(<<<'PHP'
61 2
$schema->__fromRef = $this->__fromRef;
62
$schema->setDocumentPath($this->getDocumentPath());
63
$schema->addMeta($this, 'origin');
64
return $schema;
65
PHP
66
            );
67
68 2
            $func->setBody($body);
69 2
            $class->addMethod($func);
70 2
            $class->addImplements(PhpInterface::byFQN(SchemaExporter::class));
71
        }
72
73 2
    }
74
75
}