Skip to content

Commit f440917

Browse files
Merge pull request #646 from kamil-tekiela/Refactoring-Formatter
Refactoring formatter
2 parents 2c63257 + 6bc115d commit f440917

File tree

6 files changed

+255
-630
lines changed

6 files changed

+255
-630
lines changed

phpstan-baseline.neon

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,15 +1753,15 @@ parameters:
17531753
path: src/Utils/CLI.php
17541754

17551755
-
1756-
message: '#^Parameter \#1 \$query of static method PhpMyAdmin\\SqlParser\\Utils\\Formatter\:\:format\(\) expects string, string\|false given\.$#'
1756+
message: '#^Parameter \#1 \$str of class PhpMyAdmin\\SqlParser\\Lexer constructor expects PhpMyAdmin\\SqlParser\\UtfString\|string, string\|false given\.$#'
17571757
identifier: argument.type
1758-
count: 1
1758+
count: 2
17591759
path: src/Utils/CLI.php
17601760

17611761
-
1762-
message: '#^Parameter \#1 \$str of class PhpMyAdmin\\SqlParser\\Lexer constructor expects PhpMyAdmin\\SqlParser\\UtfString\|string, string\|false given\.$#'
1762+
message: '#^Parameter \$type of class PhpMyAdmin\\SqlParser\\Utils\\FormattingOptions constructor expects ''cli''\|''html''\|''text'', string\|false given\.$#'
17631763
identifier: argument.type
1764-
count: 2
1764+
count: 1
17651765
path: src/Utils/CLI.php
17661766

17671767
-
@@ -1770,52 +1770,10 @@ parameters:
17701770
count: 1
17711771
path: src/Utils/Error.php
17721772

1773-
-
1774-
message: '#^Argument of an invalid type array\<int, array\<string, int\|string\>\>\|bool\|string supplied for foreach, only iterables are supported\.$#'
1775-
identifier: foreach.nonIterable
1776-
count: 1
1777-
path: src/Utils/Formatter.php
1778-
1779-
-
1780-
message: '#^Binary operation "&" between int and int\|string results in an error\.$#'
1781-
identifier: binaryOp.invalid
1782-
count: 1
1783-
path: src/Utils/Formatter.php
1784-
1785-
-
1786-
message: '#^Binary operation "&\=" between array\<int, array\<string, int\|string\>\>\|bool\|string\|null and array\<int, array\<string, int\|string\>\>\|bool\|string\|null results in an error\.$#'
1787-
identifier: assignOp.invalid
1788-
count: 1
1789-
path: src/Utils/Formatter.php
1790-
1791-
-
1792-
message: '#^Binary operation "\." between array\<int, array\<string, int\|string\>\>\|bool\|string and string results in an error\.$#'
1793-
identifier: binaryOp.invalid
1794-
count: 1
1795-
path: src/Utils/Formatter.php
1796-
1797-
-
1798-
message: '#^Binary operation "\.\=" between string and mixed results in an error\.$#'
1799-
identifier: assignOp.invalid
1800-
count: 1
1801-
path: src/Utils/Formatter.php
1802-
1803-
-
1804-
message: '#^Call to function in_array\(\) requires parameter \#3 to be set\.$#'
1805-
identifier: function.strict
1806-
count: 1
1807-
path: src/Utils/Formatter.php
1808-
18091773
-
18101774
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
18111775
identifier: empty.notAllowed
1812-
count: 4
1813-
path: src/Utils/Formatter.php
1814-
1815-
-
1816-
message: '#^Method PhpMyAdmin\\SqlParser\\Utils\\Formatter\:\:getMergedOptions\(\) should return array\<string, array\<int, array\<string, int\|string\>\>\|bool\|string\> but returns array\<string, mixed\>\.$#'
1817-
identifier: return.type
1818-
count: 1
1776+
count: 3
18191777
path: src/Utils/Formatter.php
18201778

18211779
-
@@ -1824,30 +1782,12 @@ parameters:
18241782
count: 1
18251783
path: src/Utils/Formatter.php
18261784

