Skip to content

Commit 5e02807

Browse files
committed
Refactoring Formatter
Signed-off-by: Kamil Tekiela <tekiela246@gmail.com>
1 parent 960cc8e commit 5e02807

File tree

6 files changed

+101
-558
lines changed

6 files changed

+101
-558
lines changed

phpstan-baseline.neon

Lines changed: 7 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,15 +1813,15 @@ parameters:
18131813
path: src/Utils/CLI.php
18141814

18151815
-
1816-
message: '#^Parameter \#1 \$query of static method PhpMyAdmin\\SqlParser\\Utils\\Formatter\:\:format\(\) expects string, string\|false given\.$#'
1816+
message: '#^Parameter \#1 \$str of class PhpMyAdmin\\SqlParser\\Lexer constructor expects PhpMyAdmin\\SqlParser\\UtfString\|string, string\|false given\.$#'
18171817
identifier: argument.type
1818-
count: 1
1818+
count: 2
18191819
path: src/Utils/CLI.php
18201820

18211821
-
1822-
message: '#^Parameter \#1 \$str of class PhpMyAdmin\\SqlParser\\Lexer constructor expects PhpMyAdmin\\SqlParser\\UtfString\|string, string\|false given\.$#'
1822+
message: '#^Parameter \$type of class PhpMyAdmin\\SqlParser\\Utils\\FormattingOptions constructor expects ''cli''\|''html''\|''text'', string\|false given\.$#'
18231823
identifier: argument.type
1824-
count: 2
1824+
count: 1
18251825
path: src/Utils/CLI.php
18261826

18271827
-
@@ -1830,52 +1830,10 @@ parameters:
18301830
count: 1
18311831
path: src/Utils/Error.php
18321832

1833-
-
1834-
message: '#^Argument of an invalid type array\<int, array\<string, int\|string\>\>\|bool\|string supplied for foreach, only iterables are supported\.$#'
1835-
identifier: foreach.nonIterable
1836-
count: 1
1837-
path: src/Utils/Formatter.php
1838-
1839-
-
1840-
message: '#^Binary operation "&" between int and int\|string results in an error\.$#'
1841-
identifier: binaryOp.invalid
1842-
count: 1
1843-
path: src/Utils/Formatter.php
1844-
1845-
-
1846-
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\.$#'
1847-
identifier: assignOp.invalid
1848-
count: 1
1849-
path: src/Utils/Formatter.php
1850-
1851-
-
1852-
message: '#^Binary operation "\." between array\<int, array\<string, int\|string\>\>\|bool\|string and string results in an error\.$#'
1853-
identifier: binaryOp.invalid
1854-
count: 1
1855-
path: src/Utils/Formatter.php
1856-
1857-
-
1858-
message: '#^Binary operation "\.\=" between string and mixed results in an error\.$#'
1859-
identifier: assignOp.invalid
1860-
count: 1
1861-
path: src/Utils/Formatter.php
1862-
1863-
-
1864-
message: '#^Call to function in_array\(\) requires parameter \#3 to be set\.$#'
1865-
identifier: function.strict
1866-
count: 1
1867-
path: src/Utils/Formatter.php
1868-
18691833
-
18701834
message: '#^Construct empty\(\) is not allowed\. Use more strict comparison\.$#'
18711835
identifier: empty.notAllowed
1872-
count: 4
1873-
path: src/Utils/Formatter.php
1874-
1875-
-
1876-
message: '#^Method PhpMyAdmin\\SqlParser\\Utils\\Formatter\:\:getMergedOptions\(\) should return array\<string, array\<int, array\<string, int\|string\>\>\|bool\|string\> but returns array\<string, mixed\>\.$#'
1877-
identifier: return.type
1878-
count: 1
1836+
count: 3
18791837
path: src/Utils/Formatter.php
18801838

18811839
-
@@ -1884,30 +1842,12 @@ parameters:
18841842
count: 1
18851843
path: src/Utils/Formatter.php
18861844

1887-
-
1888-
message: '#^Only booleans are allowed in &&, array\<int, array\<string, int\|string\>\>\|bool\|string given on the left side\.$#'
1889-
identifier: booleanAnd.leftNotBoolean
1890-
count: 2
1891-
path: src/Utils/Formatter.php
1892-
1893-
-
1894-
message: '#^Only booleans are allowed in &&, array\<int, array\<string, int\|string\>\>\|bool\|string given on the right side\.$#'
1895-
identifier: booleanAnd.rightNotBoolean
1896-
count: 2
1897-
path: src/Utils/Formatter.php
1898-
18991845
-
19001846
message: '#^Only booleans are allowed in an if condition, bool\|int given\.$#'
19011847
identifier: if.condNotBoolean
19021848
count: 1
19031849
path: src/Utils/Formatter.php
19041850

1905-
-
1906-
message: '#^Only booleans are allowed in \|\|, array\<int, array\<string, int\|string\>\>\|bool\|string given on the right side\.$#'
1907-
identifier: booleanOr.rightNotBoolean
1908-
count: 1
1909-
path: src/Utils/Formatter.php
1910-
19111851
-
19121852
message: '#^Only booleans are allowed in \|\|, int\<0, 32\> given on the right side\.$#'
19131853
identifier: booleanOr.rightNotBoolean
@@ -1926,26 +1866,14 @@ parameters:
19261866
count: 2
19271867
path: src/Utils/Formatter.php
19281868

1929-
-
1930-
message: '#^Parameter \#1 \$string of function str_repeat expects string, array\<int, array\<string, int\|string\>\>\|bool\|string given\.$#'
1931-
identifier: argument.type
1932-
count: 1
1933-
path: src/Utils/Formatter.php
1934-
19351869
-
19361870
message: '#^Parameter \#1 \$string of method PhpMyAdmin\\SqlParser\\Utils\\Formatter\:\:escapeConsole\(\) expects string, mixed given\.$#'
19371871
identifier: argument.type
19381872
count: 4
19391873
path: src/Utils/Formatter.php
19401874

