Skip to content

Commit 9a5eaab

Browse files
Merge pull request #365 from mauriciofauth/component-interface
Change `Component` abstract class to an interface
2 parents 8f5bdb5 + 66b7dda commit 9a5eaab

30 files changed

+264
-116
lines changed

phpstan-baseline.neon

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Component\\:\\:__toString\\(\\) should return string but returns mixed\\.$#"
5-
count: 1
6-
path: src/Component.php
7-
83
-
94
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Component\\:\\:build\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
105
count: 1
@@ -105,6 +100,11 @@ parameters:
105100
count: 1
106101
path: src/Components/Array2d.php
107102

103+
-
104+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\Array2d\\:\\:build\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\>, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Array2d\\) given\\.$#"
105+
count: 1
106+
path: src/Components/Array2d.php
107+
108108
-
109109
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\:\\:__construct\\(\\) has parameter \\$raw with no value type specified in iterable type array\\.$#"
110110
count: 1
@@ -180,6 +180,11 @@ parameters:
180180
count: 1
181181
path: src/Components/Condition.php
182182

183+
-
184+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition\\:\\:build\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition\\>, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition\\) given\\.$#"
185+
count: 1
186+
path: src/Components/Condition.php
187+
183188
-
184189
message: "#^Parameter \\#1 \\$expr of class PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition constructor expects string\\|null, mixed given\\.$#"
185190
count: 1
@@ -340,6 +345,11 @@ parameters:
340345
count: 1
341346
path: src/Components/ExpressionArray.php
342347

348+
-
349+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\ExpressionArray\\:\\:build\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\>, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ExpressionArray\\) given\\.$#"
350+
count: 1
351+
path: src/Components/ExpressionArray.php
352+
343353
-
344354
message: "#^Parameter \\#1 \\$string of function strlen expects string, string\\|null given\\.$#"
345355
count: 1
@@ -500,6 +510,11 @@ parameters:
500510
count: 1
501511
path: src/Components/JoinKeyword.php
502512

513+
-
514+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:build\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\>, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\) given\\.$#"
515+
count: 1
516+
path: src/Components/JoinKeyword.php
517+
503518
-
504519
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$JOINS type has no value type specified in iterable type array\\.$#"
505520
count: 1
@@ -690,6 +705,11 @@ parameters:
690705
count: 1
691706
path: src/Components/ParameterDefinition.php
692707

708+
-
709+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:build\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\>, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\) given\\.$#"
710+
count: 1
711+
path: src/Components/ParameterDefinition.php
712+
693713
-
694714
message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$inOut \\(string\\) does not accept string\\|null\\.$#"
695715
count: 1
@@ -860,6 +880,21 @@ parameters:
860880
count: 1
861881
path: src/Components/UnionKeyword.php
862882

883+
-
884+
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Components\\\\UnionKeyword\\:\\:parse\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
885+
count: 1
886+
path: src/Components/UnionKeyword.php
887+
888+
-
889+
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\UnionKeyword\\:\\:build\\(\\) expects array\\<array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\UnionKeyword\\>\\>, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\UnionKeyword\\) given\\.$#"
890+
count: 1
891+
path: src/Components/UnionKeyword.php
892+
893+
-
894+
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Components\\\\WithKeyword\\:\\:parse\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#"
895+
count: 1
896+
path: src/Components/WithKeyword.php
897+
863898
-
864899
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:escape\\(\\) has parameter \\$str with no value type specified in iterable type array\\.$#"
865900
count: 1

