Skip to content

Commit

Permalink
Update dev dependencies, fix tests and static analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Nov 25, 2024
1 parent f5e1205 commit a0429d7
Show file tree
Hide file tree
Showing 34 changed files with 90 additions and 75 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ it: vendor fix stan test ## Run useful checks before commits

.PHONY: help
help: ## Display this list of targets with descriptions
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'
@grep --extended-regexp '^[a-zA-Z0-9_-]+:.*?## .*$$' $(firstword $(MAKEFILE_LIST)) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'

.PHONY: setup
setup: build docs/node_modules vendor ## Prepare the local environment
Expand Down Expand Up @@ -37,7 +37,7 @@ prettier: up ## Format code with prettier

.PHONY: stan
stan: up ## Run static analysis with PHPStan
${dcphp} vendor/bin/phpstan
${dcphp} vendor/bin/phpstan --verbose

.PHONY: test
test: up ## Run tests with PHPUnit
Expand Down
1 change: 1 addition & 0 deletions phpbench.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"$schema": "vendor/phpbench/phpbench/phpbench.schema.json",
"runner.bootstrap": "vendor/autoload.php",
"runner.path": "benchmarks"
}
15 changes: 12 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ parameters:
- '#Parameter \#1 \$callback of static method Closure::fromCallable\(\) expects callable\(\): mixed, array{object, .*} given\.#'

# Some parts of Laravel are just really magical
- '#Parameter \#1 \$response of static method Nuwave\\Lighthouse\\Testing\\TestResponseUtils::extractValidationErrors\(\) expects Illuminate\\Testing\\TestResponse, \$this\(Nuwave\\Lighthouse\\Testing\\TestResponseMixin\) given\.#'
- path: tests/database/factories/*
- '#Parameter \#1 \$response of static method Nuwave\\Lighthouse\\Testing\\TestResponseUtils::extractValidationErrors\(\) expects Illuminate\\Testing\\TestResponse<Symfony\\Component\\HttpFoundation\\Response>, \$this\(Nuwave\\Lighthouse\\Testing\\TestResponseMixin\) given\.#'
- path: tests/database/factories
message: '#Variable \$factory might not be defined#'

# Mixins are magical
Expand All @@ -45,8 +45,14 @@ parameters:
# Relation forwards calls to Builder
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Builder<Illuminate\\Database\\Eloquent\\Model>|Illuminate\\Database\\Eloquent\\Relations\\Relation<Illuminate\\Database\\Eloquent\\Model>|Illuminate\\Database\\Query\\Builder::(orderBy|where|whereIn|whereNotIn|whereBetween|whereJsonContains|whereNotBetween)\(\)\.#'

# Laravel 11 added generics that are handled differently, so we just omit them
# Laravel 11 changes a lot of generics
- '#generic class (Illuminate\\Database\\Eloquent\\Builder|Laravel\\Scout\\Builder)( but)? does not specify its types#'
- '#contains generic type Illuminate\\Testing\\TestResponse<.+> but class Illuminate\\Testing\\TestResponse is not generic\.#'
- '#Generic type Illuminate\\Database\\Eloquent\\Relations\\(HasOne|HasMany|BelongsToMany|MorphOne|MorphMany|MorphToMany)<.+, .+> in PHPDoc tag @return specifies 2 template types, but class Illuminate\\Database\\Eloquent\\Relations\\\1 supports only 1: TRelatedModel#'
- '#Method .+ should return Illuminate\\Database\\Eloquent\\Relations\\(HasOne|HasMany|BelongsToMany|MorphOne|MorphMany|MorphToMany)<(.+), .+> but returns Illuminate\\Database\\Eloquent\\Relations\\\1<\2>.#'
- '#Method .+ should return Illuminate\\Database\\Eloquent\\Relations\\(BelongsTo|MorphTo)<(.+), \$this\((.+)\)> but returns Illuminate\\Database\\Eloquent\\Relations\\\1<\2, \3>.#'
- '#Generic type Illuminate\\Database\\Eloquent\\Relations\\(HasOneThrough|HasManyThrough)<.+, .+, .+> in PHPDoc tag @return specifies 3 template types, but class Illuminate\\Database\\Eloquent\\Relations\\\1 supports only 1: TRelatedModel#'
- '#Method .+ should return Illuminate\\Database\\Eloquent\\Relations\\(HasOneThrough|HasManyThrough)<(.+), .+> but returns Illuminate\\Database\\Eloquent\\Relations\\\1<\2>.#'

# This test cheats and uses reflection to make assertions
- path: tests/Unit/Schema/Directives/BaseDirectiveTest.php
Expand Down Expand Up @@ -101,3 +107,6 @@ parameters:
- path: src/Testing/TestResponseMixin.php
message: '#Unable to resolve the template type TValue in call to method static method Illuminate\\Support\\Arr::first\(\)#'

# Lots of work to specify the generics, little gain
- path: tests
message: '#Method .+ return type with generic class Illuminate\\Testing\\TestResponse does not specify its types: TResponse#'
2 changes: 1 addition & 1 deletion src/Auth/CanDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ function ($modelLike) use ($gate, $ability, $checkArguments) {
protected function modelsToCheck(mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo): iterable
{
if ($this->directiveArgValue('query')) {
return $resolveInfo
return $resolveInfo // @phpstan-ignore return.type (generic of Builder type is erased through enhanceBuilder)
->enhanceBuilder(
$this->getModelClass()::query(),
$this->directiveArgValue('scopes', []),
Expand Down
1 change: 1 addition & 0 deletions src/Execution/ModelsLoader/PaginatedModelsLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected function loadRelatedModels(EloquentCollection $parents): EloquentColle
// Merge all the relation queries into a single query with UNION ALL.

$firstRelation = $relations->shift();
// @phpstan-ignore function.impossibleType,instanceof.alwaysFalse
assert($firstRelation instanceof Relation, 'Non-null because only non-empty lists of parents are passed into this loader.');

// Use ->getQuery() to respect model scopes, such as soft deletes
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Directives/FindDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function resolveField(FieldValue $fieldValue): callable
throw new Error('The query returned more than one result.');
}

return $results->first();
return $results->first(); // @phpstan-ignore return.type (generic of Builder type is erased through enhanceBuilder)
};
}
}
2 changes: 2 additions & 0 deletions src/Testing/MakesGraphQLRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ trait MakesGraphQLRequests
*
* On the first call to introspect() this property is set to
* cache the result, as introspection is quite expensive.
*
* @var \Illuminate\Testing\TestResponse<\Symfony\Component\HttpFoundation\Response>
*/
protected TestResponse $introspectionResult;

