Skip to content

Commit

Permalink
PHPStan lvl 4 & Fix/issue 233 followup (#257)
Browse files Browse the repository at this point in the history
* Add failing test

* reduce fatal error to phpstan failure

* WIP

* wip

* wip

* Followup of fix/issue-233

* phpstan lvl 3

* level 4

* resolve merge conflicts

* Update HasOfDescendantsRelationships.php

* Resolve error

* Fix Singlestore getExpressionGrammar

* Update BuildsAdjacencyListQueries.php

* Use inheritDoc where possible

* Resolve @phpstan-ignore return.phpDocType

* add phpstan/phpstan-phpunit

* Lock phpstan/phpstan to 1.11.1 for now

* wip

* WIP

* wip

* Update HasRecursiveRelationshipScopes.php

* Update IsOfDescendantsRelation.php

* Update composer.json

* Refactoring

* Use native return types without docblock

* Apply review feedback

* Restore phpdoc

* Remove "tests" path

* Increase PHPStan level

* Update return types

* Refactoring

* Adjust to interface changes

* Remove mixin

* Remove "tests" path

* Improve types

* Revert breaking changes

* Formatting

* Add type checks

* Add type checks

* Formatting

---------

Co-authored-by: Jonas Staudenmeir <[email protected]>
  • Loading branch information
SanderMuller and staudenmeir authored Sep 30, 2024
1 parent 464698b commit 564131a
Show file tree
Hide file tree
Showing 49 changed files with 638 additions and 499 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/.docker export-ignore
/.github export-ignore
/tests export-ignore
/types export-ignore
.gitattributes export-ignore
.gitignore export-ignore
docker-compose.ci.yml export-ignore
Expand Down
1 change: 1 addition & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ We accept contributions via Pull Requests on [GitHub](https://github.com/stauden
docker compose run --rm php8.3 composer install
docker compose run --rm php8.3 vendor/bin/phpunit
docker compose run --rm php8.3 vendor/bin/phpstan analyse --memory-limit=-1
docker compose run --rm php8.3 vendor/bin/phpstan analyse --configuration=phpstan.types.neon.dist --memory-limit=-1
```
4 changes: 4 additions & 0 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ jobs:
run: |
docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm php${{ matrix.php }} \
vendor/bin/phpstan analyse --error-format=github --memory-limit=-1
- name: Analyse types
run: |
docker compose -f docker-compose.yml -f docker-compose.ci.yml run --rm php${{ matrix.php }} \
vendor/bin/phpstan analyse --configuration=phpstan.types.neon.dist --error-format=github --memory-limit=-1
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"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
4 changes: 1 addition & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
includes:
- ./vendor/larastan/larastan/extension.neon
- ./vendor/phpstan/phpstan-mockery/extension.neon
parameters:
level: 2
level: 5
paths:
- src
- tests
ignoreErrors:
- '#Unsafe usage of new static\(\).#'
- '#Access to an undefined property Illuminate\\Database\\Eloquent\\Model::\$laravel_through_key#'
Expand Down
6 changes: 6 additions & 0 deletions phpstan.types.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
includes:
- ./vendor/larastan/larastan/extension.neon
parameters:
level: 9
paths:
- types
2 changes: 1 addition & 1 deletion src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* @template TModel of \Illuminate\Database\Eloquent\Model
*
* @extends Base<TModel>
* @extends \Illuminate\Database\Eloquent\Builder<TModel>
*/
class Builder extends Base
{
Expand Down
2 changes: 1 addition & 1 deletion src/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Collection extends Base
* Generate a nested tree.
*
* @param string $childrenRelation
* @return static<int, TModel>
* @return static
*/
public function toTree($childrenRelation = 'children')
{
Expand Down
3 changes: 0 additions & 3 deletions src/Eloquent/Graph/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ class Collection extends Base
{
/**
* Generate a nested tree.
*
* @param string $childrenRelation
* @return $this<int, TModel>
*/
public function toTree(string $childrenRelation = 'children'): static
{
Expand Down
47 changes: 9 additions & 38 deletions src/Eloquent/Relations/BelongsToManyOfDescendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class BelongsToManyOfDescendants extends BelongsToMany
* Create a new belongs to many of descendants relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $table
* @param TRelatedModel $parent
* @param class-string<TRelatedModel>|string $table
* @param string $foreignPivotKey
* @param string $relatedPivotKey
* @param string $parentKey
Expand All @@ -46,24 +46,15 @@ public function __construct(
parent::__construct($query, $parent, $table, $foreignPivotKey, $relatedPivotKey, $parentKey, $relatedKey);
}

/**
* Set the base constraints on the relation query.
*
* @return void
*/
/** @inheritDoc */
public function addConstraints()
{
$this->performJoin();

$this->baseAddConstraints();
}

/**
* Set the where clause on the recursive expression query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return void
*/
/** @inheritDoc */
protected function addExpressionWhereConstraints(Builder $query)
{
$column = $this->andSelf ? $this->parent->getLocalKeyName() : $this->parent->getParentKeyName();
Expand All @@ -75,31 +66,19 @@ protected function addExpressionWhereConstraints(Builder $query)
);
}

/**
* Get the local key name for an eager load of the relation.
*
* @return string
*/
/** @inheritDoc */
public function getEagerLoadingLocalKeyName()
{
return $this->parentKey;
}

/**
* Get the foreign key name for an eager load of the relation.
*
* @return string
*/
/** @inheritDoc */
public function getEagerLoadingForeignKeyName()
{
return $this->foreignPivotKey;
}

/**
* Get the accessor for an eager load of the relation.
*
* @return string|null
*/
/** @inheritDoc */
public function getEagerLoadingAccessor()
{
return $this->accessor;
Expand All @@ -120,21 +99,13 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
return $this->baseGetRelationExistenceQuery($query, $parentQuery, $columns);
}

/**
* Get the local key name for the recursion expression.
*
* @return string
*/
/** @inheritDoc */
public function getExpressionLocalKeyName()
{
return $this->parentKey;
}

/**
* Get the foreign key name for the recursion expression.
*
* @return string
*/
/** @inheritDoc */
public function getExpressionForeignKeyName()
{
return $this->foreignPivotKey;
Expand Down
13 changes: 3 additions & 10 deletions src/Eloquent/Relations/Bloodline.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Bloodline extends Descendants
/**
* Create a new bloodline relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
* @param TRelatedModel $parent
* @param string $foreignKey
* @param string $localKey
* @return void
Expand All @@ -26,14 +26,7 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $localKe
parent::__construct($query, $parent, $foreignKey, $localKey, true);
}

/**
* Add a recursive expression to the query.
*
* @param callable $constraint
* @param \Illuminate\Database\Eloquent\Builder|null $query
* @param string|null $from
* @return \Illuminate\Database\Eloquent\Builder
*/
/** @inheritDoc */
protected function addExpression(callable $constraint, ?Builder $query = null, $from = null)
{
$query = $query ?: $this->query;
Expand Down
56 changes: 7 additions & 49 deletions src/Eloquent/Relations/Descendants.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ class Descendants extends HasMany implements ConcatenableRelation
buildDictionary as baseBuildDictionary;
}

/**
* Set the base constraints on the relation query.
*
* @return void
*/
/** @inheritDoc */
public function addConstraints()
{
if (static::$constraints) {
Expand All @@ -44,12 +40,7 @@ public function addConstraints()
}
}

/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
/** @inheritDoc */
public function addEagerConstraints(array $models)
{
$whereIn = $this->whereInMethod($this->parent, $this->localKey);
Expand All @@ -65,14 +56,7 @@ public function addEagerConstraints(array $models)
$this->addExpression($constraint);
}

/**
* Match the eagerly loaded results to their parents.
*
* @param array $models
* @param \Illuminate\Database\Eloquent\Collection $results
* @param string $relation
* @return array
*/
/** @inheritDoc */
public function match(array $models, Collection $results, $relation)
{
$dictionary = $this->buildDictionary($results);
Expand All @@ -90,12 +74,7 @@ public function match(array $models, Collection $results, $relation)
return $models;
}

/**
* Build model dictionary.
*
* @param \Illuminate\Database\Eloquent\Collection $results
* @return array
*/
/** @inheritDoc */
protected function buildDictionary(Collection $results)
{
if ($this->andSelf) {
Expand All @@ -117,14 +96,7 @@ protected function buildDictionary(Collection $results)
})->all();
}

/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parentQuery
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
/** @inheritDoc */
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
if ($query->getQuery()->from === $parentQuery->getQuery()->from) {
Expand All @@ -146,14 +118,7 @@ public function getRelationExistenceQuery(Builder $query, Builder $parentQuery,
return $this->addExpression($constraint, $query->select($columns));
}

/**
* Add the constraints for a relationship query on the same table.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parentQuery
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
/** @inheritDoc */
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
{
if ($columns instanceof Expression) {
Expand Down Expand Up @@ -181,14 +146,7 @@ public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder
return $this->addExpression($constraint, $query->select($columns), $from);
}

/**
* Add a recursive expression to the query.
*
* @param callable $constraint
* @param \Illuminate\Database\Eloquent\Builder|null $query
* @param string|null $from
* @return \Illuminate\Database\Eloquent\Builder
*/
/** @inheritDoc */
protected function addExpression(callable $constraint, ?Builder $query = null, $from = null)
{
$query = $query ?: $this->query;
Expand Down
26 changes: 5 additions & 21 deletions src/Eloquent/Relations/Graph/Ancestors.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ class Ancestors extends BelongsToMany implements ConcatenableRelation
buildDictionary as baseBuildDictionary;
}

/**
* Set the base constraints on the relation query.
*
* @return void
*/
/** @inheritDoc */
public function addConstraints()
{
if (static::$constraints) {
Expand All @@ -44,12 +40,7 @@ public function addConstraints()
}
}

/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
/** @inheritDoc */
public function addEagerConstraints(array $models)
{
$column = $this->andSelf ? $this->getQualifiedParentKeyName() : $this->getQualifiedRelatedPivotKeyName();
Expand All @@ -60,8 +51,8 @@ public function addEagerConstraints(array $models)
/**
* Build model dictionary.
*
* @param \Illuminate\Database\Eloquent\Collection $results
* @return array
* @param \Illuminate\Database\Eloquent\Collection<array-key, \Illuminate\Database\Eloquent\Model> $results
* @return array<string, \Illuminate\Database\Eloquent\Model[]>
*/
protected function buildDictionary(Collection $results)
{
Expand Down Expand Up @@ -94,14 +85,7 @@ protected function buildDictionary(Collection $results)
return $dictionary;
}

/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parentQuery
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
/** @inheritDoc */
public function getRelationExistenceQuery(Builder $query, Builder $parentQuery, $columns = ['*'])
{
if ($query->getQuery()->from === $parentQuery->getQuery()->from) {
Expand Down
Loading

0 comments on commit 564131a

Please sign in to comment.