psalm-baseline.xml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="4.16.1@aa7e400908833b10c0333861f86cd48c510b60eb">
3-
<file src="src/Component.php">
4-
<MixedReturnStatement occurrences="1">
5-
<code>static::build($this)</code>
6-
</MixedReturnStatement>
7-
</file>
2+
<files psalm-version="4.17.0@6f4707aa41c9174353a6434bba3fc8840f981d9c">
83
<file src="src/Components/AlterOperation.php">
94
<DocblockTypeContradiction occurrences="1">
105
<code>$component-&gt;field !== ''</code>
@@ -31,6 +26,9 @@
3126
</RedundantConditionGivenDocblockType>
3227
</file>
3328
<file src="src/Components/Array2d.php">
29+
<InvalidArgument occurrences="1">
30+
<code>$this</code>
31+
</InvalidArgument>
3432
<InvalidReturnStatement occurrences="1">
3533
<code>$ret</code>
3634
</InvalidReturnStatement>
@@ -103,6 +101,9 @@
103101
</PossiblyInvalidOperand>
104102
</file>
105103
<file src="src/Components/Condition.php">
104+
<InvalidArgument occurrences="1">
105+
<code>$this</code>
106+
</InvalidArgument>
106107
<MixedArgument occurrences="1">
107108
<code>$token-&gt;value</code>
108109
</MixedArgument>
@@ -208,6 +209,9 @@
208209
</RedundantConditionGivenDocblockType>
209210
</file>
210211
<file src="src/Components/ExpressionArray.php">
212+
<InvalidArgument occurrences="1">
213+
<code>$this</code>
214+
</InvalidArgument>
211215
<InvalidReturnStatement occurrences="1">
212216
<code>$ret</code>
213217
</InvalidReturnStatement>
@@ -300,6 +304,9 @@
300304
<DocblockTypeContradiction occurrences="1">
301305
<code>''</code>
302306
</DocblockTypeContradiction>
307+
<InvalidArgument occurrences="1">
308+
<code>$this</code>
309+
</InvalidArgument>
303310
<MixedArrayOffset occurrences="4">
304311
<code>static::$JOINS[$token-&gt;keyword]</code>
305312
<code>static::$JOINS[$token-&gt;keyword]</code>
@@ -486,6 +493,9 @@
486493
</PossiblyNullPropertyAssignmentValue>
487494
</file>
488495
<file src="src/Components/ParameterDefinition.php">
496+
<InvalidArgument occurrences="1">
497+
<code>$this</code>
498+
</InvalidArgument>
489499
<MixedArgument occurrences="1">
490500
<code>$component-&gt;name</code>
491501
</MixedArgument>
@@ -585,6 +595,9 @@
585595
</RedundantCondition>
586596
</file>
587597
<file src="src/Components/UnionKeyword.php">
598+
<InvalidArgument occurrences="1">
599+
<code>$this</code>
600+
</InvalidArgument>
588601
<MoreSpecificImplementedParamType occurrences="1">
589602
<code>$component</code>
590603
</MoreSpecificImplementedParamType>

src/Component.php

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,41 @@
11
<?php
2-
/**
3-
* Defines a component that is later extended to parse specialized components or
4-
* keywords.
5-
*
6-
* There is a small difference between *Component and *Keyword classes: usually,
7-
* *Component parsers can be reused in multiple situations and *Keyword parsers
8-
* count on the *Component classes to do their job.
9-
*/
102

113
declare(strict_types=1);
124

135
namespace PhpMyAdmin\SqlParser;
146

15-
use Exception;
167
use Stringable;
178

189
/**
19-
* A component (of a statement) is a part of a statement that is common to
20-
* multiple query types.
10+
* A component (of a statement) is a part of a statement that is common to multiple query types.
11+
*
12+
* Defines a component that is later extended to parse specialized components or keywords.
13+
*
14+
* There is a small difference between *Component and *Keyword classes: usually, *Component parsers can be reused
15+
* in multiple situations and *Keyword parsers count on the *Component classes to do their job.
2116
*/
22-
abstract class Component implements Stringable
17+
interface Component extends Stringable
2318
{
2419
/**
25-
* Parses the tokens contained in the given list in the context of the given
26-
* parser.
20+
* Parses the tokens contained in the given list in the context of the given parser.
2721
*
2822
* @param Parser $parser the parser that serves as context
2923
* @param TokensList $list the list of tokens that are being parsed
3024
* @param array $options parameters for parsing
3125
*
3226
* @return mixed
33-
*
34-
* @throws Exception not implemented yet.
3527
*/
36-
public static function parse(
37-
Parser $parser,
38-
TokensList $list,
39-
array $options = []
40-
) {
41-
// This method should be abstract, but it can't be both static and
42-
// abstract.
43-
throw new Exception(Translator::gettext('Not implemented yet.'));
44-
}
28+
public static function parse(Parser $parser, TokensList $list, array $options = []);
4529

4630
/**
4731
* Builds the string representation of a component of this type.
4832
*
49-
* In other words, this function represents the inverse function of
50-
* `static::parse`.
33+
* In other words, this function represents the inverse function of {@see Component::parse()}.
5134
*
5235
* @param mixed $component the component to be built
5336
* @param array $options parameters for building
5437
*
5538
* @return mixed
56-
*
57-
* @throws Exception not implemented yet.
58-
*/
59-
public static function build($component, array $options = [])
60-
{
61-
// This method should be abstract, but it can't be both static and
62-
// abstract.
63-
throw new Exception(Translator::gettext('Not implemented yet.'));
64-
}
65-
66-
/**
67-
* Builds the string representation of a component of this type.
68-
*
69-
* @see static::build
70-
*
71-
* @return string
7239
*/
73-
public function __toString()
74-
{
75-
return static::build($this);
76-
}
40+
public static function build($component, array $options = []);
7741
}

