Skip to content

Commit

Permalink
Merge pull request #305 from lptn/more-stubs
Browse files Browse the repository at this point in the history
Add More stubs: `lang_path`, `Eloquent\Builder::chunk`, `Database\Connection::transaction`
  • Loading branch information
lptn authored Jan 29, 2023
2 parents f218d3c + 9d24418 commit dd16593
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ protected function getCommonStubs(): array
{
return array_merge(
glob(dirname(__DIR__) . '/stubs/Contracts/*.stubphp'),
glob(dirname(__DIR__) . '/stubs/Database/*.stubphp'),
glob(dirname(__DIR__) . '/stubs/*.stubphp')
);
}
Expand Down
19 changes: 19 additions & 0 deletions stubs/Database/ManagesTransactions.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Database\Concerns;

trait ManagesTransactions
{
/**
* Execute a Closure within a transaction.
*
* @template TCallbackReturnType
*
* @param \Closure($this): TCallbackReturnType $callback
* @param positive-int $attempts
* @return TCallbackReturnType
*
* @throws \Throwable
*/
public function transaction(\Closure $callback, $attempts = 1) {}
}
7 changes: 7 additions & 0 deletions stubs/EloquentBuilder.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ class Builder
*/
public function whereRelation($relation, $column, $operator = null, $value = null) {}

/**
* @param positive-int $count
* @param callable(\Illuminate\Database\Eloquent\Collection<int, TModel>, int): mixed $callback
* @return bool
*/
public function chunk($count, $callback) {}

/**
* @param string $relation
* @param \Closure|string|array<int, string>|\Illuminate\Database\Query\Expression $column
Expand Down
21 changes: 21 additions & 0 deletions tests/acceptance/EloquentBuilderTypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,24 @@ Feature: Eloquent Builder types
"""
When I run Psalm
Then I see no errors

Scenario: call chunk returns templated Collection
Given I have the following code
"""
use \Illuminate\Database\Eloquent\Collection;
class Post extends \Illuminate\Database\Eloquent\Model {};
/** @psalm-return \Illuminate\Database\Eloquent\Collection<int, Post> */
function test_chunk(Builder $builder): Collection {
/** @var \Illuminate\Database\Eloquent\Collection<int, Post> $firstChunk */
$firstChunk = collect();
Post::query()->chunk(10, function (Collection $collection) use ($firstChunk) {
$firstChunk->merge($collection);
});
return $firstChunk;
}
"""
When I run Psalm
Then I see no errors

0 comments on commit dd16593

Please sign in to comment.