|
1
|
|
|
<?php |
|
2
|
|
|
namespace DBFaker\Helpers; |
|
3
|
|
|
|
|
4
|
|
|
use DBFaker\Exceptions\RuntimeSchemaException; |
|
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Column; |
|
6
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
9
|
|
|
|
|
10
|
|
|
class SchemaHelper |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Schema |
|
15
|
|
|
*/ |
|
16
|
|
|
private $schema; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Schema $schema) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->schema = $schema; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Table $table |
|
25
|
|
|
* @param Column $column |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public function isColumnPartOfUniqueIndex(Table $table, Column $column): bool |
|
29
|
|
|
{ |
|
30
|
|
|
$indexes = $this->schema->getTable($table->getName())->getIndexes(); |
|
31
|
|
|
foreach ($indexes as $index) { |
|
32
|
|
|
if (!$index->isUnique()) { |
|
33
|
|
|
continue; |
|
34
|
|
|
} |
|
35
|
|
|
foreach ($index->getColumns() as $columnName) { |
|
36
|
|
|
if ($column->getName() === $columnName) { |
|
37
|
|
|
return true; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Table $table |
|
47
|
|
|
* @param Column $column |
|
48
|
|
|
* @return bool |
|
49
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function isPrimaryKeyColumn(Table $table, Column $column) : bool |
|
52
|
|
|
{ |
|
53
|
|
|
$table = $this->schema->getTable($table->getName()); |
|
54
|
|
|
return \in_array($column->getName(), $table->getPrimaryKeyColumns(), true); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param Table $table |
|
59
|
|
|
* @param Column $column |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function isColumnPartOfForeignKeyConstraint(Table $table, Column $column): bool |
|
63
|
|
|
{ |
|
64
|
|
|
$constraint = $this->getForeignKeyConstraintByLocal($table, $column); |
|
65
|
|
|
return $constraint !== null; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param Table $table |
|
70
|
|
|
* @param Column $column |
|
71
|
|
|
* @return ForeignKeyConstraint|null |
|
72
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getForeignKeyConstraintByLocal(Table $table, Column $column) : ?ForeignKeyConstraint |
|
75
|
|
|
{ |
|
76
|
|
|
$table = $this->schema->getTable($table->getName()); |
|
77
|
|
|
foreach ($table->getForeignKeys() as $foreignKeyConstraint) { |
|
78
|
|
|
if (\in_array($column->getName(), $foreignKeyConstraint->getLocalColumns(), true)) { |
|
79
|
|
|
return $foreignKeyConstraint; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Tells if $colum in $table is at the same time : |
|
87
|
|
|
* - a primarykey |
|
88
|
|
|
* - a foreign key to another table's primarykey |
|
89
|
|
|
* This means current $table is extending the foreign table |
|
90
|
|
|
* @param ForeignKeyConstraint $fk |
|
91
|
|
|
* @return bool |
|
92
|
|
|
* @throws \Doctrine\DBAL\DBALException |
|
93
|
|
|
*/ |
|
94
|
|
|
public function isExtendingKey(ForeignKeyConstraint $fk) : bool |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$fk->getLocalTable()->hasPrimaryKey()) { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
$fkColumnNames = $fk->getLocalColumns(); |
|
100
|
|
|
$pkColumnNames = $fk->getLocalTable()->getPrimaryKeyColumns(); |
|
101
|
|
|
|
|
102
|
|
|
sort($fkColumnNames); |
|
103
|
|
|
sort($pkColumnNames); |
|
104
|
|
|
|
|
105
|
|
|
return $fkColumnNames === $pkColumnNames; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function isForeignKeyAlsoUniqueIndex(ForeignKeyConstraint $fk) : bool |
|
109
|
|
|
{ |
|
110
|
|
|
$table = $fk->getLocalTable(); |
|
111
|
|
|
foreach ($table->getIndexes() as $index) { |
|
112
|
|
|
if ($index->isUnique() && count($index->getColumns()) === count($fk->getLocalColumns())) { |
|
113
|
|
|
$indexCols = $index->getColumns(); |
|
114
|
|
|
$fkCols = $fk->getColumns(); |
|
115
|
|
|
sort($indexCols); |
|
116
|
|
|
sort($fkCols); |
|
117
|
|
|
if ($indexCols == $fkCols) { |
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
return false; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
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