Skip to content

Commit 98b704c

Browse files
Merge pull request #584 from kamil-tekiela/getForeignKeys
Refactor getForeignKeys()
2 parents 859b644 + 741fce0 commit 98b704c

File tree

6 files changed

+154
-154
lines changed

6 files changed

+154
-154
lines changed

phpstan-baseline.neon

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,16 @@ parameters:
475475
count: 2
476476
path: src/Statements/CallStatement.php
477477

478+
-
479+
message: "#^Cannot access property \\$database on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
480+
count: 1
481+
path: src/Statements/CreateStatement.php
482+
483+
-
484+
message: "#^Cannot access property \\$table on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
485+
count: 1
486+
path: src/Statements/CreateStatement.php
487+
478488
-
479489
message: "#^Cannot access property \\$value on PhpMyAdmin\\\\SqlParser\\\\Token\\|null\\.$#"
480490
count: 2
@@ -497,14 +507,19 @@ parameters:
497507

498508
-
499509
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
500-
count: 8
510+
count: 11
501511
path: src/Statements/CreateStatement.php
502512

503513
-
504514
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Parsers\\\\ParameterDefinitions\\:\\:buildAll\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>, array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>\\|null given\\.$#"
505515
count: 1
506516
path: src/Statements/CreateStatement.php
507517

518+
-
519+
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, mixed given\\.$#"
520+
count: 2
521+
path: src/Statements/CreateStatement.php
522+
508523
-
509524
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Statements\\\\CreateStatement\\:\\:\\$fields \\(array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\CreateDefinition\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\|null\\) does not accept array\\<PhpMyAdmin\\\\SqlParser\\\\Component\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\.$#"
510525
count: 1
@@ -875,31 +890,16 @@ parameters:
875890
count: 1
876891
path: src/Utils/Query.php
877892

878-
-
879-
message: "#^Cannot access property \\$database on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
880-
count: 1
881-
path: src/Utils/Table.php
882-
883-
-
884-
message: "#^Cannot access property \\$table on PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\|null\\.$#"
885-
count: 1
886-
path: src/Utils/Table.php
887-
888893
-
889894
message: "#^Cannot call method has\\(\\) on PhpMyAdmin\\\\SqlParser\\\\Components\\\\OptionsArray\\|null\\.$#"
890-
count: 4
895+
count: 1
891896
path: src/Utils/Table.php
892897

893898
-
894899
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Table\\:\\:getFields\\(\\) should return array\\<string, array\\{type\\: string, timestamp_not_null\\: bool, default_value\\?\\: mixed, default_current_timestamp\\?\\: true, on_update_current_timestamp\\?\\: true, expr\\?\\: mixed\\}\\> but returns array\\<string, array\\{default_current_timestamp\\?\\: true, default_value\\?\\: mixed, expr\\?\\: mixed, on_update_current_timestamp\\?\\: true, timestamp_not_null\\: bool, type\\: string\\|null\\}\\>\\.$#"
895900
count: 1
896901
path: src/Utils/Table.php
897902

898-
-
899-
message: "#^Parameter \\#3 \\$subject of function str_replace expects array\\|string, mixed given\\.$#"
900-
count: 2
901-
path: src/Utils/Table.php
902-
903903
-
904904
message: "#^Parameter \\#2 \\$list of static method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:replaceClause\\(\\) expects PhpMyAdmin\\\\SqlParser\\\\TokensList, PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null given\\.$#"
905905
count: 1

