Skip to content

Commit

Permalink
PHPStan level 2 (#253)
Browse files Browse the repository at this point in the history
* phpstan-level-2

* Remove obsolete exclusion

* Add type hint

* Add PHPStan extension for Mockery

* Fix "method.notFound" errors

* Remove obsolete ignored error

* Resolve error

* PHPStan level 2 resolve identifier: property.notFound tests

* Fix incorrect TRelatedModel

* Refactoring

* Resolve remaining phpstan errors

* Fix namespace

* Formatting

* Refactoring

* Resolve error

* Resolve error

* Resolve error

---------

Co-authored-by: Jonas Staudenmeir <[email protected]>
  • Loading branch information
SanderMuller and staudenmeir authored Aug 28, 2024
1 parent 9f9ac47 commit 7b1dc99
Show file tree
Hide file tree
Showing 36 changed files with 198 additions and 35 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"larastan/larastan": "^2.0",
"mockery/mockery": "^1.5.1",
"orchestra/testbench": "^9.0",
"phpstan/phpstan-mockery": "^1.1",
"phpunit/phpunit": "^11.0",
"singlestoredb/singlestoredb-laravel": "^1.5.4",
"staudenmeir/eloquent-has-many-deep": "^1.20"
Expand Down
9 changes: 8 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon
parameters:
level: 1
level: 2
paths:
- src
- tests
ignoreErrors:
- '#Unsafe usage of new static\(\).#'
- '#Access to an undefined property Illuminate\\Database\\Eloquent\\Model::\$laravel_through_key#'
- '#Call to an undefined method TRelatedModel of Illuminate\\Database\\Eloquent\\Model#'
- '#Call to an undefined method TDeclaringModel of Illuminate\\Database\\Eloquent\\Model#'
- '#Call to an undefined method TModel of Illuminate\\Database\\Eloquent\\Model#'
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Model::#'
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder#'
3 changes: 2 additions & 1 deletion src/Eloquent/Graph/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ class Collection extends Base
* Generate a nested tree.
*
* @param string $childrenRelation
* @return static<int, TModel>
* @return $this<int, TModel>
*/
public function toTree(string $childrenRelation = 'children'): static
{
if ($this->isEmpty()) {
return $this;
}

/** @var TModel $model */
$model = $this->first();

$parentKeyName = $model->relationLoaded('pivot')
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/Ancestors.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends HasMany<TRelatedModel>
*/
class Ancestors extends HasMany implements ConcatenableRelation
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/BelongsToManyOfDescendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends BelongsToMany<TRelatedModel>
*/
class BelongsToManyOfDescendants extends BelongsToMany
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/Bloodline.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends Descendants<TRelatedModel>
*/
class Bloodline extends Descendants
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/Descendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends HasMany<TRelatedModel>
*/
class Descendants extends HasMany implements ConcatenableRelation
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/Graph/Ancestors.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends BelongsToMany<TRelatedModel>
*/
class Ancestors extends BelongsToMany implements ConcatenableRelation
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/Graph/Descendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends BelongsToMany<TRelatedModel>
*/
class Descendants extends BelongsToMany implements ConcatenableRelation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,19 @@ public function addEagerConstraintsToDeepRelationship(Builder $query, array $mod
*/
protected function mergeExpressions(Builder $query, Builder $from): Builder
{
$query->getQuery()->expressions = array_merge(
$query->getQuery()->expressions,
$from->getQuery()->expressions
/** @var \Staudenmeir\LaravelCte\Query\Builder $baseQuery */
$baseQuery = $query->getQuery();

/** @var \Staudenmeir\LaravelCte\Query\Builder $fromQuery */
$fromQuery = $from->getQuery();

$baseQuery->expressions = array_merge(
$baseQuery->expressions,
$fromQuery->expressions
);

return $query->addBinding(
$from->getQuery()->getRawBindings()['expressions'],
$fromQuery->getRawBindings()['expressions'],
'expressions'
);
}
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/HasManyOfDescendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends HasMany<TRelatedModel>
*/
class HasManyOfDescendants extends HasMany
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/MorphToManyOfDescendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends BelongsToManyOfDescendants<TRelatedModel>
*/
class MorphToManyOfDescendants extends BelongsToManyOfDescendants
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/RootAncestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends HasOne<TRelatedModel>
*/
class RootAncestor extends HasOne
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/RootAncestorOrSelf.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends RootAncestor<TRelatedModel>
*/
class RootAncestorOrSelf extends RootAncestor
Expand Down
1 change: 1 addition & 0 deletions src/Eloquent/Relations/Siblings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @extends HasMany<TRelatedModel>
*/
class Siblings extends HasMany
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,19 @@ public function getThroughKeyForDeepRelationships(string $alias): string
*/
protected function mergeExpressions(Builder $query, Builder $from): Builder
{
$query->getQuery()->expressions = array_merge(
$query->getQuery()->expressions,
$from->getQuery()->expressions
/** @var \Staudenmeir\LaravelCte\Query\Builder $baseQuery */
$baseQuery = $query->getQuery();

/** @var \Staudenmeir\LaravelCte\Query\Builder $fromQuery */
$fromQuery = $from->getQuery();

$baseQuery->expressions = array_merge(
$baseQuery->expressions,
$fromQuery->expressions
);

return $query->addBinding(
$from->getQuery()->getRawBindings()['expressions'],
$fromQuery->getRawBindings()['expressions'],
'expressions'
);
}
Expand Down
22 changes: 12 additions & 10 deletions src/Eloquent/Traits/BuildsAdjacencyListQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,37 +77,39 @@ protected function replacePathSeparator(array $items, $path, $separator)
*/
public function getExpressionGrammar()
{
$driver = $this->query->getConnection()->getDriverName();
/** @var \Illuminate\Database\Connection $connection */
$connection = $this->query->getConnection();

switch ($driver) {
switch ($connection->getDriverName()) {
case 'mysql':
$grammar = $this->query->getConnection()->isMaria()
/** @var \Illuminate\Database\MySqlConnection $connection */
$grammar = $connection->isMaria()
? new MariaDbGrammar($this->model)
: new MySqlGrammar($this->model);

return $this->query->getConnection()->withTablePrefix($grammar);
return $connection->withTablePrefix($grammar);
case 'mariadb':
return $this->query->getConnection()->withTablePrefix(
return $connection->withTablePrefix(
new MariaDbGrammar($this->model)
);
case 'pgsql':
return $this->query->getConnection()->withTablePrefix(
return $connection->withTablePrefix(
new PostgresGrammar($this->model)
);
case 'sqlite':
return $this->query->getConnection()->withTablePrefix(
return $connection->withTablePrefix(
new SQLiteGrammar($this->model)
);
case 'sqlsrv':
return $this->query->getConnection()->withTablePrefix(
return $connection->withTablePrefix(
new SqlServerGrammar($this->model)
);
case 'singlestore':
return $this->query->getConnection()->withTablePrefix(
return $connection->withTablePrefix(
new SingleStoreGrammar($this->model)
);
case 'firebird':
return $this->query->getConnection()->withTablePrefix(
return $connection->withTablePrefix(
new FirebirdGrammar($this->model)
);
}
Expand Down
15 changes: 15 additions & 0 deletions src/Eloquent/Traits/HasAdjacencyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\RootAncestorOrSelf;
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Siblings;

/**
* @property-read Collection<int, static> $ancestors
* @property-read Collection<int, static> $ancestorsAndSelf
* @property-read Collection<int, static> $bloodline
* @property-read Collection<int, static> $children
* @property-read Collection<int, static> $childrenAndSelf
* @property-read Collection<int, static> $descendants
* @property-read Collection<int, static> $descendantsAndSelf
* @property-read static|null $parent
* @property-read Collection<int, static> $parentAndSelf
* @property-read static|null $rootAncestor
* @property-read static $rootAncestorOrSelf
* @property-read Collection<int, static> $siblings
* @property-read Collection<int, static> $siblingsAndSelf
*/
trait HasAdjacencyList
{
use HasOfDescendantsRelationships;
Expand Down
10 changes: 10 additions & 0 deletions src/Eloquent/Traits/HasGraphAdjacencyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Ancestors;
use Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Graph\Descendants;

/**
* @property-read Collection<int, static> $ancestors
* @property-read Collection<int, static> $ancestorsAndSelf
* @property-read Collection<int, static> $children
* @property-read Collection<int, static> $childrenAndSelf
* @property-read Collection<int, static> $descendants
* @property-read Collection<int, static> $descendantsAndSelf
* @property-read Collection<int, static> $parents
* @property-read Collection<int, static> $parentsAndSelf
*/
trait HasGraphAdjacencyList
{
use HasGraphRelationshipScopes;
Expand Down
6 changes: 4 additions & 2 deletions src/Eloquent/Traits/HasGraphRelationshipScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ public function scopeWithRelationshipExpression(
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @param callable $constraint
* @param int $initialDepth
* @param array $pivotColumns
* @param string $from
* @return \Illuminate\Database\Eloquent\Builder $query
*/
Expand Down Expand Up @@ -186,7 +185,10 @@ protected function addInitialQueryPivotColumns(
$columns = [$this->getParentKeyName(), $this->getChildKeyName(), ...$this->getPivotColumns()];

if ($initialDepth === 0) {
$columnDefinitions = (new Collection($query->getConnection()->getSchemaBuilder()->getColumns($pivotTable)))
/** @var \Illuminate\Database\Connection $connection */
$connection = $query->getConnection();

$columnDefinitions = (new Collection($connection->getSchemaBuilder()->getColumns($pivotTable)))
->keyBy('name');

foreach ($columns as $column) {
Expand Down
6 changes: 3 additions & 3 deletions src/Eloquent/Traits/HasRecursiveRelationshipScopes.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function scopeTree(Builder $query, $maxDepth = null)
* Add a recursive expression for a custom tree to the query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param callable|\Illuminate\Database|Eloquent\Model $constraint
* @param callable|\Illuminate\Database\Eloquent\Model $constraint
* @param int|null $maxDepth
* @return \Illuminate\Database\Eloquent\Builder
*/
Expand Down Expand Up @@ -176,7 +176,7 @@ public function scopeWithRelationshipExpression(Builder $query, $direction, call
/**
* Get the initial query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar|\Illuminate\Database\Grammar $grammar
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @param callable $constraint
* @param int $initialDepth
* @param string $from
Expand Down Expand Up @@ -217,7 +217,7 @@ protected function getInitialQuery(ExpressionGrammar $grammar, callable $constra
/**
* Get the recursive query for a relationship expression.
*
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar|\Illuminate\Database\Grammar $grammar
* @param \Staudenmeir\LaravelAdjacencyList\Query\Grammars\ExpressionGrammar $grammar
* @param string $direction
* @param string $from
* @param int|null $maxDepth
Expand Down
8 changes: 8 additions & 0 deletions src/Query/Grammars/ExpressionGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,12 @@ public function compileCycleDetectionStopConstraint(string $column): string;
* @return bool
*/
public function supportsUnionInRecursiveExpression(): bool;

/**
* Wrap a value in keyword identifiers.
*
* @param \Illuminate\Contracts\Database\Query\Expression|string $value
* @return string
*/
public function wrap($value);
}
10 changes: 6 additions & 4 deletions tests/Graph/AncestorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,10 @@ public function testWithSumForSelfRelation()
$this->markTestSkipped();
}

$user = Node::withSum('ancestors', 'pivot_weight')->find(5);
$node = Node::withSum('ancestors', 'pivot_weight')->find(5);

$this->assertEquals(1 + 4 + 5 + 10 + 11, $user->ancestors_sum_pivot_weight);
// @phpstan-ignore property.notFound
$this->assertEquals(1 + 4 + 5 + 10 + 11, $node->ancestors_sum_pivot_weight);
}

public function testWithSumForSelfRelationAndSelf()
Expand All @@ -391,9 +392,10 @@ public function testWithSumForSelfRelationAndSelf()
$this->markTestSkipped();
}

$user = Node::withSum('ancestorsAndSelf', 'pivot_weight')->find(5);
$node = Node::withSum('ancestorsAndSelf', 'pivot_weight')->find(5);

$this->assertEquals(1 + 4 + 5 + 10 + 11, $user->ancestors_and_self_sum_pivot_weight);
// @phpstan-ignore property.notFound
$this->assertEquals(1 + 4 + 5 + 10 + 11, $node->ancestors_and_self_sum_pivot_weight);
}

public function testDelete()
Expand Down
10 changes: 6 additions & 4 deletions tests/Graph/DescendantsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,10 @@ public function testWithSumForSelfRelation()
$this->markTestSkipped();
}

$user = Node::withSum('descendants', 'pivot_weight')->find(2);
$node = Node::withSum('descendants', 'pivot_weight')->find(2);

$this->assertEquals(5 + 7 + 8 + 9, $user->descendants_sum_pivot_weight);
// @phpstan-ignore property.notFound
$this->assertEquals(5 + 7 + 8 + 9, $node->descendants_sum_pivot_weight);
}

public function testWithSumForSelfRelationAndSelf()
Expand All @@ -475,9 +476,10 @@ public function testWithSumForSelfRelationAndSelf()
$this->markTestSkipped();
}

$user = Node::withSum('descendantsAndSelf', 'pivot_weight')->find(2);
$node = Node::withSum('descendantsAndSelf', 'pivot_weight')->find(2);

$this->assertEquals(5 + 7 + 8 + 9, $user->descendants_and_self_sum_pivot_weight);
// @phpstan-ignore property.notFound
$this->assertEquals(5 + 7 + 8 + 9, $node->descendants_and_self_sum_pivot_weight);
}

public function testDelete()
Expand Down
1 change: 1 addition & 0 deletions tests/Graph/EloquentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public function testScopeSubgraphWithMaxDepth()

public function testChildren()
{
/** @var \Illuminate\Support\Collection<Node> $children */
$children = Node::find(1)->children;

$this->assertEquals([2, 3, 4, 5], $children->pluck('id')->all());
Expand Down
Loading

0 comments on commit 7b1dc99

Please sign in to comment.