Passed
Pull Request — master (#97)
by Aleksei
02:34
created

SchemaToPHP   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 14
c 1
b 0
f 0
dl 0
loc 29
ccs 10
cts 12
cp 0.8333
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 3 1
A convert() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Cycle\Schema\Converter;
6
7
use Cycle\ORM\Relation;
8
use Cycle\ORM\SchemaInterface;
9
use Cycle\Schema\Relation\RelationSchema;
10
use Yiisoft\Yii\Cycle\Schema\Converter\SchemaToPHP\SchemaRenderer;
11
12
final class SchemaToPHP
13
{
14
    private SchemaInterface $schema;
15
    private const USE_LIST = [
16
        SchemaInterface::class . ' as Schema',
17
        Relation::class,
18
        RelationSchema::class,
19
    ];
20
21 3
    public function __construct(SchemaInterface $schema)
22
    {
23 3
        $this->schema = $schema;
24 3
    }
25
26
    public function __toString(): string
27
    {
28
        return $this->convert();
29
    }
30
31 3
    public function convert(): string
32
    {
33 3
        $result = "<?php\n\ndeclare(strict_types=1);\n\n";
34
        // the use block
35 3
        foreach (self::USE_LIST as $use) {
36 3
            $result .= "use {$use};\n";
37
        }
38 3
        $renderedArray = (new SchemaRenderer($this->schema))->render();
39 3
        $result .= "\nreturn {$renderedArray};\n";
40 3
        return $result;
41
    }
42
}
43