psalm-baseline.xml

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -679,19 +679,31 @@
679679
</file>
680680
<file src="src/Statements/CreateStatement.php">
681681
<MixedArgument>
682+
<code><![CDATA[$opt]]></code>
683+
<code><![CDATA[$opt]]></code>
682684
<code><![CDATA[$this->$field]]></code>
683685
</MixedArgument>
686+
<MixedAssignment>
687+
<code><![CDATA[$opt]]></code>
688+
<code><![CDATA[$opt]]></code>
689+
</MixedAssignment>
684690
<MixedOperand>
685691
<code><![CDATA[$this->$field]]></code>
686692
</MixedOperand>
687693
<PossiblyFalseOperand>
688694
<code><![CDATA[$brackets]]></code>
689695
<code><![CDATA[$brackets]]></code>
690696
</PossiblyFalseOperand>
697+
<PossiblyInvalidPropertyAssignmentValue>
698+
<code><![CDATA[str_replace(' ', '_', $opt)]]></code>
699+
<code><![CDATA[str_replace(' ', '_', $opt)]]></code>
700+
</PossiblyInvalidPropertyAssignmentValue>
691701
<PossiblyNullArgument>
692702
<code><![CDATA[$this->parameters]]></code>
693703
</PossiblyNullArgument>
694704
<PossiblyNullPropertyFetch>
705+
<code><![CDATA[$field->references->table->database]]></code>
706+
<code><![CDATA[$field->references->table->table]]></code>
695707
<code><![CDATA[$token->value]]></code>
696708
<code><![CDATA[$token->value]]></code>
697709
</PossiblyNullPropertyFetch>
@@ -713,6 +725,8 @@
713725
<code><![CDATA[build]]></code>
714726
<code><![CDATA[has]]></code>
715727
<code><![CDATA[has]]></code>
728+
<code><![CDATA[has]]></code>
729+
<code><![CDATA[has]]></code>
716730
</PossiblyNullReference>
717731
<PropertyTypeCoercion>
718732
<code><![CDATA[$token->value]]></code>
@@ -725,6 +739,7 @@
725739
<code><![CDATA[ArrayObjs::parse($parser, $list)]]></code>
726740
</PropertyTypeCoercion>
727741
<RiskyTruthyFalsyComparison>
742+
<code><![CDATA[empty($this->fields)]]></code>
728743
<code><![CDATA[empty($this->partitionBy)]]></code>
729744
<code><![CDATA[empty($this->partitionBy)]]></code>
730745
<code><![CDATA[empty($this->partitions)]]></code>
@@ -1022,6 +1037,17 @@
10221037
<code><![CDATA[$obj instanceof Parser]]></code>
10231038
</RedundantConditionGivenDocblockType>
10241039
</file>
1040+
<file src="src/Utils/ForeignKey.php">
1041+
<PossiblyUnusedProperty>
1042+
<code><![CDATA[$constraint]]></code>
1043+
<code><![CDATA[$indexList]]></code>
1044+
<code><![CDATA[$onDelete]]></code>
1045+
<code><![CDATA[$onUpdate]]></code>
1046+
<code><![CDATA[$refDbName]]></code>
1047+
<code><![CDATA[$refIndexList]]></code>
1048+
<code><![CDATA[$refTableName]]></code>
1049+
</PossiblyUnusedProperty>
1050+
</file>
10251051
<file src="src/Utils/Formatter.php">
10261052
<InvalidArrayOffset>
10271053
<code><![CDATA[JoinKeyword::JOINS[$list->tokens[$list->idx - 2]->value]]]></code>
@@ -1165,13 +1191,7 @@
11651191
<LessSpecificReturnStatement>
11661192
<code><![CDATA[$ret]]></code>
11671193
</LessSpecificReturnStatement>
1168-
<MixedArgument>
1169-
<code><![CDATA[$opt]]></code>
1170-
<code><![CDATA[$opt]]></code>
1171-
</MixedArgument>
11721194
<MixedAssignment>
1173-
<code><![CDATA[$opt]]></code>
1174-
<code><![CDATA[$opt]]></code>
11751195
<code><![CDATA[$option]]></code>
11761196
<code><![CDATA[$option]]></code>
11771197
<code><![CDATA[$option]]></code>
@@ -1196,18 +1216,11 @@
11961216
<code><![CDATA[$ret]]></code>
11971217
<code><![CDATA[$ret]]></code>
11981218
</PossiblyNullArrayOffset>
1199-
<PossiblyNullPropertyFetch>
1200-
<code><![CDATA[$field->references->table->database]]></code>
1201-
<code><![CDATA[$field->references->table->table]]></code>
1202-
</PossiblyNullPropertyFetch>
12031219
<PossiblyNullReference>
12041220
<code><![CDATA[has]]></code>
1205-
<code><![CDATA[has]]></code>
1206-
<code><![CDATA[has]]></code>
12071221
</PossiblyNullReference>
12081222
<RiskyTruthyFalsyComparison>
12091223
<code><![CDATA[empty($statement->fields)]]></code>
1210-
<code><![CDATA[empty($statement->fields)]]></code>
12111224
</RiskyTruthyFalsyComparison>
12121225
</file>
12131226
<file src="tests/Builder/AlterStatementTest.php">

src/Statements/CreateStatement.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
use PhpMyAdmin\SqlParser\Token;
2424
use PhpMyAdmin\SqlParser\TokensList;
2525
use PhpMyAdmin\SqlParser\TokenType;
26+
use PhpMyAdmin\SqlParser\Utils\ForeignKey;
2627

2728
use function is_array;
29+
use function str_replace;
2830
use function trim;
2931

