Skip to content

Commit

Permalink
fix: add more method definitions to stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
canvural committed Apr 12, 2020
1 parent b6830f6 commit af981c6
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 3 deletions.
1 change: 1 addition & 0 deletions extension.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
stubFiles:
- stubs/Arrayable.stub
- stubs/Enumerable.stub
- stubs/EloquentBuilder.stub
- stubs/Collection.stub
Expand Down
13 changes: 13 additions & 0 deletions stubs/Arrayable.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Illuminate\Contracts\Support;

interface Arrayable
{
/**
* Get the instance as an array.
*
* @return array<mixed>
*/
public function toArray();
}
85 changes: 85 additions & 0 deletions stubs/BelongstoMany.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,91 @@ namespace Illuminate\Database\Eloquent\Relations;
*/
class BelongsToMany extends Relation
{
/**
* Find a related model by its primary key or return new instance of the related model.
*
* @param mixed $id
* @param array<int, mixed> $columns
* @return \Illuminate\Support\Collection<int, TRelatedModel>|TRelatedModel
*/
public function findOrNew($id, $columns = ['*']);

/**
* Get the first related model record matching the attributes or instantiate it.
*
* @param array<int, mixed> $attributes
* @return TRelatedModel
*/
public function firstOrNew(array $attributes);

/**
* Get the first related record matching the attributes or create it.
*
* @param array<int, mixed> $attributes
* @param array<mixed> $joining
* @param bool $touch
* @return TRelatedModel
*/
public function firstOrCreate(array $attributes, array $joining = [], $touch = true);

/**
* Create or update a related record matching the attributes, and fill it with values.
*
* @param array<int, mixed> $attributes
* @param array<mixed> $values
* @param array<mixed> $joining
* @param bool $touch
* @return TRelatedModel
*/
public function updateOrCreate(array $attributes, array $values = [], array $joining = [], $touch = true);

/**
* Find a related model by its primary key.
*
* @param mixed $id
* @param array<int, mixed> $columns
* @return TRelatedModel|\Illuminate\Database\Eloquent\Collection<TRelatedModel>|null
*/
public function find($id, $columns = ['*']);

/**
* Find multiple related models by their primary keys.
*
* @param \Illuminate\Contracts\Support\Arrayable|int[] $ids
* @param array<int, mixed> $columns
* @return \Illuminate\Database\Eloquent\Collection<TRelatedModel>
*/
public function findMany($ids, $columns = ['*']);

/**
* Find a related model by its primary key or throw an exception.
*
* @param mixed $id
* @param array<int, mixed> $columns
* @return TRelatedModel|\Illuminate\Database\Eloquent\Collection<TRelatedModel>
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function findOrFail($id, $columns = ['*']);

/**
* Execute the query and get the first result.
*
* @param array<int, mixed> $columns
* @return TRelatedModel|null
*/
public function first($columns = ['*']);

/**
* Execute the query and get the first result or throw an exception.
*
* @param array<int, mixed> $columns
* @return TRelatedModel
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function firstOrFail($columns = ['*']);

/**
* Get the results of the relationship.
*
Expand Down
58 changes: 55 additions & 3 deletions stubs/HasOneOrMany.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,56 @@ namespace Illuminate\Database\Eloquent\Relations;
*/
abstract class HasOneOrMany extends Relation
{
/**
* @param array<int, mixed> $attributes
* @phpstan-return TRelatedModel
*/
public function make(array $attributes = []);

/**
* Find a model by its primary key or return new instance of the related model.
*
* @param mixed $id
* @param array<int, mixed> $columns
* @return \Illuminate\Support\Collection<int, TRelatedModel>|TRelatedModel
*/
public function findOrNew($id, $columns = ['*']);

/**
* Get the first related model record matching the attributes or instantiate it.
*
* @param array<int, mixed> $attributes
* @param array<mixed, mixed> $values
* @return TRelatedModel
*/
public function firstOrNew(array $attributes, array $values = []);

/**
* Get the first related record matching the attributes or create it.
*
* @param array<int, mixed> $attributes
* @param array<mixed, mixed> $values
* @return TRelatedModel
*/
public function firstOrCreate(array $attributes, array $values = []);

/**
* Create or update a related record matching the attributes, and fill it with values.
*
* @param array<int, mixed> $attributes
* @param array<mixed, mixed> $values
* @return TRelatedModel
*/
public function updateOrCreate(array $attributes, array $values = []);

/**
* Attach a model instance to the parent model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return TRelatedModel|false
*/
public function save(\Illuminate\Database\Eloquent\Model $model);

/**
* @param array<string, mixed> $attributes
*
Expand All @@ -16,8 +66,10 @@ abstract class HasOneOrMany extends Relation
public function create(array $attributes = []);

/**
* @param array<int, mixed> $attributes
* @phpstan-return TRelatedModel
* Create a Collection of new instances of the related model.
*
* @param iterable<mixed> $records
* @return \Illuminate\Database\Eloquent\Collection<TRelatedModel>
*/
public function make(array $attributes = []);
public function createMany(iterable $records);
}
2 changes: 2 additions & 0 deletions stubs/Model.stub
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ namespace Illuminate\Database\Eloquent;
abstract class Model implements \JsonSerializable, \ArrayAccess
{
}

class ModelNotFoundException extends \RuntimeException {}
6 changes: 6 additions & 0 deletions tests/Features/Methods/ModelExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Features\Methods;

use App\Account;
use App\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand Down Expand Up @@ -215,6 +216,11 @@ public function testCustomAccessorOnModels(): string

return $thread->custom_property;
}

public function testFirstOrCreateWithRelation(User $user): Account
{
return $user->accounts()->firstOrCreate([]);
}
}

function foo(): string
Expand Down

0 comments on commit af981c6

Please sign in to comment.