Expand Down
2 changes: 1 addition & 1 deletion src/Testing/TestResponseUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class TestResponseUtils
{
/**
* @param \Illuminate\Testing\TestResponse $response
* @param \Illuminate\Testing\TestResponse<\Symfony\Component\HttpFoundation\Response> $response
*
* @return array<string, array<int, string>>|null
*/
Expand Down
7 changes: 4 additions & 3 deletions tests/Integration/Pagination/PaginateDirectiveDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,14 @@ public function testPaginateWithScopes(): void
public static function builder(): EloquentBuilder
{
return User::query()
->orderBy('id', 'DESC');
->orderByDesc('id');
}

/** @return \Illuminate\Database\Eloquent\Relations\Relation<\Tests\Utils\Models\Post> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\Post, \Tests\Utils\Models\User> */
public static function builderForRelation(User $parent): Relation
{
return $parent->posts()->orderBy('id', 'DESC');
return $parent->posts()
->orderByDesc('id');
}

public static function builderForScoutBuilder(): ScoutBuilder
Expand Down
7 changes: 4 additions & 3 deletions tests/Integration/Schema/Directives/AllDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,14 @@ public function testSpecifyCustomBuilderForScoutBuilder(): void
public static function builder(): EloquentBuilder
{
return User::query()
->orderBy('id', 'DESC');
->orderByDesc('id');
}

/** @return \Illuminate\Database\Eloquent\Relations\Relation<\Tests\Utils\Models\Post> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\Post, \Tests\Utils\Models\User> */
public static function builderForRelation(User $parent): Relation
{
return $parent->posts()->orderBy('id', 'DESC');
return $parent->posts()
->orderByDesc('id');
}

public static function builderForScoutBuilder(): ScoutBuilder
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Pagination/PaginateDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ public function testQueriesFirst0SimplePaginator(): void
/**
* @param array{first: int} $args
*
* @return \Illuminate\Pagination\LengthAwarePaginator<array<string, int>>
* @return \Illuminate\Pagination\LengthAwarePaginator<array{id: int}>
*/
public static function returnPaginatedDataInsteadOfBuilder(mixed $root, array $args): LengthAwarePaginator
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Schema/AST/ASTHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function manipulateFieldDefinition(
$typeType = $fieldType->type;
assert($typeType instanceof NamedTypeNode);

$this->assertSame($typeType->name->value, 'Int');
$this->assertSame('Int', $typeType->name->value);
}

public function testDynamicallyAddedArgManipulatorDirective(): void
Expand Down Expand Up @@ -363,7 +363,7 @@ public function manipulateArgDefinition(
$typeType = $argumentType->type;
assert($typeType instanceof NamedTypeNode);

$this->assertSame($typeType->name->value, 'Int');
$this->assertSame('Int', $typeType->name->value);
}

public function testDynamicallyAddedInputFieldManipulatorDirective(): void
Expand Down Expand Up @@ -427,6 +427,6 @@ public function manipulateInputFieldDefinition(
$typeType = $fieldType->type;
assert($typeType instanceof NamedTypeNode);

$this->assertSame($typeType->name->value, 'Int');
$this->assertSame('Int', $typeType->name->value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ final class RedisStorageManagerTest extends TestCase
* @param array<mixed> $firstCallArguments
* @param array<mixed> ...$consecutiveCallsArguments
*
* @return iterable<callback<mixed>>
* @return iterable<\PHPUnit\Framework\Constraint\Callback<mixed>>
*/
private function withConsecutive(array $firstCallArguments, array ...$consecutiveCallsArguments): iterable
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
*/
final class Activity extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, self> */
/** @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this> */
public function content(): MorphTo
{
return $this->morphTo();
}

/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Models/AlternateConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class AlternateConnection extends Model

protected $connection = DBTestCase::ALTERNATE_CONNECTION;

/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ final class Category extends Model
{
protected $primaryKey = 'category_id';

/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<self, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<self, $this> */
public function parent(): BelongsTo
{
return $this->belongsTo(Category::class, 'parent_id');
}

/** @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\Tests\Utils\Models\Post> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\Tests\Utils\Models\Post, $this> */
public function posts(): BelongsToMany
{
return $this->belongsToMany(Post::class, 'category_post', 'post_id', 'category_id');
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Models/Color.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
*/
final class Color extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\Product> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\Product, $this> */
public function products(): HasMany
{
return $this->hasMany(Product::class);
}

/** @return \Illuminate\Database\Eloquent\Relations\MorphTo<Model, self> */
/** @return \Illuminate\Database\Eloquent\Relations\MorphTo<Model, $this> */
public function creator(): MorphTo
{
return $this->morphTo();
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
*/
final class Comment extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}

/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\Post, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\Post, $this> */
public function post(): BelongsTo
{
return $this->belongsTo(Post::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Models/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
final class Company extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\User> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\User, $this> */
public function users(): HasMany
{
return $this->hasMany(User::class);
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Models/Contractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
*/
final class Contractor extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\MorphOne<\Tests\Utils\Models\User> */
/** @return \Illuminate\Database\Eloquent\Relations\MorphOne<\Tests\Utils\Models\User, $this> */
public function user(): MorphOne
{
return $this->morphOne(User::class, 'person');
}

/** @return \Illuminate\Database\Eloquent\Relations\MorphMany<\Tests\Utils\Models\Color> */
/** @return \Illuminate\Database\Eloquent\Relations\MorphMany<\Tests\Utils\Models\Color, $this> */
public function colors(): MorphMany
{
return $this->morphMany(Color::class, 'creator');
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Models/CustomPrimaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class CustomPrimaryKey extends Model

public $timestamps = false;

/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\Tests\Utils\Models\User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Models/Employee.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
*/
final class Employee extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\MorphOne<\Tests\Utils\Models\User> */
/** @return \Illuminate\Database\Eloquent\Relations\MorphOne<\Tests\Utils\Models\User, $this> */
public function user(): MorphOne
{
return $this->morphOne(User::class, 'person');
}

/** @return \Illuminate\Database\Eloquent\Relations\MorphMany<\Tests\Utils\Models\Color> */
/** @return \Illuminate\Database\Eloquent\Relations\MorphMany<\Tests\Utils\Models\Color, $this> */
public function colors(): MorphMany
{
return $this->morphMany(Color::class, 'creator');
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
final class Image extends Model
{
/** @return \Illuminate\Database\Eloquent\Relations\MorphTo<Model, self> */
/** @return \Illuminate\Database\Eloquent\Relations\MorphTo<Model, $this> */
public function imageable(): MorphTo
{
return $this->morphTo();
Expand Down
4 changes: 2 additions & 2 deletions tests/Utils/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ final class Location extends Model
'extra' => 'array',
];

/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<self, self> */
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<self, $this> */
public function parent(): BelongsTo
{
return $this->belongsTo(__CLASS__, 'parent_id');
}

/** @return \Illuminate\Database\Eloquent\Relations\HasMany<self> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<self, $this> */
public function children(): HasMany
{
return $this->hasMany(__CLASS__, 'parent_id');
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Models/NullConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getConnectionName(): ?string
return null;
}

/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\User> */
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\Tests\Utils\Models\User, $this> */
public function users(): HasMany
{
return $this->hasMany(User::class, 'company_id');
Expand Down
Loading

0 comments on commit a0429d7

Please sign in to comment.