Completed
Push — master ( d75c1f...eed677 )
by Viacheslav
12s
created

SchemaExporterInterface   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 9

1 Method

Rating   Name   Duplication   Size   Complexity  
C process() 0 52 9
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
}