Skip to content

Commit

Permalink
Upgrade to PHPStan 2 (#62)
Browse files Browse the repository at this point in the history
* Upgrade to PHPStan 2

* Increase PHPStan level
  • Loading branch information
staudenmeir authored Nov 24, 2024
1 parent 4721cfc commit 81a1725
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
level: 9
level: 10
paths:
- src
1 change: 1 addition & 0 deletions src/Connectors/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ConnectionFactory extends Base
*/
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
{
/** @var (\Closure(\PDO|\Closure, string, string, array<array-key, mixed>): \Illuminate\Database\Connection)|null $resolver */
$resolver = Connection::getResolver($driver);

if (!in_array($driver, ['singlestore', 'firebird']) && $resolver) {
Expand Down
3 changes: 2 additions & 1 deletion src/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Staudenmeir\LaravelCte;

use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;
use Staudenmeir\LaravelCte\Connectors\ConnectionFactory;

Expand All @@ -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);
});
}
Expand Down
3 changes: 3 additions & 0 deletions src/Eloquent/QueriesExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Staudenmeir\LaravelCte\Query\OracleBuilder;
use Staudenmeir\LaravelCte\Query\SingleStoreBuilder;

/**
* @phpstan-ignore trait.unused
*/
trait QueriesExpressions
{
/**
Expand Down
15 changes: 9 additions & 6 deletions src/Query/Grammars/Traits/CompilesExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct()
* Compile the common table expressions.
*
* @param \Illuminate\Database\Query\Builder $query
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>,
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null}> $expressions
* @return string
Expand Down Expand Up @@ -57,7 +57,7 @@ public function compileExpressions(Builder $query, array $expressions)
/**
* Get the "recursive" keyword.
*
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>,
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null}> $expressions
* @return string
Expand Down Expand Up @@ -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<string|\Illuminate\Database\Query\Expression>,
* @param array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null} $expression
* @return string
Expand Down Expand Up @@ -177,15 +177,18 @@ public function compileUpdate(Builder $query, array $values)
* where: list<mixed>, having: list<mixed>, order: list<mixed>, union: list<mixed>,
* unionOrder: list<mixed>} $bindings
* @param array<string, mixed> $values
* @return list<mixed>
* @return array<int, mixed>
*/
public function prepareBindingsForUpdate(array $bindings, array $values)
{
$values = array_merge($bindings['expressions'], $values);

unset($bindings['expressions']);

return parent::prepareBindingsForUpdate($bindings, $values);
/** @var array<int, mixed> $bindings */
$bindings = parent::prepareBindingsForUpdate($bindings, $values);

return $bindings;
}

/**
Expand All @@ -196,7 +199,7 @@ public function prepareBindingsForUpdate(array $bindings, array $values)
* where: list<mixed>, having: list<mixed>, order: list<mixed>, union: list<mixed>,
* unionOrder: list<mixed>} $bindings
* @param array<string, mixed> $values
* @return list<mixed>
* @return array<int, mixed>
*/
public function getBindingsForUpdate(Builder $query, array $bindings, array $values)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Grammars/Traits/CompilesMySqlExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ trait CompilesMySqlExpressions
* Compile the cycle detection.
*
* @param \Illuminate\Database\Query\Builder $query
* @param array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>,
* @param array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null} $expression
* @return string
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Grammars/Traits/CompilesOracleExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait CompilesOracleExpressions
/**
* Get the "recursive" keyword.
*
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>,
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null}> $expressions
* @return string
Expand Down
19 changes: 14 additions & 5 deletions src/Query/Grammars/Traits/CompilesPostgresExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ public function compileUpdate(Builder $query, array $values)
* where: list<mixed>, having: list<mixed>, order: list<mixed>, union: list<mixed>,
* unionOrder: list<mixed>} $bindings
* @param array<string, mixed> $values
* @return list<mixed>
* @return array<int, mixed>
*/
public function getBindingsForUpdate(Builder $query, array $bindings, array $values)
{
if ($query->joins || isset($query->limit)) {
return parent::prepareBindingsForUpdate($bindings, $values);
/** @var array<int, mixed> $bindings */
$bindings = parent::prepareBindingsForUpdate($bindings, $values);

return $bindings;
}

return $this->prepareBindingsForUpdate($bindings, $values);
/** @var array<int, mixed> $bindings */
$bindings = $this->prepareBindingsForUpdate($bindings, $values);

return $bindings;
}

/**
Expand Down Expand Up @@ -72,15 +78,18 @@ public function compileUpdateFrom(Builder $query, $values)
* where: list<mixed>, having: list<mixed>, order: list<mixed>, union: list<mixed>,
* unionOrder: list<mixed>} $bindings
* @param list<mixed> $values
* @return list<mixed>
* @return array<int, mixed>
*/
public function prepareBindingsForUpdateFrom(array $bindings, array $values)
{
$values = array_merge($bindings['expressions'], $values);

unset($bindings['expressions']);

return parent::prepareBindingsForUpdateFrom($bindings, $values);
/** @var array<int, mixed> $bindings */
$bindings = parent::prepareBindingsForUpdateFrom($bindings, $values);

return $bindings;
}

/** @inheritDoc */
Expand Down
12 changes: 9 additions & 3 deletions src/Query/Grammars/Traits/CompilesSQLiteExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,21 @@ public function compileUpdate(Builder $query, array $values)
* where: list<mixed>, having: list<mixed>, order: list<mixed>, union: list<mixed>,
* unionOrder: list<mixed>} $bindings
* @param array<string, mixed> $values
* @return list<mixed>
* @return array<int, mixed>
*/
public function getBindingsForUpdate(Builder $query, array $bindings, array $values)
{
if ($query->joins || isset($query->limit)) {
return parent::prepareBindingsForUpdate($bindings, $values);
/** @var array<int, mixed> $bindings */
$bindings = parent::prepareBindingsForUpdate($bindings, $values);

return $bindings;
}

return $this->prepareBindingsForUpdate($bindings, $values);
/** @var array<int, mixed> $bindings */
$bindings = $this->prepareBindingsForUpdate($bindings, $values);

return $bindings;
}

/** @inheritDoc */
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Grammars/Traits/CompilesSqlServerExpressions.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function compileSelect(Builder $query)
/**
* Get the "recursive" keyword.
*
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>,
* @param list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null}> $expressions
* @return string
Expand Down
20 changes: 16 additions & 4 deletions src/Query/Traits/BuildsExpressionQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait BuildsExpressionQueries
/**
* The common table expressions.
*
* @var list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>,
* @var list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null}>
*/
Expand All @@ -29,7 +29,7 @@ trait BuildsExpressionQueries
/**
* The common table expressions for union queries.
*
* @var list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>,
* @var list<array{name: string, query: string, columns: list<string|\Illuminate\Database\Query\Expression>|null,
* recursive: bool, materialized: bool|null,
* cycle: array{columns: list<string>, markColumn: string, pathColumn: string}|null}>
*/
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -200,9 +207,14 @@ public function insertUsing(array $columns, $query)
{
$this->applyBeforeQueryCallbacks();

/** @var array<int, mixed> $expressionBindings */
$expressionBindings = $this->bindings['expressions'];

/** @var string $sql */
/** @var array<int, mixed> $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),
Expand Down

0 comments on commit 81a1725

Please sign in to comment.