Skip to content

Commit 7442f63

Browse files
authored
Merge pull request #90 from marcreichel/feat/type-coverage
✨ Type Coverage
2 parents c412f58 + 25cf649 commit 7442f63

File tree

7 files changed

+65
-37
lines changed

7 files changed

+65
-37
lines changed

.github/workflows/tests.yml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ jobs:
3636
with:
3737
php-version: ${{ matrix.php }}
3838
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo
39+
coverage: xdebug
3940

4041
- name: Setup problem matchers
4142
run: |

.github/workflows/type-coverage.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Type Coverage
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
type-coverage:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
with:
17+
php-version: 8.2
18+
coverage: xdebug
19+
- name: Install dependencies
20+
run: composer install --prefer-dist --no-progress --dev
21+
- run: composer test:type-coverage

composer.json

+9-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"roave/security-advisories": "dev-latest",
2626
"larastan/larastan": "^2.9.2",
2727
"laravel/pint": "^1.13",
28-
"pestphp/pest": "^2"
28+
"pestphp/pest": "^2",
29+
"pestphp/pest-plugin-type-coverage": "2.x-dev"
2930
},
3031
"license": "MIT",
3132
"authors": [
@@ -36,15 +37,19 @@
3637
],
3738
"scripts": {
3839
"pint": "./vendor/bin/pint --test -v",
39-
"test": "./vendor/bin/pest --parallel -v",
40+
"test": "./vendor/bin/pest --parallel",
4041
"stan": "./vendor/bin/phpstan --memory-limit=2G",
4142
"test:coverage": [
4243
"@putenv XDEBUG_MODE=coverage",
43-
"@test --coverage-clover build/clover.xml"
44+
"@test --coverage --min=90 --coverage-clover build/clover.xml"
4445
],
4546
"test:coverage-html": [
4647
"@putenv XDEBUG_MODE=coverage",
47-
"@test --coverage-html build/coverage"
48+
"@test --coverage --min=90 --coverage-html build/coverage"
49+
],
50+
"test:type-coverage": [
51+
"@putenv XDEBUG_MODE=coverage",
52+
"@test -- --type-coverage --min=100"
4853
]
4954
},
5055
"autoload": {

src/Builder.php

+17-14
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function select(mixed $fields): self
7777
$collection->push('*');
7878
}
7979

80-
$collection = $collection->filter(fn ($field) => !strpos($field, '.'))->flatten();
80+
$collection = $collection->filter(fn (string $field) => !strpos($field, '.'))->flatten();
8181

