From 81a172596e8e222170e371fd680e153425a9500c Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sun, 24 Nov 2024 14:36:31 +0100 Subject: [PATCH] Upgrade to PHPStan 2 (#62) * Upgrade to PHPStan 2 * Increase PHPStan level --- README.md | 2 +- composer.json | 5 +++-- phpstan.neon.dist | 2 +- src/Connectors/ConnectionFactory.php | 1 + src/DatabaseServiceProvider.php | 3 ++- src/Eloquent/QueriesExpressions.php | 3 +++ .../Grammars/Traits/CompilesExpressions.php | 15 ++++++++------ .../Traits/CompilesMySqlExpressions.php | 2 +- .../Traits/CompilesOracleExpressions.php | 2 +- .../Traits/CompilesPostgresExpressions.php | 19 +++++++++++++----- .../Traits/CompilesSQLiteExpressions.php | 12 ++++++++--- .../Traits/CompilesSqlServerExpressions.php | 2 +- src/Query/Traits/BuildsExpressionQueries.php | 20 +++++++++++++++---- 13 files changed, 62 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 1d85c41..5898ccb 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![CI](https://github.com/staudenmeir/laravel-cte/actions/workflows/ci.yml/badge.svg)](https://github.com/staudenmeir/laravel-cte/actions/workflows/ci.yml?query=branch%3Amain) [![Code Coverage](https://codecov.io/gh/staudenmeir/laravel-cte/graph/badge.svg?token=JWHOOEYYGG)](https://codecov.io/gh/staudenmeir/laravel-cte) -[![PHPStan](https://img.shields.io/badge/PHPStan-level%209-brightgreen.svg?style=flat)](https://github.com/staudenmeir/laravel-cte/actions/workflows/static-analysis.yml?query=branch%3Amain) +[![PHPStan](https://img.shields.io/badge/PHPStan-level%2010-brightgreen.svg?style=flat)](https://github.com/staudenmeir/laravel-cte/actions/workflows/static-analysis.yml?query=branch%3Amain) [![Latest Stable Version](https://poser.pugx.org/staudenmeir/laravel-cte/v/stable)](https://packagist.org/packages/staudenmeir/laravel-cte) [![Total Downloads](https://poser.pugx.org/staudenmeir/laravel-cte/downloads)](https://packagist.org/packages/staudenmeir/laravel-cte/stats) [![License](https://poser.pugx.org/staudenmeir/laravel-cte/license)](https://github.com/staudenmeir/laravel-cte/blob/main/LICENSE) diff --git a/composer.json b/composer.json index 848191a..8106070 100644 --- a/composer.json +++ b/composer.json @@ -14,8 +14,9 @@ }, "require-dev": { "harrygulliford/laravel-firebird": "^3.3", - "orchestra/testbench": "^9.0", - "phpstan/phpstan": "^1.12", + "laravel/framework": "^11.0", + "orchestra/testbench-core": "^9.5", + "phpstan/phpstan": "^2.0", "phpunit/phpunit": "^11.0", "singlestoredb/singlestoredb-laravel": "^1.5.4", "yajra/laravel-oci8": "^11.2.4" diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 3e639cd..9228327 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,4 +1,4 @@ parameters: - level: 9 + level: 10 paths: - src diff --git a/src/Connectors/ConnectionFactory.php b/src/Connectors/ConnectionFactory.php index 6502d5b..b4e5c96 100644 --- a/src/Connectors/ConnectionFactory.php +++ b/src/Connectors/ConnectionFactory.php @@ -29,6 +29,7 @@ class ConnectionFactory extends Base */ protected function createConnection($driver, $connection, $database, $prefix = '', array $config = []) { + /** @var (\Closure(\PDO|\Closure, string, string, array): \Illuminate\Database\Connection)|null $resolver */ $resolver = Connection::getResolver($driver); if (!in_array($driver, ['singlestore', 'firebird']) && $resolver) { diff --git a/src/DatabaseServiceProvider.php b/src/DatabaseServiceProvider.php index 9c63e91..41bb545 100644 --- a/src/DatabaseServiceProvider.php +++ b/src/DatabaseServiceProvider.php @@ -2,6 +2,7 @@ namespace Staudenmeir\LaravelCte; +use Illuminate\Contracts\Container\Container; use Illuminate\Support\ServiceProvider; use Staudenmeir\LaravelCte\Connectors\ConnectionFactory; @@ -10,7 +11,7 @@ class DatabaseServiceProvider extends ServiceProvider /** @inheritDoc */ public function register() { - $this->app->singleton('db.factory', function ($app) { + $this->app->singleton('db.factory', function (Container $app) { return new ConnectionFactory($app); }); } diff --git a/src/Eloquent/QueriesExpressions.php b/src/Eloquent/QueriesExpressions.php index 8697a0e..d233c9c 100644 --- a/src/Eloquent/QueriesExpressions.php +++ b/src/Eloquent/QueriesExpressions.php @@ -7,6 +7,9 @@ use Staudenmeir\LaravelCte\Query\OracleBuilder; use Staudenmeir\LaravelCte\Query\SingleStoreBuilder; +/** + * @phpstan-ignore trait.unused + */ trait QueriesExpressions { /** diff --git a/src/Query/Grammars/Traits/CompilesExpressions.php b/src/Query/Grammars/Traits/CompilesExpressions.php index 90b529c..e793eee 100644 --- a/src/Query/Grammars/Traits/CompilesExpressions.php +++ b/src/Query/Grammars/Traits/CompilesExpressions.php @@ -24,7 +24,7 @@ public function __construct() * Compile the common table expressions. * * @param \Illuminate\Database\Query\Builder $query - * @param list, + * @param list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null}> $expressions * @return string @@ -57,7 +57,7 @@ public function compileExpressions(Builder $query, array $expressions) /** * Get the "recursive" keyword. * - * @param list, + * @param list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null}> $expressions * @return string @@ -87,7 +87,7 @@ public function compileRecursionLimit(Builder $query, $recursionLimit) * Compile the cycle detection. * * @param \Illuminate\Database\Query\Builder $query - * @param array{name: string, query: string, columns: list, + * @param array{name: string, query: string, columns: list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null} $expression * @return string @@ -177,7 +177,7 @@ public function compileUpdate(Builder $query, array $values) * where: list, having: list, order: list, union: list, * unionOrder: list} $bindings * @param array $values - * @return list + * @return array */ public function prepareBindingsForUpdate(array $bindings, array $values) { @@ -185,7 +185,10 @@ public function prepareBindingsForUpdate(array $bindings, array $values) unset($bindings['expressions']); - return parent::prepareBindingsForUpdate($bindings, $values); + /** @var array $bindings */ + $bindings = parent::prepareBindingsForUpdate($bindings, $values); + + return $bindings; } /** @@ -196,7 +199,7 @@ public function prepareBindingsForUpdate(array $bindings, array $values) * where: list, having: list, order: list, union: list, * unionOrder: list} $bindings * @param array $values - * @return list + * @return array */ public function getBindingsForUpdate(Builder $query, array $bindings, array $values) { diff --git a/src/Query/Grammars/Traits/CompilesMySqlExpressions.php b/src/Query/Grammars/Traits/CompilesMySqlExpressions.php index 5ecf9d5..8eaf311 100644 --- a/src/Query/Grammars/Traits/CompilesMySqlExpressions.php +++ b/src/Query/Grammars/Traits/CompilesMySqlExpressions.php @@ -12,7 +12,7 @@ trait CompilesMySqlExpressions * Compile the cycle detection. * * @param \Illuminate\Database\Query\Builder $query - * @param array{name: string, query: string, columns: list, + * @param array{name: string, query: string, columns: list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null} $expression * @return string diff --git a/src/Query/Grammars/Traits/CompilesOracleExpressions.php b/src/Query/Grammars/Traits/CompilesOracleExpressions.php index 1139c8c..527e442 100644 --- a/src/Query/Grammars/Traits/CompilesOracleExpressions.php +++ b/src/Query/Grammars/Traits/CompilesOracleExpressions.php @@ -11,7 +11,7 @@ trait CompilesOracleExpressions /** * Get the "recursive" keyword. * - * @param list, + * @param list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null}> $expressions * @return string diff --git a/src/Query/Grammars/Traits/CompilesPostgresExpressions.php b/src/Query/Grammars/Traits/CompilesPostgresExpressions.php index 7b779a5..970b43f 100644 --- a/src/Query/Grammars/Traits/CompilesPostgresExpressions.php +++ b/src/Query/Grammars/Traits/CompilesPostgresExpressions.php @@ -36,15 +36,21 @@ public function compileUpdate(Builder $query, array $values) * where: list, having: list, order: list, union: list, * unionOrder: list} $bindings * @param array $values - * @return list + * @return array */ public function getBindingsForUpdate(Builder $query, array $bindings, array $values) { if ($query->joins || isset($query->limit)) { - return parent::prepareBindingsForUpdate($bindings, $values); + /** @var array $bindings */ + $bindings = parent::prepareBindingsForUpdate($bindings, $values); + + return $bindings; } - return $this->prepareBindingsForUpdate($bindings, $values); + /** @var array $bindings */ + $bindings = $this->prepareBindingsForUpdate($bindings, $values); + + return $bindings; } /** @@ -72,7 +78,7 @@ public function compileUpdateFrom(Builder $query, $values) * where: list, having: list, order: list, union: list, * unionOrder: list} $bindings * @param list $values - * @return list + * @return array */ public function prepareBindingsForUpdateFrom(array $bindings, array $values) { @@ -80,7 +86,10 @@ public function prepareBindingsForUpdateFrom(array $bindings, array $values) unset($bindings['expressions']); - return parent::prepareBindingsForUpdateFrom($bindings, $values); + /** @var array $bindings */ + $bindings = parent::prepareBindingsForUpdateFrom($bindings, $values); + + return $bindings; } /** @inheritDoc */ diff --git a/src/Query/Grammars/Traits/CompilesSQLiteExpressions.php b/src/Query/Grammars/Traits/CompilesSQLiteExpressions.php index 062dc98..10ea0c7 100644 --- a/src/Query/Grammars/Traits/CompilesSQLiteExpressions.php +++ b/src/Query/Grammars/Traits/CompilesSQLiteExpressions.php @@ -60,15 +60,21 @@ public function compileUpdate(Builder $query, array $values) * where: list, having: list, order: list, union: list, * unionOrder: list} $bindings * @param array $values - * @return list + * @return array */ public function getBindingsForUpdate(Builder $query, array $bindings, array $values) { if ($query->joins || isset($query->limit)) { - return parent::prepareBindingsForUpdate($bindings, $values); + /** @var array $bindings */ + $bindings = parent::prepareBindingsForUpdate($bindings, $values); + + return $bindings; } - return $this->prepareBindingsForUpdate($bindings, $values); + /** @var array $bindings */ + $bindings = $this->prepareBindingsForUpdate($bindings, $values); + + return $bindings; } /** @inheritDoc */ diff --git a/src/Query/Grammars/Traits/CompilesSqlServerExpressions.php b/src/Query/Grammars/Traits/CompilesSqlServerExpressions.php index 173916e..5d2bbf3 100644 --- a/src/Query/Grammars/Traits/CompilesSqlServerExpressions.php +++ b/src/Query/Grammars/Traits/CompilesSqlServerExpressions.php @@ -23,7 +23,7 @@ public function compileSelect(Builder $query) /** * Get the "recursive" keyword. * - * @param list, + * @param list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null}> $expressions * @return string diff --git a/src/Query/Traits/BuildsExpressionQueries.php b/src/Query/Traits/BuildsExpressionQueries.php index 1013dcf..dc7138b 100644 --- a/src/Query/Traits/BuildsExpressionQueries.php +++ b/src/Query/Traits/BuildsExpressionQueries.php @@ -20,7 +20,7 @@ trait BuildsExpressionQueries /** * The common table expressions. * - * @var list, + * @var list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null}> */ @@ -29,7 +29,7 @@ trait BuildsExpressionQueries /** * The common table expressions for union queries. * - * @var list, + * @var list|null, * recursive: bool, materialized: bool|null, * cycle: array{columns: list, markColumn: string, pathColumn: string}|null}> */ @@ -105,9 +105,16 @@ protected function getQueryGrammar(Connection $connection) */ public function withExpression($name, $query, ?array $columns = null, $recursive = false, $materialized = null, ?array $cycle = null) { + /** @var string $query */ [$query, $bindings] = $this->createSub($query); - $this->{$this->unions ? 'unionExpressions' : 'expressions'}[] = compact('name', 'query', 'columns', 'recursive', 'materialized', 'cycle'); + $expression = compact('name', 'query', 'columns', 'recursive', 'materialized', 'cycle'); + + if ($this->unions) { + $this->unionExpressions[] = $expression; + } else { + $this->expressions[] = $expression; + } $this->addBinding($bindings, 'expressions'); @@ -200,9 +207,14 @@ public function insertUsing(array $columns, $query) { $this->applyBeforeQueryCallbacks(); + /** @var array $expressionBindings */ + $expressionBindings = $this->bindings['expressions']; + + /** @var string $sql */ + /** @var array $bindings */ [$sql, $bindings] = $this->createSub($query); - $bindings = array_merge($this->bindings['expressions'], $bindings); + $bindings = array_merge($expressionBindings, $bindings); return $this->connection->affectingStatement( $this->grammar->compileInsertUsing($this, $columns, $sql),