From 66b7ddad92d4a3d648ca18131aaeaf5b8f30af48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maur=C3=ADcio=20Meneghini=20Fauth?= Date: Mon, 3 Jan 2022 11:39:46 -0300 Subject: [PATCH] Change `Component` abstract class to an interface MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: MaurĂ­cio Meneghini Fauth --- phpstan-baseline.neon | 45 +++++++++++++++++--- psalm-baseline.xml | 25 ++++++++--- src/Component.php | 58 +++++--------------------- src/Components/AlterOperation.php | 7 +++- src/Components/Array2d.php | 7 +++- src/Components/ArrayObj.php | 7 +++- src/Components/CaseExpression.php | 7 +++- src/Components/Condition.php | 7 +++- src/Components/CreateDefinition.php | 7 +++- src/Components/DataType.php | 7 +++- src/Components/Expression.php | 7 +++- src/Components/ExpressionArray.php | 7 +++- src/Components/FunctionCall.php | 7 +++- src/Components/GroupKeyword.php | 7 +++- src/Components/IndexHint.php | 7 +++- src/Components/IntoKeyword.php | 7 +++- src/Components/JoinKeyword.php | 7 +++- src/Components/Key.php | 7 +++- src/Components/Limit.php | 7 +++- src/Components/LockExpression.php | 7 +++- src/Components/OptionsArray.php | 7 +++- src/Components/OrderKeyword.php | 7 +++- src/Components/ParameterDefinition.php | 7 +++- src/Components/PartitionDefinition.php | 7 +++- src/Components/Reference.php | 7 +++- src/Components/RenameOperation.php | 7 +++- src/Components/SetOperation.php | 7 +++- src/Components/UnionKeyword.php | 27 +++++++++++- src/Components/WithKeyword.php | 25 ++++++++++- tests/Components/ComponentTest.php | 32 -------------- 30 files changed, 264 insertions(+), 116 deletions(-) delete mode 100644 tests/Components/ComponentTest.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index bfcc1beef..c4719507a 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,10 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Component\\:\\:__toString\\(\\) should return string but returns mixed\\.$#" - count: 1 - path: src/Component.php - - message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Component\\:\\:build\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#" count: 1 @@ -105,6 +100,11 @@ parameters: count: 1 path: src/Components/Array2d.php + - + message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\Array2d\\:\\:build\\(\\) expects array\\, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Array2d\\) given\\.$#" + count: 1 + path: src/Components/Array2d.php + - message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Components\\\\ArrayObj\\:\\:__construct\\(\\) has parameter \\$raw with no value type specified in iterable type array\\.$#" count: 1 @@ -180,6 +180,11 @@ parameters: count: 1 path: src/Components/Condition.php + - + message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition\\:\\:build\\(\\) expects array\\, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition\\) given\\.$#" + count: 1 + path: src/Components/Condition.php + - message: "#^Parameter \\#1 \\$expr of class PhpMyAdmin\\\\SqlParser\\\\Components\\\\Condition constructor expects string\\|null, mixed given\\.$#" count: 1 @@ -340,6 +345,11 @@ parameters: count: 1 path: src/Components/ExpressionArray.php + - + message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\ExpressionArray\\:\\:build\\(\\) expects array\\, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ExpressionArray\\) given\\.$#" + count: 1 + path: src/Components/ExpressionArray.php + - message: "#^Parameter \\#1 \\$string of function strlen expects string, string\\|null given\\.$#" count: 1 @@ -500,6 +510,11 @@ parameters: count: 1 path: src/Components/JoinKeyword.php + - + message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:build\\(\\) expects array\\, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\) given\\.$#" + count: 1 + path: src/Components/JoinKeyword.php + - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\JoinKeyword\\:\\:\\$JOINS type has no value type specified in iterable type array\\.$#" count: 1 @@ -690,6 +705,11 @@ parameters: count: 1 path: src/Components/ParameterDefinition.php + - + message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:build\\(\\) expects array\\, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\) given\\.$#" + count: 1 + path: src/Components/ParameterDefinition.php + - message: "#^Property PhpMyAdmin\\\\SqlParser\\\\Components\\\\ParameterDefinition\\:\\:\\$inOut \\(string\\) does not accept string\\|null\\.$#" count: 1 @@ -860,6 +880,21 @@ parameters: count: 1 path: src/Components/UnionKeyword.php + - + message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Components\\\\UnionKeyword\\:\\:parse\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#" + count: 1 + path: src/Components/UnionKeyword.php + + - + message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\UnionKeyword\\:\\:build\\(\\) expects array\\\\>, \\$this\\(PhpMyAdmin\\\\SqlParser\\\\Components\\\\UnionKeyword\\) given\\.$#" + count: 1 + path: src/Components/UnionKeyword.php + + - + message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Components\\\\WithKeyword\\:\\:parse\\(\\) has parameter \\$options with no value type specified in iterable type array\\.$#" + count: 1 + path: src/Components/WithKeyword.php + - message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Context\\:\\:escape\\(\\) has parameter \\$str with no value type specified in iterable type array\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 0478efe0d..8acc25a9b 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,10 +1,5 @@ - - - - static::build($this) - - + $component->field !== '' @@ -31,6 +26,9 @@ + + $this + $ret @@ -103,6 +101,9 @@ + + $this + $token->value @@ -208,6 +209,9 @@ + + $this + $ret @@ -300,6 +304,9 @@ '' + + $this + static::$JOINS[$token->keyword] static::$JOINS[$token->keyword] @@ -486,6 +493,9 @@ + + $this + $component->name @@ -585,6 +595,9 @@ + + $this + $component diff --git a/src/Component.php b/src/Component.php index cab499b52..e7c504363 100644 --- a/src/Component.php +++ b/src/Component.php @@ -1,77 +1,41 @@ type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/Array2d.php b/src/Components/Array2d.php index 299bfef49..b5757a67c 100644 --- a/src/Components/Array2d.php +++ b/src/Components/Array2d.php @@ -21,7 +21,7 @@ * * @final */ -class Array2d extends Component +class Array2d implements Component { /** * @param Parser $parser the parser that serves as context @@ -126,4 +126,9 @@ public static function build($component, array $options = []) { return ArrayObj::build($component); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/ArrayObj.php b/src/Components/ArrayObj.php index a6099bc96..b7bc99621 100644 --- a/src/Components/ArrayObj.php +++ b/src/Components/ArrayObj.php @@ -22,7 +22,7 @@ * * @final */ -class ArrayObj extends Component +class ArrayObj implements Component { /** * The array that contains the unprocessed value of each token. @@ -181,4 +181,9 @@ public static function build($component, array $options = []) return '(' . implode(', ', $component->values) . ')'; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/CaseExpression.php b/src/Components/CaseExpression.php index 33839a646..9f5b08f96 100644 --- a/src/Components/CaseExpression.php +++ b/src/Components/CaseExpression.php @@ -20,7 +20,7 @@ * * @final */ -class CaseExpression extends Component +class CaseExpression implements Component { /** * The value to be compared. @@ -303,4 +303,9 @@ public static function build($component, array $options = []) return $ret; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/Condition.php b/src/Components/Condition.php index 11a6c9fb7..dddbac7c8 100644 --- a/src/Components/Condition.php +++ b/src/Components/Condition.php @@ -22,7 +22,7 @@ * * @final */ -class Condition extends Component +class Condition implements Component { /** * Logical operators that can be used to delimit expressions. @@ -239,4 +239,9 @@ public static function build($component, array $options = []) return $component->expr; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/CreateDefinition.php b/src/Components/CreateDefinition.php index 384ba65d4..1c4cc2047 100644 --- a/src/Components/CreateDefinition.php +++ b/src/Components/CreateDefinition.php @@ -26,7 +26,7 @@ * * @final */ -class CreateDefinition extends Component +class CreateDefinition implements Component { /** * All field options. @@ -358,4 +358,9 @@ public static function build($component, array $options = []) return trim($tmp); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/DataType.php b/src/Components/DataType.php index c92311a33..193118ee3 100644 --- a/src/Components/DataType.php +++ b/src/Components/DataType.php @@ -22,7 +22,7 @@ * * @final */ -class DataType extends Component +class DataType implements Component { /** * All data type options. @@ -174,4 +174,9 @@ public static function build($component, array $options = []) return trim($name . $parameters . ' ' . $component->options); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/Expression.php b/src/Components/Expression.php index 77759fc3d..127c56828 100644 --- a/src/Components/Expression.php +++ b/src/Components/Expression.php @@ -26,7 +26,7 @@ * * @final */ -class Expression extends Component +class Expression implements Component { /** * List of allowed reserved keywords in expressions. @@ -466,4 +466,9 @@ public static function build($component, array $options = []) return $ret; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/ExpressionArray.php b/src/Components/ExpressionArray.php index dc8d8becc..abbc15ac6 100644 --- a/src/Components/ExpressionArray.php +++ b/src/Components/ExpressionArray.php @@ -25,7 +25,7 @@ * * @final */ -class ExpressionArray extends Component +class ExpressionArray implements Component { /** * @param Parser $parser the parser that serves as context @@ -139,4 +139,9 @@ public static function build($component, array $options = []) return implode(', ', $ret); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/FunctionCall.php b/src/Components/FunctionCall.php index 1cae94255..64fcf8965 100644 --- a/src/Components/FunctionCall.php +++ b/src/Components/FunctionCall.php @@ -19,7 +19,7 @@ * * @final */ -class FunctionCall extends Component +class FunctionCall implements Component { /** * The name of this function. @@ -114,4 +114,9 @@ public static function build($component, array $options = []) { return $component->name . $component->parameters; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/GroupKeyword.php b/src/Components/GroupKeyword.php index 65082b6e2..e7c0099a2 100644 --- a/src/Components/GroupKeyword.php +++ b/src/Components/GroupKeyword.php @@ -21,7 +21,7 @@ * * @final */ -class GroupKeyword extends Component +class GroupKeyword implements Component { /** @var mixed */ public $type; @@ -130,4 +130,9 @@ public static function build($component, array $options = []) return trim((string) $component->expr); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/IndexHint.php b/src/Components/IndexHint.php index fbd7a9d99..70f628524 100644 --- a/src/Components/IndexHint.php +++ b/src/Components/IndexHint.php @@ -20,7 +20,7 @@ * * @final */ -class IndexHint extends Component +class IndexHint implements Component { /** * The type of hint (USE/FORCE/IGNORE) @@ -207,4 +207,9 @@ public static function build($component, array $options = []) return $ret . ExpressionArray::build($component->indexes); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/IntoKeyword.php b/src/Components/IntoKeyword.php index 6782a0bc3..9879d5ee2 100644 --- a/src/Components/IntoKeyword.php +++ b/src/Components/IntoKeyword.php @@ -20,7 +20,7 @@ * * @final */ -class IntoKeyword extends Component +class IntoKeyword implements Component { /** * FIELDS/COLUMNS Options for `SELECT...INTO` statements. @@ -296,4 +296,9 @@ public static function build($component, array $options = []) return $ret; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/JoinKeyword.php b/src/Components/JoinKeyword.php index a2515cf35..52d40ce21 100644 --- a/src/Components/JoinKeyword.php +++ b/src/Components/JoinKeyword.php @@ -20,7 +20,7 @@ * * @final */ -class JoinKeyword extends Component +class JoinKeyword implements Component { /** * Types of join. @@ -221,4 +221,9 @@ public static function build($component, array $options = []) return implode(' ', $ret); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/Key.php b/src/Components/Key.php index f23e29875..f623fd5e9 100644 --- a/src/Components/Key.php +++ b/src/Components/Key.php @@ -23,7 +23,7 @@ * * @final */ -class Key extends Component +class Key implements Component { /** * All key options. @@ -305,4 +305,9 @@ public static function build($component, array $options = []) return trim($ret); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/Limit.php b/src/Components/Limit.php index 7f8ddc268..a7576d98c 100644 --- a/src/Components/Limit.php +++ b/src/Components/Limit.php @@ -17,7 +17,7 @@ * * @final */ -class Limit extends Component +class Limit implements Component { /** * The number of rows skipped. @@ -123,4 +123,9 @@ public static function build($component, array $options = []) { return $component->offset . ', ' . $component->rowCount; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/LockExpression.php b/src/Components/LockExpression.php index 8ce30e8de..892074e8a 100644 --- a/src/Components/LockExpression.php +++ b/src/Components/LockExpression.php @@ -20,7 +20,7 @@ * * @final */ -class LockExpression extends Component +class LockExpression implements Component { /** * The table to be locked. @@ -206,4 +206,9 @@ private static function parseLockType(Parser $parser, TokensList $list) return $lockType; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/OptionsArray.php b/src/Components/OptionsArray.php index 5e477f1cd..daaf97f4e 100644 --- a/src/Components/OptionsArray.php +++ b/src/Components/OptionsArray.php @@ -27,7 +27,7 @@ * * @final */ -class OptionsArray extends Component +class OptionsArray implements Component { /** * ArrayObj of selected options. @@ -383,4 +383,9 @@ public function isEmpty() { return empty($this->options); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/OrderKeyword.php b/src/Components/OrderKeyword.php index bd475cb29..37171ecfa 100644 --- a/src/Components/OrderKeyword.php +++ b/src/Components/OrderKeyword.php @@ -20,7 +20,7 @@ * * @final */ -class OrderKeyword extends Component +class OrderKeyword implements Component { /** * The expression that is used for ordering. @@ -135,4 +135,9 @@ public static function build($component, array $options = []) return $component->expr . ' ' . $component->type; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/ParameterDefinition.php b/src/Components/ParameterDefinition.php index 807266938..8feadb1d1 100644 --- a/src/Components/ParameterDefinition.php +++ b/src/Components/ParameterDefinition.php @@ -22,7 +22,7 @@ * * @final */ -class ParameterDefinition extends Component +class ParameterDefinition implements Component { /** * The name of the new column. @@ -168,4 +168,9 @@ public static function build($component, array $options = []) $tmp . Context::escape($component->name) . ' ' . $component->type ); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/PartitionDefinition.php b/src/Components/PartitionDefinition.php index 68e6c6538..30064549d 100644 --- a/src/Components/PartitionDefinition.php +++ b/src/Components/PartitionDefinition.php @@ -25,7 +25,7 @@ * * @final */ -class PartitionDefinition extends Component +class PartitionDefinition implements Component { /** * All field options. @@ -254,4 +254,9 @@ public static function build($component, array $options = []) . $component->options . $subpartitions ); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/Reference.php b/src/Components/Reference.php index d961bbec9..abc261463 100644 --- a/src/Components/Reference.php +++ b/src/Components/Reference.php @@ -21,7 +21,7 @@ * * @final */ -class Reference extends Component +class Reference implements Component { /** * All references options. @@ -157,4 +157,9 @@ public static function build($component, array $options = []) . $component->options ); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/RenameOperation.php b/src/Components/RenameOperation.php index 54b7b48ec..fcfa89097 100644 --- a/src/Components/RenameOperation.php +++ b/src/Components/RenameOperation.php @@ -20,7 +20,7 @@ * * @final */ -class RenameOperation extends Component +class RenameOperation implements Component { /** * The old table name. @@ -167,4 +167,9 @@ public static function build($component, array $options = []) return $component->old . ' TO ' . $component->new; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/SetOperation.php b/src/Components/SetOperation.php index 917d5146c..3f4fbf0d5 100644 --- a/src/Components/SetOperation.php +++ b/src/Components/SetOperation.php @@ -21,7 +21,7 @@ * * @final */ -class SetOperation extends Component +class SetOperation implements Component { /** * The name of the column that is being updated. @@ -158,4 +158,9 @@ public static function build($component, array $options = []) return $component->column . ' = ' . $component->value; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/UnionKeyword.php b/src/Components/UnionKeyword.php index 68d156359..899b83c7c 100644 --- a/src/Components/UnionKeyword.php +++ b/src/Components/UnionKeyword.php @@ -8,6 +8,10 @@ namespace PhpMyAdmin\SqlParser\Components; use PhpMyAdmin\SqlParser\Component; +use PhpMyAdmin\SqlParser\Parser; +use PhpMyAdmin\SqlParser\TokensList; +use PhpMyAdmin\SqlParser\Translator; +use RuntimeException; use function implode; @@ -16,8 +20,24 @@ * * @final */ -class UnionKeyword extends Component +class UnionKeyword implements Component { + /** + * Parses the tokens contained in the given list in the context of the given parser. + * + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing + * + * @return mixed + * + * @throws RuntimeException not implemented yet. + */ + public static function parse(Parser $parser, TokensList $list, array $options = []) + { + throw new RuntimeException(Translator::gettext('Not implemented yet.')); + } + /** * @param array $component the component to be built * @param array $options parameters for building @@ -33,4 +53,9 @@ public static function build($component, array $options = []) return implode(' ', $tmp); } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/src/Components/WithKeyword.php b/src/Components/WithKeyword.php index 3b9a76c74..c81b60f9b 100644 --- a/src/Components/WithKeyword.php +++ b/src/Components/WithKeyword.php @@ -9,6 +9,8 @@ use PhpMyAdmin\SqlParser\Component; use PhpMyAdmin\SqlParser\Parser; +use PhpMyAdmin\SqlParser\TokensList; +use PhpMyAdmin\SqlParser\Translator; use RuntimeException; /** @@ -16,7 +18,7 @@ * * @final */ -final class WithKeyword extends Component +final class WithKeyword implements Component { /** @var string */ public $name; @@ -32,6 +34,22 @@ public function __construct(string $name) $this->name = $name; } + /** + * Parses the tokens contained in the given list in the context of the given parser. + * + * @param Parser $parser the parser that serves as context + * @param TokensList $list the list of tokens that are being parsed + * @param array $options parameters for parsing + * + * @return mixed + * + * @throws RuntimeException not implemented yet. + */ + public static function parse(Parser $parser, TokensList $list, array $options = []) + { + throw new RuntimeException(Translator::gettext('Not implemented yet.')); + } + /** * @param WithKeyword $component * @param mixed[] $options @@ -64,4 +82,9 @@ public static function build($component, array $options = []) return $str; } + + public function __toString(): string + { + return static::build($this); + } } diff --git a/tests/Components/ComponentTest.php b/tests/Components/ComponentTest.php deleted file mode 100644 index 1c9462ebc..000000000 --- a/tests/Components/ComponentTest.php +++ /dev/null @@ -1,32 +0,0 @@ -expectExceptionMessage('Not implemented yet.'); - $this->expectException(Throwable::class); - Component::parse(new Parser(), new TokensList()); - } - - public function testBuild(): void - { - $this->expectExceptionMessage('Not implemented yet.'); - $this->expectException(Throwable::class); - Component::build(null); - } -}