Completed
Pull Request — master (#3)
by Viacheslav
02:33
created

SchemaExporterInterface   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 96.43%

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 57
ccs 27
cts 28
cp 0.9643
rs 10
c 0
b 0
f 0
wmc 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 55 10
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
            foreach ($propertiesFound as $name) {
56 2
                $body->addSnippet(<<<PHP
57 2
\$schema->$name = \$this->$name;
58
59
PHP
60
                );
61
62
            }
63
64 2
            $body->addSnippet(<<<'PHP'
65 2
$schema->__fromRef = $this->__fromRef;
66
$schema->setDocumentPath($this->getDocumentPath());
67
$schema->addMeta($this, 'origin');
68
return $schema;
69
PHP
70
            );
71
72 2
            $func->setBody($body);
73 2
            $class->addMethod($func);
74 2
            $class->addImplements(PhpInterface::byFQN(SchemaExporter::class));
75
        }
76
77 2
    }
78
79
}