1827-
-
1828-
message: '#^Only booleans are allowed in &&, array\<int, array\<string, int\|string\>\>\|bool\|string given on the left side\.$#'
1829-
identifier: booleanAnd.leftNotBoolean
1830-
count: 2
1831-
path: src/Utils/Formatter.php
1832-
1833-
-
1834-
message: '#^Only booleans are allowed in &&, array\<int, array\<string, int\|string\>\>\|bool\|string given on the right side\.$#'
1835-
identifier: booleanAnd.rightNotBoolean
1836-
count: 2
1837-
path: src/Utils/Formatter.php
1838-
18391785
-
18401786
message: '#^Only booleans are allowed in an if condition, bool\|int given\.$#'
18411787
identifier: if.condNotBoolean
18421788
count: 1
18431789
path: src/Utils/Formatter.php
18441790

1845-
-
1846-
message: '#^Only booleans are allowed in \|\|, array\<int, array\<string, int\|string\>\>\|bool\|string given on the right side\.$#'
1847-
identifier: booleanOr.rightNotBoolean
1848-
count: 1
1849-
path: src/Utils/Formatter.php
1850-
18511791
-
18521792
message: '#^Only booleans are allowed in \|\|, int\<0, 32\> given on the right side\.$#'
18531793
identifier: booleanOr.rightNotBoolean
@@ -1866,36 +1806,18 @@ parameters:
18661806
count: 2
18671807
path: src/Utils/Formatter.php
18681808

1869-
-
1870-
message: '#^Parameter \#1 \$string of function str_repeat expects string, array\<int, array\<string, int\|string\>\>\|bool\|string given\.$#'
1871-
identifier: argument.type
1872-
count: 1
1873-
path: src/Utils/Formatter.php
1874-
18751809
-
18761810
message: '#^Parameter \#1 \$string of method PhpMyAdmin\\SqlParser\\Utils\\Formatter\:\:escapeConsole\(\) expects string, mixed given\.$#'
18771811
identifier: argument.type
18781812
count: 4
18791813
path: src/Utils/Formatter.php
18801814

1881-
-
1882-
message: '#^Parameter \#2 \$newFormats of static method PhpMyAdmin\\SqlParser\\Utils\\Formatter\:\:mergeFormats\(\) expects array\<int, array\<string, int\|string\>\>, array\<int, array\<string, int\|string\>\>\|bool\|string given\.$#'
1883-
identifier: argument.type
1884-
count: 1
1885-
path: src/Utils/Formatter.php
1886-
18871815
-
18881816
message: '#^Possibly invalid array key type bool\|float\|int\|string\.$#'
18891817
identifier: offsetAccess.invalidOffset
18901818
count: 7
18911819
path: src/Utils/Formatter.php
18921820

