Skip to content

Commit 6d3b4ac

Browse files
TomasVotrubastaabm
authored andcommitted
[cs] apply PSR-12 and other sets
1 parent 803e2d5 commit 6d3b4ac

File tree

91 files changed

+427
-276
lines changed

Some content is hidden

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

91 files changed

+427
-276
lines changed

bootstrap.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use Dotenv\Dotenv;
46
use staabm\PHPStanDba\QueryReflection\QueryReflection;
57
use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration;
68
use staabm\PHPStanDba\Tests\ReflectorFactory;
79

8-
require_once __DIR__.'/vendor/autoload.php';
10+
require_once __DIR__ . '/vendor/autoload.php';
911

1012
if (false === getenv('GITHUB_ACTION')) {
1113
$dotenv = Dotenv::createImmutable(__DIR__);

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
},
4444
"scripts": {
4545
"csfix": [
46-
"vendor/bin/ecs --ansi"
46+
"vendor/bin/ecs --ansi --fix"
4747
],
4848
"test": [
4949
"@putenv DBA_REFLECTOR=mysqli",

ecs.php

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
declare(strict_types=1);
44

5-
use PhpCsFixer\Fixer\Import\NoUnusedImportsFixer;
65
use Symplify\EasyCodingStandard\Config\ECSConfig;
76
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
87

@@ -15,18 +14,18 @@
1514
]);
1615

1716
$ecsConfig->skip([
18-
'*/data/*'
17+
'*/data/*',
1918
]);
2019

2120
// this way you can add sets - group of rules
2221
$ecsConfig->sets([
2322
// run and fix, one by one
24-
SetList::SPACES,
25-
SetList::ARRAY,
26-
SetList::STRICT,
27-
SetList::DOCBLOCK,
28-
SetList::NAMESPACES,
29-
SetList::COMMENTS,
30-
SetList::PSR_12,
23+
SetList::SPACES,
24+
SetList::ARRAY,
25+
SetList::STRICT,
26+
SetList::DOCBLOCK,
27+
SetList::NAMESPACES,
28+
SetList::COMMENTS,
29+
SetList::PSR_12,
3130
]);
3231
};

src/Analyzer/QueryPlanAnalyzer.php