3032
/**
@@ -775,4 +777,55 @@ public function parse(Parser $parser, TokensList $list): void
775777
}
776778
}
777779
}
780+
781+
/** @return list<ForeignKey> */
782+
public function getForeignKeys(): array
783+
{
784+
if (empty($this->fields) || (! is_array($this->fields)) || (! $this->options->has('TABLE'))) {
785+
return [];
786+
}
787+
788+
$ret = [];
789+
790+
foreach ($this->fields as $field) {
791+
if ($field->key === null || $field->key->type !== 'FOREIGN KEY') {
792+
continue;
793+
}
794+
795+
$columns = [];
796+
foreach ($field->key->columns as $column) {
797+
if (! isset($column['name'])) {
798+
continue;
799+
}
800+
801+
$columns[] = $column['name'];
802+
}
803+
804+
$foreignKey = new ForeignKey();
805+
$foreignKey->constraint = $field->name;
806+
$foreignKey->indexList = $columns;
807+
808+
if ($field->references !== null) {
809+
$foreignKey->refDbName = $field->references->table->database;
810+
$foreignKey->refTableName = $field->references->table->table;
811+
$foreignKey->refIndexList = $field->references->columns;
812+
813+
$opt = $field->references->options->has('ON UPDATE');
814+
815+
if ($opt) {
816+
$foreignKey->onUpdate = str_replace(' ', '_', $opt);
817+
}
818+
819+
$opt = $field->references->options->has('ON DELETE');
820+
821+
if ($opt) {
822+
$foreignKey->onDelete = str_replace(' ', '_', $opt);
823+
}
824+
}
825+
826+
$ret[] = $foreignKey;
827+
}
828+
829+
return $ret;
830+
}
778831
}

src/Utils/ForeignKey.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpMyAdmin\SqlParser\Utils;
6+
7+
final class ForeignKey
8+
{
9+
/**
10+
* @param (int|string)[] $indexList
11+
* @param string[] $refIndexList
12+
*/
13+
public function __construct(
14+
public string|null $constraint = null,
15+
public array $indexList = [],
16+
public string|null $refDbName = null,
17+
public string|null $refTableName = null,
18+
public array $refIndexList = [],
19+
public string|null $onUpdate = null,
20+
public string|null $onDelete = null,
21+
) {
22+
}
23+
}

src/Utils/Table.php

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,78 +7,12 @@
77
use PhpMyAdmin\SqlParser\Statements\CreateStatement;
88

99
use function is_array;
10-
use function str_replace;
1110

1211
/**
1312
* Table utilities.
1413
*/
1514
class Table
1615
{
17-
/**
18-
* Gets the foreign keys of the table.
19-
*
20-
* @return list<(string|string[]|null)[]>
21-
* @psalm-return list<array{
22-
* constraint: string|null,
23-
* index_list: (int|string)[],
24-
* ref_db_name?: string|null,
25-
* ref_table_name?: string|null,
26-
* ref_index_list?: string[],
27-
* on_update?: string|string[],
28-
* on_delete?: string|string[],
29-
* }>
30-
*/
31-
public static function getForeignKeys(CreateStatement $statement): array
32-
{
33-
if (empty($statement->fields) || (! is_array($statement->fields)) || (! $statement->options->has('TABLE'))) {
34-
return [];
35-
}
36-
37-
$ret = [];
38-
39-
foreach ($statement->fields as $field) {
40-
if (empty($field->key) || ($field->key->type !== 'FOREIGN KEY')) {
41-
continue;
42-
}
43-
44-
$columns = [];
45-
foreach ($field->key->columns as $column) {
46-
if (! isset($column['name'])) {
47-
continue;
48-
}
49-
50-
$columns[] = $column['name'];
51-
}
52-
53-
$tmp = [
54-
'constraint' => $field->name,
55-
'index_list' => $columns,
56-
];
57-
58-
if (! empty($field->references)) {
59-
$tmp['ref_db_name'] = $field->references->table->database;
60-
$tmp['ref_table_name'] = $field->references->table->table;
61-
$tmp['ref_index_list'] = $field->references->columns;
62-
63-
$opt = $field->references->options->has('ON UPDATE');
64-
65-
if ($opt) {
66-
$tmp['on_update'] = str_replace(' ', '_', $opt);
67-
}
68-
69-
$opt = $field->references->options->has('ON DELETE');
70-
71-
if ($opt) {
72-
$tmp['on_delete'] = str_replace(' ', '_', $opt);
73-
}
74-
}
75-
76-
$ret[] = $tmp;
77-
}
78-
79-
return $ret;
80-
}
81-
8216
/**
8317
* Gets fields of the table.
8418
*

0 commit comments

Comments
 (0)