Skip to content

Commit

Permalink
use subset of phpstan-strict-rules
Browse files Browse the repository at this point in the history
  • Loading branch information
schlndh committed Apr 28, 2023
1 parent 6f926ab commit 3f82e50
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"squizlabs/php_codesniffer": "^3.7",
"slevomat/coding-standard": "~8.11.0",
"phpstan/phpstan-phpunit": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.1"
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpstan/phpstan-strict-rules": "^1.5"
},
"config": {
"allow-plugins": {
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- phpstan-baseline.neon
parameters:
phpVersion: 80100
Expand All @@ -19,6 +20,13 @@ parameters:
- 'MariaStan\Ast\Exception\InvalidArgumentException'
- 'InvalidArgumentException'
- 'ReflectionException'
strictRules:
# Ignore Dynamic call to static method PHPUnit\Framework\Assert::assertSame(). etc
strictCalls: false
# Ignore: Only booleans are allowed in an if condition, MariaStan\Parser\Token|null given etc
booleansInConditions: false
# Allow short ternary operator
disallowedConstructs: false
ignoreErrors:
-
message: '#.*throws checked exception.*#'
Expand Down
6 changes: 3 additions & 3 deletions src/Analyser/AnalyserState.php
Original file line number Diff line number Diff line change
Expand Up @@ -1298,10 +1298,10 @@ private function resolveExprType(Expr\Expr $expr, ?AnalyserConditionTypeEnum $co

$subresults = [];

foreach ($expr->conditions as $condition) {
$field = $this->resolveExprType($condition->when);
foreach ($expr->conditions as $caseCondition) {
$field = $this->resolveExprType($caseCondition->when);
$this->checkNotTuple($field->type);
$subresults[] = $field = $this->resolveExprType($condition->then);
$subresults[] = $field = $this->resolveExprType($caseCondition->then);
$this->checkNotTuple($field->type);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Analyser/ColumnResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,8 +593,8 @@ public function resolveAllColumns(?string $table): array
$fields[] = new QueryResultField($column, $exprType);
}
} else {
foreach ($this->allColumns as ['column' => $column, 'table' => $table]) {
$exprType = $this->findColumnExprType($table, $column);
foreach ($this->allColumns as ['column' => $column, 'table' => $columnTable]) {
$exprType = $this->findColumnExprType($columnTable, $column);

// This would have already been reported previously, so let's ignore it.
if ($exprType === null) {
Expand Down
4 changes: 2 additions & 2 deletions src/PHPStan/Helper/MySQLi/PHPStanMySQLiHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public function execute(AnalyserResultPHPStanParams $params, array $executeParam
$supportedPlaceholderTxt = implode(', ', array_keys($supportedPlaceholderCounts));
$errors = [];

foreach ($executeParamTypes as $params) {
$count = count($params);
foreach ($executeParamTypes as $executeParams) {
$count = count($executeParams);

if (isset($supportedPlaceholderCounts[$count])) {
continue;
Expand Down
7 changes: 6 additions & 1 deletion src/Parser/Position.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MariaStan\Parser;

use function assert;
use function max;
use function mb_substr;
use function min;
Expand All @@ -28,9 +29,13 @@ public function advance(string $str): self
$lines = substr_count($str, "\n");

if ($lines) {
$newLinePos = strrpos($str, "\n");
// implied by if ($lines)
assert($newLinePos !== false);

return new self(
$this->line + $lines,
strlen($str) - strrpos($str, "\n"),
strlen($str) - $newLinePos,
$this->offset + strlen($str),
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Parser/TokenTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use function array_map;
use function array_search;
use function array_slice;
use function assert;

enum TokenTypeEnum: string
{
Expand Down Expand Up @@ -325,6 +326,7 @@ public static function getKeywordsMap(): array
/** @phpstan-var array<int, TokenTypeEnum> $result Analysis is needlessly slow without this. */
$result = self::cases();
$eoiIdx = array_search(self::END_OF_INPUT, $result, true);
assert($eoiIdx !== false);
$result = array_slice($result, $eoiIdx + 1);
$result = array_combine(
array_map(static fn (self $e) => $e->value, $result),
Expand Down
1 change: 0 additions & 1 deletion tests/DbReflection/DbReflectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ public function test(DbReflection $reflection): void
$valDefaultExpr = $parser->parseSingleExpression('(abs(`val_mediumint`) + 5)');
$nullExpr = $parser->parseSingleExpression('NULL');

$this->assertNotNull($schema);
$this->assertSame($tableName, $schema->name);
$this->assertEquals([
'id' => new Column('id', new IntType(), false, null, true),
Expand Down
2 changes: 1 addition & 1 deletion tests/Parser/CodeTestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private function extractMode(string $expected): array
return [$expected, null];
}

$expected = (string) substr($expected, $firstNewLine + 1);
$expected = substr($expected, $firstNewLine + 1);

return [$expected, substr($firstLine, 2)];
}
Expand Down

0 comments on commit 3f82e50

Please sign in to comment.