+2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ final class QueryPlanAnalyzer
1313
* number of unindexed reads allowed before a query is considered inefficient.
1414
*/
1515
public const DEFAULT_UNINDEXED_READS_THRESHOLD = 100000;
16+
1617
/**
1718
* allows analyzing queries even on empty database schemas.
1819
*/
1920
public const TABLES_WITHOUT_DATA = 0;
21+
2022
/**
2123
* max number of rows in a table, for which we don't report errors, because using a index/table-scan wouldn't improve performance.
2224
* requires production quality data in the database at analysis time.

src/Analyzer/QueryPlanAnalyzerMysql.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ final class QueryPlanAnalyzerMysql
1717
* @deprecated use QueryPlanAnalyzer::DEFAULT_UNINDEXED_READS_THRESHOLD instead
1818
*/
1919
public const DEFAULT_UNINDEXED_READS_THRESHOLD = QueryPlanAnalyzer::DEFAULT_UNINDEXED_READS_THRESHOLD;
20+
2021
/**
2122
* @api
2223
*
2324
* @deprecated use QueryPlanAnalyzer::TABLES_WITHOUT_DATA instead
2425
*/
2526
public const TABLES_WITHOUT_DATA = QueryPlanAnalyzer::TABLES_WITHOUT_DATA;
27+
2628
/**
2729
* @api
2830
*
@@ -48,7 +50,7 @@ public function __construct($connection)
4850
*/
4951
public function analyze(string $query): QueryPlanResult
5052
{
51-
$simulatedQuery = 'EXPLAIN '.$query;
53+
$simulatedQuery = 'EXPLAIN ' . $query;
5254

5355
if ($this->connection instanceof PDO) {
5456
$this->connection->beginTransaction();

src/Analyzer/QueryPlanResult.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
final class QueryPlanResult
88
{
99
public const NO_INDEX = 'no-index';
10+
1011
public const TABLE_SCAN = 'table-scan';
12+
1113
public const UNINDEXED_READS = 'unindexed-reads';
1214

1315
/**
@@ -32,8 +34,6 @@ public function getSimulatedQuery(): string
3234

3335
/**
3436
* @param self::* $result
35-
*
36-
* @return void
3737
*/
3838
public function addRow(string $table, string $result)
3939
{

src/Ast/ExpressionFinder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
138138
// move to previous expression
139139
$previousStatement = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PREVIOUS);
140140
if (null !== $previousStatement) {
141-
if (!$previousStatement instanceof Node) {
141+
if (! $previousStatement instanceof Node) {
142142
throw new ShouldNotHappenException();
143143
}
144144
$foundNode = $this->findFirst([$previousStatement], $filter);

src/Ast/PreviousConnectingVisitor.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace staabm\PHPStanDba\Ast;
66

7-
use function array_pop;
87
use PhpParser\Node;
98
use PhpParser\NodeVisitorAbstract;
9+
use function array_pop;
1010

1111
final class PreviousConnectingVisitor extends NodeVisitorAbstract
1212
{
1313
public const ATTRIBUTE_PARENT = 'dba-parent';
14+
1415
public const ATTRIBUTE_PREVIOUS = 'dba-previous';
1516

1617
/**

src/CacheNotPopulatedException.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace staabm\PHPStanDba;
46

57
final class CacheNotPopulatedException extends DbaException

src/DbaException.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace staabm\PHPStanDba;
46

57
class DbaException extends \RuntimeException

src/Error.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace staabm\PHPStanDba;
46

57
use staabm\PHPStanDba\QueryReflection\BasePdoQueryReflector;
@@ -47,7 +49,7 @@ public function getCode()
4749

4850
public function asRuleMessage(): string
4951
{
50-
return 'Query error: '.$this->getMessage().' ('.$this->getCode().').';
52+
return 'Query error: ' . $this->getMessage() . ' (' . $this->getCode() . ').';
5153
}
5254

5355
/**
@@ -63,7 +65,7 @@ public static function forSyntaxError(\Throwable $exception, $code, string $quer
6365

6466
// to ease debugging, print the error we simulated
6567
$simulatedQuery = QuerySimulation::simulate($queryString);
66-
$message = $message."\n\nSimulated query: ".$simulatedQuery;
68+
$message = $message . "\n\nSimulated query: " . $simulatedQuery;
6769

6870
return new self($message, $code);
6971
}

src/Extensions/DoctrineConnectionExecuteQueryDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4444
}
4545

4646
// make sure we don't report wrong types in doctrine 2.x
47-
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
47+
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
4848
return null;
4949
}
5050

src/Extensions/DoctrineConnectionFetchDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
5757
}
5858

5959
// make sure we don't report wrong types in doctrine 2.x
60-
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
60+
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
6161
return null;
6262
}
6363

src/Extensions/DoctrineConnectionPrepareDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4444
}
4545

4646
// make sure we don't report wrong types in doctrine 2.x
47-
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
47+
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
4848
return null;
4949
}
5050

src/Extensions/DoctrineConnectionQueryDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4444
}
4545

4646
// make sure we don't report wrong types in doctrine 2.x
47-
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
47+
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
4848
return null;
4949
}
5050

src/Extensions/DoctrineResultDynamicReturnTypeExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
4949
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
5050
{
5151
// make sure we don't report wrong types in doctrine 2.x
52-
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
52+
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
5353
return null;
5454
}
5555

@@ -71,7 +71,7 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
7171

7272
if ($resultRowType instanceof ConstantArrayType) {
7373
$columnCount = \count($resultRowType->getKeyTypes()) / 2;
74-
if (!\is_int($columnCount)) {
74+
if (! \is_int($columnCount)) {
7575
throw new ShouldNotHappenException();
7676
}
7777

src/Extensions/DoctrineStatementExecuteDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
4040
}
4141

4242
// make sure we don't report wrong types in doctrine 2.x
43-
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
43+
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
4444
return null;
4545
}
4646

src/Extensions/MysqliQueryDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private function inferResultType(Expr $queryExpr, Scope $scope): ?Type
130130

131131
$resultType = TypeCombinator::union(...$genericObjects);
132132

133-
if (!QueryReflection::getRuntimeConfiguration()->throwsMysqliExceptions($this->phpVersion)) {
133+
if (! QueryReflection::getRuntimeConfiguration()->throwsMysqliExceptions($this->phpVersion)) {
134134
return TypeCombinator::union(
135135
$resultType,
136136
new ConstantBooleanType(false)

src/Extensions/PdoQuoteDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private function inferType(MethodCall $methodCall, Scope $scope): ?Type
7777
$type = PDO::PARAM_STR;
7878
} else {
7979
$typeType = $scope->getType($args[1]->value);
80-
if (!$typeType instanceof ConstantIntegerType) {
80+
if (! $typeType instanceof ConstantIntegerType) {
8181
return null;
8282
}
8383
$type = $typeType->getValue();

src/Extensions/PdoStatementFetchDynamicReturnTypeExtension.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
6767
}
6868

6969
// fetchAll() can return false prior to php8
70-
if (null !== $returnType && !$returnType instanceof MixedType && 'fetchAll' === $methodReflection->getName() && $this->phpVersion->getVersionId() >= 80000) {
70+
if (null !== $returnType && ! $returnType instanceof MixedType && 'fetchAll' === $methodReflection->getName() && $this->phpVersion->getVersionId() >= 80000) {
7171
$returnType = TypeCombinator::remove($returnType, new ConstantBooleanType(false));
7272
}
7373

@@ -116,7 +116,7 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
116116
}
117117
}
118118

119-
if (!$this->reflectionProvider->hasClass($className)) {
119+
if (! $this->reflectionProvider->hasClass($className)) {
120120
return null;
121121
}
122122

src/Extensions/PdoStatementFetchObjectDynamicReturnTypeExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
6565
}
6666
}
6767

68-
if (!$this->reflectionProvider->hasClass($className)) {
68+
if (! $this->reflectionProvider->hasClass($className)) {
6969
// XXX should we return NEVER or FALSE on unknown classes?
7070
return null;
7171
}

src/PdoReflection/PdoStatementObjectType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function newWithFetchType(int $fetchType): self
6262
*/
6363
private function reduceBothType(Type $bothType, int $fetchType): Type
6464
{
65-
if (!$bothType instanceof ConstantArrayType) {
65+
if (! $bothType instanceof ConstantArrayType) {
6666
return $bothType;
6767
}
6868

src/PdoReflection/PdoStatementReflection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function findPrepareBindCalls(MethodCall $methodCall): array
5454
*/
5555
public function getFetchType(Type $fetchModeType): ?int
5656
{
57-
if (!$fetchModeType instanceof ConstantIntegerType) {
57+
if (! $fetchModeType instanceof ConstantIntegerType) {
5858
return null;
5959
}
6060

src/PhpDoc/PhpDocUtil.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace staabm\PHPStanDba\PhpDoc;
46

57
use PhpParser\Node\Expr;
@@ -91,7 +93,7 @@ private static function matchStringAnnotation(string $annotation, $callLike, Sco
9193
// atm no resolved phpdoc for methods
9294
// see https://github.com/phpstan/phpstan/discussions/7657
9395
$phpDocString = $methodReflection->getDocComment();
94-
if (null !== $phpDocString && 1 === preg_match('/'.$annotation.'\s+(\S+).*$/m', $phpDocString, $matches)) {
96+
if (null !== $phpDocString && 1 === preg_match('/' . $annotation . '\s+(\S+).*$/m', $phpDocString, $matches)) {
9597
$placeholder = $matches[1];
9698

9799
if (\in_array($placeholder[0], ['"', "'"], true)) {

src/QueryReflection/BasePdoQueryReflector.php

+15-5
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
abstract class BasePdoQueryReflector implements QueryReflector, RecordingReflector
2222
{
2323
private const PSQL_SYNTAX_ERROR = '42601';
24+
2425
private const PSQL_INVALID_TEXT_REPRESENTATION = '22P02';
26+
2527
private const PSQL_UNDEFINED_COLUMN = '42703';
28+
2629
private const PSQL_UNDEFINED_TABLE = '42P01';
2730

2831
private const MYSQL_SYNTAX_ERROR_CODE = '42000';
32+
2933
private const MYSQL_UNKNOWN_COLUMN_IN_FIELDLIST = '42S22';
34+
3035
private const MYSQL_UNKNOWN_TABLE = '42S02';
3136

3237
private const PDO_SYNTAX_ERROR_CODES = [
@@ -62,6 +67,7 @@ abstract class BasePdoQueryReflector implements QueryReflector, RecordingReflect
6267
* @phpstan-ignore-next-line
6368
*/
6469
protected $stmt = null;
70+
6571
/**
6672
* @var array<string, array<string, list<string>>>
6773
*/
@@ -82,7 +88,7 @@ public function validateQueryString(string $queryString): ?Error
8288
{
8389
$result = $this->simulateQuery($queryString);
8490

85-
if (!$result instanceof PDOException) {
91+
if (! $result instanceof PDOException) {
8692
return null;
8793
}
8894

@@ -108,7 +114,7 @@ public function getResultType(string $queryString, int $fetchType): ?Type
108114
{
109115
$result = $this->simulateQuery($queryString);
110116

111-
if (!\is_array($result)) {
117+
if (! \is_array($result)) {
112118
return null;
113119
}
114120

@@ -157,7 +163,7 @@ protected function emulateFlags(string $nativeType, string $tableName, string $c
157163
// determine flags of all columns of the given table once
158164
$schemaFlags = $this->checkInformationSchema($tableName);
159165
foreach ($schemaFlags as $schemaColumnName => $flag) {
160-
if (!\array_key_exists($schemaColumnName, $this->emulatedFlags[$tableName])) {
166+
if (! \array_key_exists($schemaColumnName, $this->emulatedFlags[$tableName])) {
161167
$this->emulatedFlags[$tableName][$schemaColumnName] = [];
162168
}
163169
$this->emulatedFlags[$tableName][$schemaColumnName][] = $flag;
@@ -171,9 +177,13 @@ public function getDatasource()
171177
return $this->pdo;
172178
}
173179

174-
/** @return PDOException|list<ColumnMeta>|null */
180+
/**
181+
* @return PDOException|list<ColumnMeta>|null
182+
*/
175183
abstract protected function simulateQuery(string $queryString);
176184

177-
/** @return Iterator<string, TypeMapper::FLAG_*> */
185+
/**
186+
* @return Iterator<string, TypeMapper::FLAG_*>
187+
*/
178188
abstract protected function checkInformationSchema(string $tableName);
179189
}

0 commit comments

Comments
 (0)