1893-
-
1894-
message: '#^Trying to invoke int\<min, \-1\>\|int\<1, max\>\|non\-falsy\-string but it might not be a callable\.$#'
1895-
identifier: callable.nonCallable
1896-
count: 1
1897-
path: src/Utils/Formatter.php
1898-
18991821
-
19001822
message: '''
19011823
#^Access to deprecated property \$isDelete of class PhpMyAdmin\\SqlParser\\Utils\\StatementFlags\:
@@ -2730,7 +2652,7 @@ parameters:
27302652
-
27312653
message: '#^Dynamic call to static method PHPUnit\\Framework\\Assert\:\:assertEquals\(\)\.$#'
27322654
identifier: staticMethod.dynamicCall
2733-
count: 4
2655+
count: 3
27342656
path: tests/Utils/FormatterTest.php
27352657

27362658
-

psalm-baseline.xml

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="6.16.1@f1f5de594dc76faf8784e02d3dc4716c91c6f6ac">
2+
<files psalm-version="6.15.1@28dc127af1b5aecd52314f6f645bafc10d0e11f9">
33
<file src="src/Components/AlterOperation.php">
44
<PossiblyNullReference>
55
<code><![CDATA[has]]></code>
@@ -951,6 +951,9 @@
951951
</PossiblyNullOperand>
952952
</file>
953953
<file src="src/Utils/CLI.php">
954+
<ArgumentTypeCoercion>
955+
<code><![CDATA[$params['f']]]></code>
956+
</ArgumentTypeCoercion>
954957
<InvalidReturnStatement>
955958
<code><![CDATA[getopt($opt, $long)]]></code>
956959
</InvalidReturnStatement>
@@ -962,7 +965,7 @@
962965
</MixedArgumentTypeCoercion>
963966
<PossiblyFalseArgument>
964967
<code><![CDATA[$params['c']]]></code>
965-
<code><![CDATA[$params['q']]]></code>
968+
<code><![CDATA[$params['f']]]></code>
966969
<code><![CDATA[$params['q']]]></code>
967970
<code><![CDATA[$params['q']]]></code>
968971
</PossiblyFalseArgument>
@@ -1008,16 +1011,6 @@
10081011
<code><![CDATA[$text]]></code>
10091012
<code><![CDATA[$text]]></code>
10101013
</MixedArgument>
1011-
<MixedArrayAccess>
1012-
<code><![CDATA[$format['cli']]]></code>
1013-
<code><![CDATA[$format['cli']]]></code>
1014-
<code><![CDATA[$format['cli']]]></code>
1015-
<code><![CDATA[$format['flags']]]></code>
1016-
<code><![CDATA[$format['flags']]]></code>
1017-
<code><![CDATA[$format['function']]]></code>
1018-
<code><![CDATA[$format['html']]]></code>
1019-
<code><![CDATA[$format['type']]]></code>
1020-
</MixedArrayAccess>
10211014
<MixedArrayOffset>
10221015
<code><![CDATA[self::$inlineClauses[$lastClause]]]></code>
10231016
<code><![CDATA[self::$inlineClauses[$lastClause]]]></code>
@@ -1030,32 +1023,16 @@
10301023
</MixedArrayTypeCoercion>
10311024
<MixedAssignment>
10321025
<code><![CDATA[$blocksLineEndings[]]]></code>
1033-
<code><![CDATA[$format]]></code>
1034-
<code><![CDATA[$func]]></code>
10351026
<code><![CDATA[$lineEnded]]></code>
1036-
<code><![CDATA[$prev]]></code>
10371027
<code><![CDATA[$text]]></code>
10381028
</MixedAssignment>
1039-
<MixedFunctionCall>
1040-
<code><![CDATA[$func($text)]]></code>
1041-
</MixedFunctionCall>
10421029
<MixedOperand>
1043-
<code><![CDATA[$format['cli']]]></code>
1044-
<code><![CDATA[$format['flags']]]></code>
1045-
<code><![CDATA[$format['html']]]></code>
10461030
<code><![CDATA[$lineEnded]]></code>
10471031
</MixedOperand>
10481032
<MixedReturnStatement>
10491033
<code><![CDATA[$text]]></code>
10501034
<code><![CDATA[$text]]></code>
10511035
</MixedReturnStatement>
1052-
<PossiblyInvalidArgument>
1053-
<code><![CDATA[$options['formats']]]></code>
1054-
<code><![CDATA[$this->options['indentation']]]></code>
1055-
</PossiblyInvalidArgument>
1056-
<PossiblyInvalidIterator>
1057-
<code><![CDATA[$this->options['formats']]]></code>
1058-
</PossiblyInvalidIterator>
10591036
<PossiblyNullArrayOffset>
10601037
<code><![CDATA[Parser::KEYWORD_PARSERS]]></code>
10611038
<code><![CDATA[Parser::STATEMENT_PARSERS]]></code>
@@ -1068,11 +1045,6 @@
10681045
</PossiblyNullOperand>
10691046
<RiskyTruthyFalsyComparison>
10701047
<code><![CDATA[$lineEnded]]></code>
1071-
<code><![CDATA[$this->options['clause_newline']]]></code>
1072-
<code><![CDATA[$this->options['parts_newline']]]></code>
1073-
<code><![CDATA[$this->options['parts_newline']]]></code>
1074-
<code><![CDATA[$this->options['parts_newline']]]></code>
1075-
<code><![CDATA[$this->options['remove_comments']]]></code>
10761048
</RiskyTruthyFalsyComparison>
10771049
</file>
10781050
<file src="src/Utils/Query.php">

src/Utils/CLI.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,13 @@ public function runHighlight(): int
139139
Context::setMode(Context::SQL_MODE_ANSI_QUOTES);
140140
}
141141

142-
if (isset($params['q'])) {
142+
if (isset($params['q']) && $params['q'] !== false) {
143+
// $params['f'] is guaranteed to be set and valid at this point. @see parseHighlight()
143144
echo Formatter::format(
144145
$params['q'],
145-
['type' => $params['f']],
146+
new FormattingOptions(type: $params['f']),
146147
);
148+
147149
echo "\n";
148150

149151
return 0;

0 commit comments

Comments
 (0)