19411875
-
1942-
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\.$#'
1943-
identifier: argument.type
1944-
count: 1
1945-
path: src/Utils/Formatter.php
1946-
1947-
-
1948-
message: '#^Trying to invoke int\<min, \-1\>\|int\<1, max\>\|non\-falsy\-string but it might not be a callable\.$#'
1876+
message: '#^Trying to invoke non\-empty\-string but it might not be a callable\.$#'
19491877
identifier: callable.nonCallable
19501878
count: 1
19511879
path: src/Utils/Formatter.php
@@ -2796,7 +2724,7 @@ parameters:
27962724
-
27972725
message: '#^Dynamic call to static method PHPUnit\\Framework\\Assert\:\:assertEquals\(\)\.$#'
27982726
identifier: staticMethod.dynamicCall
2799-
count: 4
2727+
count: 3
28002728
path: tests/Utils/FormatterTest.php
28012729

28022730
-

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.13.0@70cdf647255a1362b426bb0f522a85817b8c791c">
2+
<files psalm-version="6.10.3@90b5b9f5e7c8e441b191d3c82c58214753d7c7c1">
33
<file src="src/Components/AlterOperation.php">
44
<PossiblyNullReference>
55
<code><![CDATA[has]]></code>
@@ -942,6 +942,9 @@
942942
</PossiblyNullOperand>
943943
</file>
944944
<file src="src/Utils/CLI.php">
945+
<ArgumentTypeCoercion>
946+
<code><![CDATA[$params['f']]]></code>
947+
</ArgumentTypeCoercion>
945948
<InvalidReturnStatement>
946949
<code><![CDATA[getopt($opt, $long)]]></code>
947950
</InvalidReturnStatement>
@@ -953,7 +956,7 @@
953956
</MixedArgumentTypeCoercion>
954957
<PossiblyFalseArgument>
955958
<code><![CDATA[$params['c']]]></code>
956-
<code><![CDATA[$params['q']]]></code>
959+
<code><![CDATA[$params['f']]]></code>
957960
<code><![CDATA[$params['q']]]></code>
958961
<code><![CDATA[$params['q']]]></code>
959962
</PossiblyFalseArgument>
@@ -999,16 +1002,6 @@
9991002
<code><![CDATA[$text]]></code>
10001003
<code><![CDATA[$text]]></code>
10011004
</MixedArgument>
1002-
<MixedArrayAccess>
1003-
<code><![CDATA[$format['cli']]]></code>
1004-
<code><![CDATA[$format['cli']]]></code>
1005-
<code><![CDATA[$format['cli']]]></code>
1006-
<code><![CDATA[$format['flags']]]></code>
1007-
<code><![CDATA[$format['flags']]]></code>
1008-
<code><![CDATA[$format['function']]]></code>
1009-
<code><![CDATA[$format['html']]]></code>
1010-
<code><![CDATA[$format['type']]]></code>
1011-
</MixedArrayAccess>
10121005
<MixedArrayOffset>
10131006
<code><![CDATA[self::$inlineClauses[$lastClause]]]></code>
10141007
<code><![CDATA[self::$inlineClauses[$lastClause]]]></code>
@@ -1021,32 +1014,16 @@
10211014
</MixedArrayTypeCoercion>
10221015
<MixedAssignment>
10231016
<code><![CDATA[$blocksLineEndings[]]]></code>
1024-
<code><![CDATA[$format]]></code>
1025-
<code><![CDATA[$func]]></code>
10261017
<code><![CDATA[$lineEnded]]></code>
1027-
<code><![CDATA[$prev]]></code>
10281018
<code><![CDATA[$text]]></code>
10291019
</MixedAssignment>
1030-
<MixedFunctionCall>
1031-
<code><![CDATA[$func($text)]]></code>
1032-
</MixedFunctionCall>
10331020
<MixedOperand>
1034-
<code><![CDATA[$format['cli']]]></code>
1035-
<code><![CDATA[$format['flags']]]></code>
1036-
<code><![CDATA[$format['html']]]></code>
10371021
<code><![CDATA[$lineEnded]]></code>
10381022
</MixedOperand>
10391023
<MixedReturnStatement>
10401024
<code><![CDATA[$text]]></code>
10411025
<code><![CDATA[$text]]></code>
10421026
</MixedReturnStatement>
1043-
<PossiblyInvalidArgument>
1044-
<code><![CDATA[$options['formats']]]></code>
1045-
<code><![CDATA[$this->options['indentation']]]></code>
1046-
</PossiblyInvalidArgument>
1047-
<PossiblyInvalidIterator>
1048-
<code><![CDATA[$this->options['formats']]]></code>
1049-
</PossiblyInvalidIterator>
10501027
<PossiblyNullOperand>
10511028
<code><![CDATA[$indent]]></code>
10521029
<code><![CDATA[$indent]]></code>
@@ -1055,11 +1032,6 @@
10551032
</PossiblyNullOperand>
10561033
<RiskyTruthyFalsyComparison>
10571034
<code><![CDATA[$lineEnded]]></code>
1058-
<code><![CDATA[$this->options['clause_newline']]]></code>
1059-
<code><![CDATA[$this->options['parts_newline']]]></code>
1060-
<code><![CDATA[$this->options['parts_newline']]]></code>
1061-
<code><![CDATA[$this->options['parts_newline']]]></code>
1062-
<code><![CDATA[$this->options['remove_comments']]]></code>
10631035
</RiskyTruthyFalsyComparison>
10641036
</file>
10651037
<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)