Skip to content

Commit 03e2a50

Browse files
committed
Add phpstan-strict-rules; fix bunch of bugs; some more simplifications in TokenList
1 parent d0e84f1 commit 03e2a50

File tree

67 files changed

+254
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+254
-287
lines changed

build/PhpStan/TokenListDynamicReturnTypeExtension.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

build/PhpStan/phpstan.neon

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
includes:
2+
- ../../vendor/phpstan/phpstan-strict-rules/rules.neon
13

24
parameters:
35
level: 8
@@ -17,19 +19,8 @@ parameters:
1719
- '~Property SqlFtw\\Sql\\Ddl\\DataType::\$(size|values) \(array<(int|string)>(\|int)?\|null\) does not accept~'
1820
- '~Parameter #1 \$strings of method SqlFtw\\Formatter\\Formatter::formatStringList\(\) expects array<string>, array<int\|string> given.~'
1921

20-
# end() on non-empty array
21-
#- '~Method .*Reflection::getLastCommand\(\) should return .*Command but returns .*Command\|false~'
22-
23-
# bullshit
24-
- '~Method SqlFtw\\Parser\\Dal\\UserCommandsParser::parseResourceOptions\(\) never returns array<SqlFtw\\Sql\\Dal\\User\\UserResourceOption> so it can be removed from the return type~'
2522

2623
earlyTerminatingMethodCalls:
2724
SqlFtw\Parser\TokenList:
2825
- expected
2926
- expectedAnyKeyword
30-
31-
services:
32-
-
33-
class: SqlFtw\PhpStan\TokenListDynamicReturnTypeExtension
34-
tags:
35-
- phpstan.broker.dynamicMethodReturnTypeExtension

build/spell-checker/custom.dic

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ destructor
4343
instantiable
4444
introducer
4545
leakproof
46+
matchers
4647
ok
4748
ordinality
4849
preload

composer.json

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
"dogma/dogma": "^0.2.10"
1616
},
1717
"require-dev": {
18-
"dogma/dogma-dev": "0.1.24"
18+
"dogma/dogma-dev": "0.1.25",
19+
"phpstan/phpstan": "1.4.6",
20+
"phpstan/phpstan-strict-rules": "^1.0"
1921
},
2022
"autoload": {
2123
"classmap": ["sources", "tests", "build"]
@@ -56,14 +58,14 @@
5658
"@phpcs:tests"
5759
],
5860

59-
"tests:run": "php vendor/nette/tester/src/tester tests -c tests --colors 1",
61+
"tests:run": "php vendor/nette/tester/src/tester tests -C --colors 1",
6062
"tests:all": [
61-
"php81 vendor/nette/tester/src/tester tests -c tests --colors 1",
62-
"php80 vendor/nette/tester/src/tester tests -c tests --colors 1",
63-
"php74 vendor/nette/tester/src/tester tests -c tests --colors 1",
64-
"php73 vendor/nette/tester/src/tester tests -c tests --colors 1",
65-
"php72 vendor/nette/tester/src/tester tests -c tests --colors 1",
66-
"php71 vendor/nette/tester/src/tester tests -c tests --colors 1"
63+
"php81 vendor/nette/tester/src/tester tests -C --colors 1",
64+
"php80 vendor/nette/tester/src/tester tests -C --colors 1",
65+
"php74 vendor/nette/tester/src/tester tests -C --colors 1",
66+
"php73 vendor/nette/tester/src/tester tests -C --colors 1",
67+
"php72 vendor/nette/tester/src/tester tests -C --colors 1",
68+
"php71 vendor/nette/tester/src/tester tests -C --colors 1"
6769
],
6870
"tests:coverage": "php vendor/nette/tester/src/tester tests -c tests --colors 1 -p phpdbg --coverage tests/coverage.html --coverage-src sources",
6971

