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; |
|
|
|
|
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 |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths