GenerateTypecast   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 58
ccs 18
cts 20
cp 0.9
rs 10
c 1
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A compute() 0 16 5
A run() 0 7 2
A typecast() 0 20 6
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
    public function run(Registry $registry): Registry
18
    {
19
        foreach ($registry as $entity) {
20
            $this->compute($registry, $entity);
21
        }
22 8
23
        return $registry;
24 8
    }
25 8
26
    /**
27
     * Automatically clarify column types based on table column types.
28 8
     *
29
     */
30
    protected function compute(Registry $registry, Entity $entity): void
31
    {
32
        if (!$registry->hasTable($entity)) {
33
            return;
34
        }
35
36
        $table = $registry->getTableSchema($entity);
37 8
38
        foreach ($entity->getFields() as $field) {
39 8
            if ($field->hasTypecast() || !$table->hasColumn($field->getColumn())) {
40
                continue;
41
            }
42
43 8
            $column = $table->column($field->getColumn());
44
45 8
            $field->setTypecast($this->typecast($column));
46 8
        }
47
    }
48
49
    /**
50 8
     *
51
     * @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...
52 8
     */
53
    private function typecast(AbstractColumn $column)
54 8
    {
55
        switch ($column->getType()) {
56
            case AbstractColumn::BOOL:
57
                return 'bool';
58
            case AbstractColumn::INT:
59
                return 'int';
60
            case AbstractColumn::FLOAT:
61 8
                return 'float';
62
        }
63 8
64
        if (in_array($column->getAbstractType(), ['datetime', 'date', 'time', 'timestamp'])) {
65 6
            return 'datetime';
66
        }
67 8
68
        if ($column->getType() === AbstractColumn::STRING) {
69 8
            return 'string';
70
        }
71
72 8
        return null;
73 8
    }
74
}
75