sources/Parser/Dal/CharsetCommandsParser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function parseSetNames(TokenList $tokenList): SetNamesCommand
5050
{
5151
$tokenList->expectKeywords(Keyword::SET, Keyword::NAMES);
5252
$charset = $collation = null;
53-
if ($tokenList->hasKeyword(Keyword::DEFAULT) === null) {
53+
if (!$tokenList->hasKeyword(Keyword::DEFAULT)) {
5454
$charset = Charset::get($tokenList->expectNameOrString());
55-
if ($tokenList->hasKeyword(Keyword::COLLATE) !== null) {
55+
if ($tokenList->hasKeyword(Keyword::COLLATE)) {
5656
$collation = Collation::get($tokenList->expectNameOrString());
5757
}
5858
}

sources/Parser/Dal/ReplicationCommandsParser.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,6 @@ public function parseStartSlave(TokenList $tokenList): StartSlaveCommand
301301
$tokenList->expectKeywords(Keyword::START, Keyword::SLAVE);
302302

303303
$threadTypes = null;
304-
/** @var ReplicationThreadType $threadType|null */
305304
$threadType = $tokenList->getKeywordEnum(ReplicationThreadType::class);
306305
if ($threadType !== null) {
307306
$threadTypes = [$threadType];
@@ -413,10 +412,10 @@ private function parseGtidSet(TokenList $tokenList)
413412
$tokenList->expect(TokenType::DOUBLE_COLON);
414413
do {
415414
$start = $tokenList->expectInt();
416-
if ($tokenList->getOperator(Operator::MINUS)) {
415+
if ($tokenList->hasOperator(Operator::MINUS)) {
417416
$end = $tokenList->expectInt();
418417
// phpcs:ignore
419-
} elseif ($end = $tokenList->getInt()) {
418+
} elseif (($end = $tokenList->getInt()) !== null) {
420419
// todo: lexer returns "10-20" as tokens of int and negative int :/
421420
$end = abs($end);
422421
}

sources/Parser/Dal/SetCommandParser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use SqlFtw\Sql\Dal\Set\SetAssignment;
1717
use SqlFtw\Sql\Dal\Set\SetCommand;
1818
use SqlFtw\Sql\Dal\SystemVariable;
19+
use SqlFtw\Sql\Expression\Operator;
1920
use SqlFtw\Sql\Keyword;
2021
use SqlFtw\Sql\Scope;
2122
use function strpos;
@@ -93,6 +94,7 @@ public function parseSet(TokenList $tokenList): SetCommand
9394
}
9495
}
9596

97+
$tokenList->expectOperator(Operator::EQUAL);
9698
$expression = $this->expressionParser->parseExpression($tokenList);
9799

98100
$assignments[] = new SetAssignment($variable, $expression, $scope);

sources/Parser/Dal/ShowCommandsParser.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
122122
$like = $where = null;
123123
if ($tokenList->hasKeyword(Keyword::LIKE)) {
124124
$like = $tokenList->expectString();
125-
} elseif ($tokenList->expectKeyword(Keyword::WHERE)) {
125+
} elseif ($tokenList->hasKeyword(Keyword::WHERE)) {
126126
$where = $this->expressionParser->parseExpression($tokenList);
127127
}
128128
$tokenList->expectEnd();
@@ -133,7 +133,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
133133
$like = $where = null;
134134
if ($tokenList->hasKeyword(Keyword::LIKE)) {
135135
$like = $tokenList->expectString();
136-
} elseif ($tokenList->expectKeyword(Keyword::WHERE)) {
136+
} elseif ($tokenList->hasKeyword(Keyword::WHERE)) {
137137
$where = $this->expressionParser->parseExpression($tokenList);
138138
}
139139
$tokenList->expectEnd();
@@ -209,7 +209,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
209209
$like = $where = null;
210210
if ($tokenList->hasKeyword(Keyword::LIKE)) {
211211
$like = $tokenList->expectString();
212-
} elseif ($tokenList->expectKeyword(Keyword::WHERE)) {
212+
} elseif ($tokenList->hasKeyword(Keyword::WHERE)) {
213213
$where = $this->expressionParser->parseExpression($tokenList);
214214
}
215215
$tokenList->expectEnd();
@@ -272,7 +272,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
272272
$like = $where = null;
273273
if ($tokenList->hasKeyword(Keyword::LIKE)) {
274274
$like = $tokenList->expectString();
275-
} elseif ($tokenList->expectKeyword(Keyword::WHERE)) {
275+
} elseif ($tokenList->hasKeyword(Keyword::WHERE)) {
276276
$where = $this->expressionParser->parseExpression($tokenList);
277277
}
278278
$tokenList->expectEnd();
@@ -295,7 +295,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
295295
}
296296
$tokenList->expectEnd();
297297

298-
return new ShowGrantsCommand($forUser, $usingRoles ?: null);
298+
return new ShowGrantsCommand($forUser, $usingRoles !== [] ? $usingRoles : null);
299299
case Keyword::INDEX:
300300
case Keyword::INDEXES:
301301
case Keyword::KEYS:
@@ -351,7 +351,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
351351
$like = $where = null;
352352
if ($tokenList->hasKeyword(Keyword::LIKE)) {
353353
$like = $tokenList->expectString();
354-
} elseif ($tokenList->expectKeyword(Keyword::WHERE)) {
354+
} elseif ($tokenList->hasKeyword(Keyword::WHERE)) {
355355
$where = $this->expressionParser->parseExpression($tokenList);
356356
}
357357
$tokenList->expectEnd();
@@ -386,7 +386,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
386386

387387
$types = [];
388388
$type = $tokenList->getAnyKeyword(...array_keys($keywords));
389-
if ($type) {
389+
if ($type !== null) {
390390
$types[] = ShowProfileType::get($continue($type));
391391
}
392392
while ($tokenList->hasComma()) {
@@ -517,7 +517,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
517517
}
518518
$tokenList->expectEnd();
519519

520-
return new ShowStatusCommand($scope ? Scope::get($scope) : null, $like, $where);
520+
return new ShowStatusCommand($scope !== null ? Scope::get($scope) : null, $like, $where);
521521
} elseif ($tokenList->seekKeyword(Keyword::VARIABLES, 2)) {
522522
// SHOW [GLOBAL | SESSION] VARIABLES [LIKE 'pattern' | WHERE expr]
523523
$scope = $tokenList->getAnyKeyword(Keyword::GLOBAL, Keyword::SESSION);
@@ -530,7 +530,7 @@ public function parseShow(TokenList $tokenList): ShowCommand
530530
}
531531
$tokenList->expectEnd();
532532

533-
return new ShowVariablesCommand($scope ? Scope::get($scope) : null, $like, $where);
533+
return new ShowVariablesCommand($scope !== null ? Scope::get($scope) : null, $like, $where);
534534
} elseif ($tokenList->seekKeyword(Keyword::COLUMNS, 2)) {
535535
// SHOW [FULL] COLUMNS {FROM | IN} tbl_name [{FROM | IN} db_name] [LIKE 'pattern' | WHERE expr]
536536
$full = $tokenList->hasKeyword(Keyword::FULL);

sources/Parser/Dal/UserCommandsParser.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,10 @@ private function parseIdentifiedUsers(TokenList $tokenList): array
180180
}
181181
}
182182

183-
$action = $action ? IdentifiedUserAction::get($action) : null;
183+
if ($action !== null) {
184+
$action = IdentifiedUserAction::get($action);
185+
}
186+
184187
$users[] = new IdentifiedUser($user, $action, $password, $plugin, $replace, $retainCurrent);
185188
} while ($tokenList->hasComma());
186189

