Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ECS and apply standard sets #528

Merged
merged 5 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/code-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
composer-options: --ansi --prefer-dist

- name: Check code style
run: vendor/bin/php-cs-fixer fix --ansi --diff --dry-run --format=checkstyle | cs2pr # check whether there are still errors left
run: vendor/bin/ecs --ansi
33 changes: 0 additions & 33 deletions .php-cs-fixer.dist.php

This file was deleted.

4 changes: 3 additions & 1 deletion bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

use Dotenv\Dotenv;
use staabm\PHPStanDba\QueryReflection\QueryReflection;
use staabm\PHPStanDba\QueryReflection\RuntimeConfiguration;
use staabm\PHPStanDba\Tests\ReflectorFactory;

require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__ . '/vendor/autoload.php';

if (false === getenv('GITHUB_ACTION')) {
$dotenv = Dotenv::createImmutable(__DIR__);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"ext-pdo": "*",
"dibi/dibi": "^4.2",
"doctrine/dbal": "^2.13|^3.0",
"friendsofphp/php-cs-fixer": "3.4.0",
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpstan/extension-installer": "^1.2",
"phpstan/phpstan-php-parser": "^1.1",
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-strict-rules": "^1.1",
"phpunit/phpunit": "^8.5|^9.5",
"symplify/easy-coding-standard": "^11.2",
"tomasvotruba/unused-public": "^0.0.34",
"vlucas/phpdotenv": "^5.4"
},
Expand All @@ -43,7 +43,7 @@
},
"scripts": {
"csfix": [
"php-cs-fixer fix"
"vendor/bin/ecs --ansi --fix"
],
"test": [
"@putenv DBA_REFLECTOR=mysqli",
Expand Down
31 changes: 31 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

use Symplify\EasyCodingStandard\Config\ECSConfig;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return function (ECSConfig $ecsConfig): void {
$ecsConfig->paths([
__DIR__ . '/src',
__DIR__ . '/tests',
__DIR__ . '/ecs.php',
__DIR__ . '/bootstrap.php',
]);

$ecsConfig->skip([
'*/data/*',
]);

// this way you can add sets - group of rules
$ecsConfig->sets([
// run and fix, one by one
SetList::SPACES,
SetList::ARRAY,
SetList::STRICT,
SetList::DOCBLOCK,
SetList::NAMESPACES,
SetList::COMMENTS,
SetList::PSR_12,
]);
};
2 changes: 2 additions & 0 deletions src/Analyzer/QueryPlanAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ final class QueryPlanAnalyzer
* number of unindexed reads allowed before a query is considered inefficient.
*/
public const DEFAULT_UNINDEXED_READS_THRESHOLD = 100000;

/**
* allows analyzing queries even on empty database schemas.
*/
public const TABLES_WITHOUT_DATA = 0;

/**
* max number of rows in a table, for which we don't report errors, because using a index/table-scan wouldn't improve performance.
* requires production quality data in the database at analysis time.
Expand Down
4 changes: 3 additions & 1 deletion src/Analyzer/QueryPlanAnalyzerMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ final class QueryPlanAnalyzerMysql
* @deprecated use QueryPlanAnalyzer::DEFAULT_UNINDEXED_READS_THRESHOLD instead
*/
public const DEFAULT_UNINDEXED_READS_THRESHOLD = QueryPlanAnalyzer::DEFAULT_UNINDEXED_READS_THRESHOLD;

/**
* @api
*
* @deprecated use QueryPlanAnalyzer::TABLES_WITHOUT_DATA instead
*/
public const TABLES_WITHOUT_DATA = QueryPlanAnalyzer::TABLES_WITHOUT_DATA;

/**
* @api
*
Expand All @@ -48,7 +50,7 @@ public function __construct($connection)
*/
public function analyze(string $query): QueryPlanResult
{
$simulatedQuery = 'EXPLAIN '.$query;
$simulatedQuery = 'EXPLAIN ' . $query;

if ($this->connection instanceof PDO) {
$this->connection->beginTransaction();
Expand Down
6 changes: 3 additions & 3 deletions src/Analyzer/QueryPlanResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
final class QueryPlanResult
{
public const NO_INDEX = 'no-index';

public const TABLE_SCAN = 'table-scan';

public const UNINDEXED_READS = 'unindexed-reads';

/**
Expand All @@ -32,10 +34,8 @@ public function getSimulatedQuery(): string

/**
* @param self::* $result
*
* @return void
*/
public function addRow(string $table, string $result)
public function addRow(string $table, string $result): void
{
$this->result[$table] = $result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Ast/ExpressionFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private function findFirstPreviousOfNode(Node $node, callable $filter): ?Node
// move to previous expression
$previousStatement = $node->getAttribute(PreviousConnectingVisitor::ATTRIBUTE_PREVIOUS);
if (null !== $previousStatement) {
if (!$previousStatement instanceof Node) {
if (! $previousStatement instanceof Node) {
throw new ShouldNotHappenException();
}
$foundNode = $this->findFirst([$previousStatement], $filter);
Expand Down
3 changes: 2 additions & 1 deletion src/Ast/PreviousConnectingVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace staabm\PHPStanDba\Ast;

use function array_pop;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use function array_pop;

final class PreviousConnectingVisitor extends NodeVisitorAbstract
{
public const ATTRIBUTE_PARENT = 'dba-parent';

public const ATTRIBUTE_PREVIOUS = 'dba-previous';

/**
Expand Down
2 changes: 2 additions & 0 deletions src/CacheNotPopulatedException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba;

final class CacheNotPopulatedException extends DbaException
Expand Down
2 changes: 2 additions & 0 deletions src/DbaException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba;

class DbaException extends \RuntimeException
Expand Down
6 changes: 4 additions & 2 deletions src/Error.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba;

use staabm\PHPStanDba\QueryReflection\BasePdoQueryReflector;
Expand Down Expand Up @@ -47,7 +49,7 @@ public function getCode()

public function asRuleMessage(): string
{
return 'Query error: '.$this->getMessage().' ('.$this->getCode().').';
return 'Query error: ' . $this->getMessage() . ' (' . $this->getCode() . ').';
}

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

// to ease debugging, print the error we simulated
$simulatedQuery = QuerySimulation::simulate($queryString);
$message = $message."\n\nSimulated query: ".$simulatedQuery;
$message = $message . "\n\nSimulated query: " . $simulatedQuery;

return new self($message, $code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/Extensions/DoctrineResultDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function isMethodSupported(MethodReflection $methodReflection): bool
public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
// make sure we don't report wrong types in doctrine 2.x
if (!InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
if (! InstalledVersions::satisfies(new VersionParser(), 'doctrine/dbal', '3.*')) {
return null;
}

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

if ($resultRowType instanceof ConstantArrayType) {
$columnCount = \count($resultRowType->getKeyTypes()) / 2;
if (!\is_int($columnCount)) {
if (! \is_int($columnCount)) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/MysqliQueryDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ private function inferResultType(Expr $queryExpr, Scope $scope): ?Type

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

if (!QueryReflection::getRuntimeConfiguration()->throwsMysqliExceptions($this->phpVersion)) {
if (! QueryReflection::getRuntimeConfiguration()->throwsMysqliExceptions($this->phpVersion)) {
return TypeCombinator::union(
$resultType,
new ConstantBooleanType(false)
Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/PdoQuoteDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private function inferType(MethodCall $methodCall, Scope $scope): ?Type
$type = PDO::PARAM_STR;
} else {
$typeType = $scope->getType($args[1]->value);
if (!$typeType instanceof ConstantIntegerType) {
if (! $typeType instanceof ConstantIntegerType) {
return null;
}
$type = $typeType->getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function getTypeFromMethodCall(MethodReflection $methodReflection, Method
}

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

Expand Down Expand Up @@ -116,7 +116,7 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
}
}

if (!$this->reflectionProvider->hasClass($className)) {
if (! $this->reflectionProvider->hasClass($className)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private function inferType(MethodReflection $methodReflection, MethodCall $metho
}
}

if (!$this->reflectionProvider->hasClass($className)) {
if (! $this->reflectionProvider->hasClass($className)) {
// XXX should we return NEVER or FALSE on unknown classes?
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion src/PdoReflection/PdoStatementObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function newWithFetchType(int $fetchType): self
*/
private function reduceBothType(Type $bothType, int $fetchType): Type
{
if (!$bothType instanceof ConstantArrayType) {
if (! $bothType instanceof ConstantArrayType) {
return $bothType;
}

Expand Down
2 changes: 1 addition & 1 deletion src/PdoReflection/PdoStatementReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function findPrepareBindCalls(MethodCall $methodCall): array
*/
public function getFetchType(Type $fetchModeType): ?int
{
if (!$fetchModeType instanceof ConstantIntegerType) {
if (! $fetchModeType instanceof ConstantIntegerType) {
return null;
}

Expand Down
4 changes: 3 additions & 1 deletion src/PhpDoc/PhpDocUtil.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace staabm\PHPStanDba\PhpDoc;

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

if (\in_array($placeholder[0], ['"', "'"], true)) {
Expand Down
Loading