8282
if ($collection->isEmpty()) {
8383
$collection->push('*');
@@ -186,6 +186,7 @@ public function search(string $query): self
186186
*
187187
* @throws ReflectionException
188188
* @throws InvalidParamsException
189+
* @throws JsonException
189190
*/
190191
public function fuzzySearch(
191192
mixed $key,
@@ -196,7 +197,7 @@ public function fuzzySearch(
196197
$tokenizedQuery = explode(' ', $query);
197198
$keys = collect($key)->crossJoin($tokenizedQuery)->toArray();
198199

199-
return $this->whereNested(function ($query) use ($keys, $caseSensitive): void {
200+
return $this->whereNested(function (Builder $query) use ($keys, $caseSensitive): void {
200201
foreach ($keys as $v) {
201202
if (is_array($v)) {
202203
$query->whereLike($v[0], $v[1], $caseSensitive, '|');
@@ -458,7 +459,7 @@ protected function addArrayOfWheres(
458459
string $boolean,
459460
string $method = 'where',
460461
): self {
461-
return $this->whereNested(function ($query) use (
462+
return $this->whereNested(function (Builder $query) use (
462463
$arrayOfWheres,
463464
$method,
464465
$boolean
@@ -526,7 +527,7 @@ public function whereIn(
526527

527528
$where = $this->query->get('where', new Collection());
528529

529-
$valuesString = collect($values)->map(fn ($value) => !is_numeric($value) ? '"' . $value . '"' : $value)->implode(',');
530+
$valuesString = collect($values)->map(fn (mixed $value) => !is_numeric($value) ? '"' . $value . '"' : $value)->implode(',');
530531

531532
$where->push(($where->count() ? $boolean . ' ' : '') . $key . ' ' . $operator . ' ' . $prefix . $valuesString . $suffix);
532533

@@ -694,6 +695,7 @@ public function orWhereNotInExact(
694695
*
695696
* @throws ReflectionException
696697
* @throws InvalidParamsException
698+
* @throws JsonException
697699
*/
698700
public function whereBetween(
699701
string $key,
@@ -707,7 +709,7 @@ public function whereBetween(
707709
$second = $this->castDate($second);
708710
}
709711

710-
$this->whereNested(function ($query) use (
712+
$this->whereNested(function (Builder $query) use (
711713
$key,
712714
$first,
713715
$second,
@@ -741,6 +743,7 @@ public function orWhereBetween(
741743
*
742744
* @throws ReflectionException
743745
* @throws InvalidParamsException
746+
* @throws JsonException
744747
*/
745748
public function whereNotBetween(
746749
string $key,
@@ -754,7 +757,7 @@ public function whereNotBetween(
754757
$second = $this->castDate($second);
755758
}
756759

757-
$this->whereNested(function ($query) use (
760+
$this->whereNested(function (Builder $query) use (
758761
$key,
759762
$first,
760763
$second,
@@ -1046,20 +1049,20 @@ public function orderByDesc(string $key): self
10461049
public function with(array $relationships): self
10471050
{
10481051
$relationships = collect($relationships)->mapWithKeys(function (
1049-
$fields,
1050-
$relationship,
1052+
mixed $fields,
1053+
mixed $relationship,
10511054
) {
10521055
if (is_numeric($relationship)) {
10531056
return [$fields => ['*']];
10541057
}
10551058

10561059
return [$relationship => $fields];
1057-
})->map(function ($fields, $relationship) {
1060+
})->map(function (mixed $fields, mixed $relationship) {
10581061
if (collect($fields)->count() === 0) {
10591062
$fields = ['*'];
10601063
}
10611064

1062-
return collect($fields)->map(fn ($field) => $relationship . '.' . $field)->implode(',');
1065+
return collect($fields)->map(fn (mixed $field) => $relationship . '.' . $field)->implode(',');
10631066
})
10641067
->values()
10651068
->toArray();
@@ -1088,16 +1091,16 @@ public function cache(int $seconds): self
10881091
*/
10891092
public function getQuery(): string
10901093
{
1091-
return $this->query->map(function ($value, $key) {
1094+
return $this->query->map(function (mixed $value, string $key) {
10921095
if ($key === 'where') {
10931096
return collect($value)->unique()->implode(' ');
10941097
}
10951098
if ($key === 'fields') {
1096-
return collect($value)->unique()->sortBy(fn ($field) => count(explode('.', $field)))->implode(',');
1099+
return collect($value)->unique()->sortBy(fn (mixed $field) => count(explode('.', $field)))->implode(',');
10971100
}
10981101

10991102
return $value;
1100-
})->map(fn ($value, $key) => Str::finish($key . ' ' . $value, ';'))->unique()->sort()->implode("\n");
1103+
})->map(fn (mixed $value, string $key) => Str::finish($key . ' ' . $value, ';'))->unique()->sort()->implode("\n");
11011104
}
11021105

11031106
/**
@@ -1176,7 +1179,7 @@ public function get(): mixed
11761179
$data = $this->fetchApi();
11771180

11781181
if (isset($this->class) && $this->class) {
1179-
$data = collect($data)->map(fn ($result) => $this->mapToModel($result));
1182+
$data = collect($data)->map(fn (mixed $result) => $this->mapToModel($result));
11801183
}
11811184

11821185
$this->init();

src/Console/CreateWebhook.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,19 @@ private function getModels(): array
9595
$grep = preg_grep($pattern, $glob, PREG_GREP_INVERT);
9696

9797
return collect($grep ?: [])
98-
->map(fn ($path) => basename($path, '.php'))
98+
->map(fn (string $path): string => basename($path, '.php'))
9999
->toArray();
100100
}
101101

102102
private function getClosestModel(string $model): ?string
103103
{
104-
return collect($this->getModels())->map(fn ($m) => [
104+
return collect($this->getModels())->map(fn (string $m): array => [
105105
'model' => $m,
106106
'levenshtein' => levenshtein($m, $model),
107107
])
108-
->filter(fn ($m) => $m['levenshtein'] <= 5)
109-
->sortBy(fn ($m) => $m['levenshtein'])
110-
->map(fn ($m) => $m['model'])
108+
->filter(fn (array $m) => $m['levenshtein'] <= 5)
109+
->sortBy(fn (array $m) => $m['levenshtein'])
110+
->map(fn (array $m) => $m['model'])
111111
->first();
112112
}
113113
}

src/Models/Model.php

+11-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Carbon\Carbon;
1010
use Error;
1111
use Exception;
12-
use Illuminate\Contracts\Support\{Arrayable, Jsonable};
12+
use Illuminate\Contracts\Support\Arrayable;
1313
use Illuminate\Http\Client\RequestException;
1414
use Illuminate\Pagination\Paginator;
1515
use Illuminate\Support\Collection;
@@ -81,7 +81,7 @@
8181
* @method static \Illuminate\Support\Collection all()
8282
* @method static Paginator paginate(int $limit = 10)
8383
*/
84-
abstract class Model implements Arrayable, ArrayAccess, Jsonable
84+
abstract class Model implements Arrayable, ArrayAccess
8585
{
8686
public ?string $identifier;
8787
public Builder $builder;
@@ -175,13 +175,13 @@ protected function setIdentifier(): void
175175

176176
protected function setAttributes(array $attributes): void
177177
{
178-
$this->attributes = collect($attributes)->filter(function ($value) {
178+
$this->attributes = collect($attributes)->filter(function (mixed $value) {
179179
if (is_array($value)) {
180-
return collect($value)->filter(fn ($value) => is_object($value))->isEmpty();
180+
return collect($value)->filter(fn (mixed $value) => is_object($value))->isEmpty();
181181
}
182182

183183
return !is_object($value);
184-
})->map(function ($value, $key) {
184+
})->map(function (mixed $value, string $key) {
185185
$dates = collect($this->dates);
186186
if ($dates->contains($key)) {
187187
return Carbon::createFromTimestamp($value);
@@ -194,10 +194,10 @@ protected function setAttributes(array $attributes): void
194194
protected function setRelations(array $attributes): void
195195
{
196196
$this->relations = collect($attributes)
197-
->filter(fn ($value, $key) => array_key_exists($key, $this->casts))
198-
->map(function ($value, $key) {
197+
->filter(fn (mixed $value, string $key) => array_key_exists($key, $this->casts))
198+
->map(function (mixed $value, string $key) {
199199
if (is_array($value) && array_is_list($value)) {
200-
return collect($value)->map(fn ($value) => $this->mapToModel($key, $value))->filter();
200+
return collect($value)->map(fn (mixed $value) => $this->mapToModel($key, $value))->filter();
201201
}
202202

203203
return $this->mapToModel($key, $value);
@@ -243,7 +243,7 @@ private function mapToModel(string $property, mixed $value): mixed
243243
}
244244

245245
if (is_array($value)) {
246-
return collect($value)->map(function ($single) use ($class) {
246+
return collect($value)->map(function (mixed $single) use ($class) {
247247
if (!is_object($single)) {
248248
return $single;
249249
}
@@ -305,7 +305,7 @@ public function getEndpoint(): string
305305
public function toArray(): array
306306
{
307307
$attributes = collect($this->attributes);
308-
$relations = $this->relations->map(function ($relation) {
308+
$relations = $this->relations->map(function (mixed $relation) {
309309
if (!$relation instanceof Arrayable) {
310310
return $relation;
311311
}
@@ -318,10 +318,8 @@ public function toArray(): array
318318

319319
/**
320320
* Convert the object to its JSON representation.
321-
*
322-
* @param int $options
323321
*/
324-
public function toJson($options = 0): string
322+
public function toJson(int $options = 0): string
325323
{
326324
return collect($this->toArray())->toJson($options);
327325
}

src/Models/Webhook.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private function fill(mixed ...$parameters): void
204204

205205
private function mapToModel(\Illuminate\Support\Collection $collection): \Illuminate\Support\Collection
206206
{
207-
return $collection->map(function ($item) {
207+
return $collection->map(function (array $item) {
208208
$webhook = new self(...$item);
209209

210210
unset($webhook->client);

0 commit comments

Comments
 (0)