GenerateTypecast   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 62
ccs 20
cts 22
cp 0.9091
rs 10
c 1
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compute() 0 16 5
A run() 0 7 2
A typecast() 0 16 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Schema\Generator;
6
7
use Cycle\Schema\Definition\Entity;
8
use Cycle\Schema\GeneratorInterface;
9
use Cycle\Schema\Registry;
10
use Cycle\Database\Schema\AbstractColumn;
0 ignored issues
show
Bug introduced by
The type Cycle\Database\Schema\AbstractColumn was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Must be run after RenderTable.
14
 */
15
final class GenerateTypecast implements GeneratorInterface
16
{
17
    /**
18
     * @param Registry $registry
19
     *
20
     * @return Registry
21
     */
22 8
    public function run(Registry $registry): Registry
23
    {
24 8
        foreach ($registry as $entity) {
25 8
            $this->compute($registry, $entity);
26
        }
27
28 8
        return $registry;
29
    }
30
31
    /**
32
     * Automatically clarify column types based on table column types.
33
     *
34
     * @param Registry $registry
35
     * @param Entity   $entity
36
     */
37 8
    protected function compute(Registry $registry, Entity $entity): void
38
    {
39 8
        if (!$registry->hasTable($entity)) {
40
            return;
41
        }
42
43 8
        $table = $registry->getTableSchema($entity);
44
45 8
        foreach ($entity->getFields() as $field) {
46 8
            if ($field->hasTypecast() || !$table->hasColumn($field->getColumn())) {
47
                continue;
48
            }
49
50 8
            $column = $table->column($field->getColumn());
51
52 8
            $field->setTypecast($this->typecast($column));
53
        }
54 8
    }
55
56
    /**
57
     * @param AbstractColumn $column
58
     *
59
     * @return callable-array|string|null
0 ignored issues
show
Documentation Bug introduced by
The doc comment callable-array|string|null at position 0 could not be parsed: Unknown type name 'callable-array' at position 0 in callable-array|string|null.
Loading history...
60
     */
61 8
    private function typecast(AbstractColumn $column)
62
    {
63 8
        switch ($column->getType()) {
64
            case AbstractColumn::BOOL:
65 6
                return 'bool';
66
            case AbstractColumn::INT:
67 8
                return 'int';
68
            case AbstractColumn::FLOAT:
69 8
                return 'float';
70
        }
71
72 8
        if (in_array($column->getAbstractType(), ['datetime', 'date', 'time', 'timestamp'])) {
73 8
            return 'datetime';
74
        }
75
76 8
        return null;
77
    }
78
}
79