@@ -243,13 +246,12 @@ private function parseResourceOptions(TokenList $tokenList): ?array
243246
}
244247

245248
$resourceOptions = [];
246-
/** @var UserResourceOptionType $type */
247249
$type = $tokenList->expectKeywordEnum(UserResourceOptionType::class);
248-
while ($type !== null) {
250+
do {
249251
$value = $tokenList->expectInt();
250252
$resourceOptions[] = new UserResourceOption($type, $value);
251253
$type = $tokenList->getKeywordEnum(UserResourceOptionType::class);
252-
}
254+
} while ($type !== null);
253255

254256
return $resourceOptions;
255257
}
@@ -496,7 +498,7 @@ private function parseResource(TokenList $tokenList): UserPrivilegeResource
496498
$tokenList->expectKeyword(Keyword::ON);
497499
/** @var UserPrivilegeResourceType|null $resourceType */
498500
$resourceType = $tokenList->getKeywordEnum(UserPrivilegeResourceType::class);
499-
if ($tokenList->getOperator(Operator::MULTIPLY)) {
501+
if ($tokenList->hasOperator(Operator::MULTIPLY)) {
500502
$object = false;
501503
if ($tokenList->has(TokenType::DOT)) {
502504
$tokenList->expectOperator(Operator::MULTIPLY);
@@ -508,7 +510,7 @@ private function parseResource(TokenList $tokenList): UserPrivilegeResource
508510
$name = $tokenList->expectName();
509511
if ($tokenList->has(TokenType::DOT)) {
510512
$schema = $name;
511-
if ($tokenList->getOperator(Operator::MULTIPLY)) {
513+
if ($tokenList->hasOperator(Operator::MULTIPLY)) {
512514
return new UserPrivilegeResource($schema, UserPrivilegeResource::ALL, $resourceType);
513515
} else {
514516
$name = $tokenList->expectName();
@@ -632,17 +634,17 @@ public function parseSetPassword(TokenList $tokenList): SetPasswordCommand
632634
$user = new UserName(...$tokenList->expectUserName());
633635
}
634636
$tokenList->expectOperator(Operator::EQUAL);
635-
$function = $tokenList->hasKeyword(Keyword::PASSWORD);
636-
if ($function !== null) {
637+
$passwordFunction = $tokenList->hasKeyword(Keyword::PASSWORD);
638+
if ($passwordFunction) {
637639
$tokenList->expect(TokenType::LEFT_PARENTHESIS);
638640
}
639641
$password = $tokenList->expectString();
640-
if ($function !== null) {
642+
if ($passwordFunction) {
641643
$tokenList->expect(TokenType::RIGHT_PARENTHESIS);
642644
}
643645
$tokenList->expectEnd();
644646

645-
return new SetPasswordCommand($user, $password, (bool) $function);
647+
return new SetPasswordCommand($user, $password, $passwordFunction);
646648
}
647649

648650
/**

sources/Parser/Ddl/EventCommandsParser.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use SqlFtw\Sql\Ddl\Event\EventDefinition;
2020
use SqlFtw\Sql\Ddl\Event\EventSchedule;
2121
use SqlFtw\Sql\Ddl\Event\EventState;
22-
use SqlFtw\Sql\Expression\Operator;
2322
use SqlFtw\Sql\Keyword;
2423
use SqlFtw\Sql\QualifiedName;
2524

@@ -108,7 +107,7 @@ public function parseCreateEvent(TokenList $tokenList): CreateEventCommand
108107
$definer = $preserve = $state = $comment = null;
109108

110109
if ($tokenList->hasKeyword(Keyword::DEFINER)) {
111-
$tokenList->getOperator(Operator::EQUAL);
110+
$tokenList->passEqual();
112111
$definer = $this->expressionParser->parseUserExpression($tokenList);
113112
}
114113
$tokenList->expectKeyword(Keyword::EVENT);

0 commit comments

Comments
 (0)