src/Components/AlterOperation.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @final
2424
*/
25-
class AlterOperation extends Component
25+
class AlterOperation implements Component
2626
{
2727
/**
2828
* All database options.
@@ -429,4 +429,9 @@ private static function checkIfTokenQuotedSymbol($token)
429429
{
430430
return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
431431
}
432+
433+
public function __toString(): string
434+
{
435+
return static::build($this);
436+
}
432437
}

src/Components/Array2d.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @final
2323
*/
24-
class Array2d extends Component
24+
class Array2d implements Component
2525
{
2626
/**
2727
* @param Parser $parser the parser that serves as context
@@ -126,4 +126,9 @@ public static function build($component, array $options = [])
126126
{
127127
return ArrayObj::build($component);
128128
}
129+
130+
public function __toString(): string
131+
{
132+
return static::build($this);
133+
}
129134
}

src/Components/ArrayObj.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @final
2424
*/
25-
class ArrayObj extends Component
25+
class ArrayObj implements Component
2626
{
2727
/**
2828
* The array that contains the unprocessed value of each token.
@@ -181,4 +181,9 @@ public static function build($component, array $options = [])
181181

182182
return '(' . implode(', ', $component->values) . ')';
183183
}
184+
185+
public function __toString(): string
186+
{
187+
return static::build($this);
188+
}
184189
}

src/Components/CaseExpression.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*
2121
* @final
2222
*/
23-
class CaseExpression extends Component
23+
class CaseExpression implements Component
2424
{
2525
/**
2626
* The value to be compared.
@@ -303,4 +303,9 @@ public static function build($component, array $options = [])
303303

304304
return $ret;
305305
}
306+
307+
public function __toString(): string
308+
{
309+
return static::build($this);
310+
}
306311
}

src/Components/Condition.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @final
2424
*/
25-
class Condition extends Component
25+
class Condition implements Component
2626
{
2727
/**
2828
* Logical operators that can be used to delimit expressions.
@@ -239,4 +239,9 @@ public static function build($component, array $options = [])
239239

240240
return $component->expr;
241241
}
242+
243+
public function __toString(): string
244+
{
245+
return static::build($this);
246+
}
242247
}

src/Components/CreateDefinition.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
* @final
2828
*/
29-
class CreateDefinition extends Component
29+
class CreateDefinition implements Component
3030
{
3131
/**
3232
* All field options.
@@ -358,4 +358,9 @@ public static function build($component, array $options = [])
358358

359359
return trim($tmp);
360360
}
361+
362+
public function __toString(): string
363+
{
364+
return static::build($this);
365+
}
361366
}

src/Components/DataType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @final
2424
*/
25-
class DataType extends Component
25+
class DataType implements Component
2626
{
2727
/**
2828
* All data type options.
@@ -174,4 +174,9 @@ public static function build($component, array $options = [])
174174

175175
return trim($name . $parameters . ' ' . $component->options);
176176
}
177+
178+
public function __toString(): string
179+
{
180+
return static::build($this);
181+
}
177182
}

0 commit comments

Comments
 (0)