Skip to content

Commit 2ab2259

Browse files
committed
tests: fix types in stubs
1 parent d294a72 commit 2ab2259

9 files changed

+96
-46
lines changed

tests/_mocks/ItemStub.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@
88

99
class ItemStub extends Item
1010
{
11+
/**
12+
* @var array<int, string>
13+
*/
1114
protected $hidden = ['password'];
1215

16+
/**
17+
* @var array<string, string>
18+
*/
1319
protected $casts = [
1420
'age' => 'integer',
1521
'score' => 'float',
@@ -25,10 +31,16 @@ class ItemStub extends Item
2531
'foo' => 'bar',
2632
];
2733

34+
/**
35+
* @var array<int, string>
36+
*/
2837
protected $guarded = [
2938
'secret',
3039
];
3140

41+
/**
42+
* @var array<int, string>
43+
*/
3244
protected $fillable = [
3345
'name',
3446
'city',
@@ -42,34 +54,46 @@ class ItemStub extends Item
4254
'collection_data',
4355
];
4456

45-
public function getListItemsAttribute($value)
57+
/**
58+
* @return array<int, string>
59+
*/
60+
public function getListItemsAttribute(string $value): array
4661
{
47-
return json_decode($value, true);
62+
$result = json_decode($value, true);
63+
assert(is_array($result));
64+
65+
/** @var array<int, string> $result */
66+
return $result;
4867
}
4968

50-
public function setListItemsAttribute($value)
69+
/**
70+
* @param array<int, string> $value
71+
*/
72+
public function setListItemsAttribute(array $value): void
5173
{
5274
$this->attributes['list_items'] = json_encode($value);
5375
}
5476

55-
public function setBirthdayAttribute($value)
77+
public function setBirthdayAttribute(string $value): void
5678
{
5779
$this->attributes['birthday'] = strtotime($value);
5880
}
5981

60-
public function getBirthdayAttribute($value)
82+
public function getBirthdayAttribute(int $value): string
6183
{
6284
return date('Y-m-d', $value);
6385
}
6486

65-
public function getAgeAttribute($value)
87+
public function getAgeAttribute(mixed $value): int
6688
{
89+
assert(is_int($this->attributes['birthday']));
6790
$date = \DateTime::createFromFormat('U', (string) $this->attributes['birthday']);
91+
assert($date instanceof \DateTime);
6892

6993
return $date->diff(new \DateTime('now'))->y;
7094
}
7195

72-
public function getTestAttribute($value)
96+
public function getTestAttribute(mixed $value): string
7397
{
7498
return 'test';
7599
}

tests/_mocks/Items/AnotherRelatedItem.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88

99
class AnotherRelatedItem extends Item
1010
{
11-
/**
12-
* @var string
13-
*/
1411
protected $type = 'another-related-item';
1512

1613
/**
17-
* @var array
14+
* @var array<int, string>
1815
*/
1916
protected $visible = [
2017
'test_related_attribute1',

tests/_mocks/Items/ChildItem.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@
88

99
class ChildItem extends Item
1010
{
11-
/**
12-
* @var string
13-
*/
1411
protected $type = 'child';
1512

1613
/**
17-
* @var array
14+
* @var array<int, string>
1815
*/
1916
protected $visible = [
2017
'active',
2118
'description',
2219
];
2320

2421
/**
25-
* @var array
22+
* @var array<string, string>
2623
*/
2724
protected $casts = [
2825
'active' => 'bool',
2926
];
3027

28+
/**
29+
* @var array<string, mixed>
30+
*/
3131
protected $attributes = [
3232
'active' => true,
3333
];

tests/_mocks/Items/ParentItem.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44

55
namespace Swis\JsonApi\Client\Tests\Mocks\Items;
66

7+
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
8+
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
79
use Swis\JsonApi\Client\Item;
810

911
class ParentItem extends Item
1012
{
11-
/**
12-
* @var string
13-
*/
1413
protected $type = 'parent';
1514

1615
/**
17-
* @var array
16+
* @var array<int, string>
1817
*/
1918
protected $visible = [
2019
'active',
@@ -23,21 +22,21 @@ class ParentItem extends Item
2322
];
2423

2524
/**
26-
* @var array
25+
* @var array<string, string>
2726
*/
2827
protected $casts = [
2928
'active' => 'bool',
3029
];
3130

3231
/**
33-
* @var array
32+
* @var array<string, mixed>
3433
*/
3534
protected $attributes = [
3635
'active' => true,
3736
];
3837

3938
/**
40-
* @var array
39+
* @var array<int, string>
4140
*/
4241
protected $availableRelations = [
4342
'child',
@@ -47,27 +46,42 @@ class ParentItem extends Item
4746
'does_not_exist',
4847
];
4948

50-
public function child()
49+
/**
50+
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem>
51+
*/
52+
public function child(): OneRelationInterface
5153
{
5254
return $this->hasOne(ChildItem::class);
5355
}
5456

55-
public function children()
57+
/**
58+
* @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem>
59+
*/
60+
public function children(): ManyRelationInterface
5661
{
5762
return $this->hasMany(ChildItem::class);
5863
}
5964

60-
public function morph()
65+
/**
66+
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface>
67+
*/
68+
public function morph(): OneRelationInterface
6169
{
6270
return $this->morphTo();
6371
}
6472

65-
public function morphmany()
73+
/**
74+
* @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface>
75+
*/
76+
public function morphmany(): ManyRelationInterface
6677
{
6778
return $this->morphToMany();
6879
}
6980

70-
public function empty()
81+
/**
82+
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem>
83+
*/
84+
public function empty(): OneRelationInterface
7185
{
7286
return $this->hasOne(ChildItem::class);
7387
}

tests/_mocks/Items/RelatedItem.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@
44

55
namespace Swis\JsonApi\Client\Tests\Mocks\Items;
66

7+
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
78
use Swis\JsonApi\Client\Item;
89

910
class RelatedItem extends Item
1011
{
11-
/**
12-
* @var string
13-
*/
1412
protected $type = 'related-item';
1513

1614
/**
17-
* @var array
15+
* @var array<int, string>
1816
*/
1917
protected $visible = [
2018
'test_related_attribute1',
2119
'test_related_attribute2',
2220
];
2321

2422
/**
25-
* @var array
23+
* @var array<int, string>
2624
*/
2725
protected $availableRelations = [
2826
'parent_relation',
2927
];
3028

31-
public function parentRelation()
29+
/**
30+
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\WithRelationshipItem>
31+
*/
32+
public function parentRelation(): OneRelationInterface
3233
{
3334
return $this->hasOne(WithRelationshipItem::class);
3435
}

tests/_mocks/Items/WithHiddenItem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class WithHiddenItem extends Item
1010
{
11+
/**
12+
* @var array<int, string>
13+
*/
1114
protected $hidden = [
1215
'testKey',
1316
];

tests/_mocks/Items/WithRelationshipItem.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44

55
namespace Swis\JsonApi\Client\Tests\Mocks\Items;
66

7+
use Swis\JsonApi\Client\Interfaces\ManyRelationInterface;
8+
use Swis\JsonApi\Client\Interfaces\OneRelationInterface;
79
use Swis\JsonApi\Client\Item;
810

911
class WithRelationshipItem extends Item
1012
{
11-
/**
12-
* @var string
13-
*/
1413
protected $type = 'item-with-relationship';
1514

1615
/**
17-
* @var array
16+
* @var array<int, string>
1817
*/
1918
protected $visible = [
2019
'test_attribute_1',
2120
'test_attribute_2',
2221
];
2322

2423
/**
25-
* @var array
24+
* @var array<int, string>
2625
*/
2726
protected $availableRelations = [
2827
'hasone_relation',
@@ -31,22 +30,34 @@ class WithRelationshipItem extends Item
3130
'morphtomany_relation',
3231
];
3332

34-
public function hasoneRelation()
33+
/**
34+
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\RelatedItem>
35+
*/
36+
public function hasoneRelation(): OneRelationInterface
3537
{
3638
return $this->hasOne(RelatedItem::class);
3739
}
3840

39-
public function hasmanyRelation()
41+
/**
42+
* @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\RelatedItem>
43+
*/
44+
public function hasmanyRelation(): ManyRelationInterface
4045
{
4146
return $this->hasMany(RelatedItem::class);
4247
}
4348

44-
public function morphtoRelation()
49+
/**
50+
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface>
51+
*/
52+
public function morphtoRelation(): OneRelationInterface
4553
{
4654
return $this->morphTo();
4755
}
4856

49-
public function morphtomanyRelation()
57+
/**
58+
* @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface>
59+
*/
60+
public function morphtomanyRelation(): ManyRelationInterface
5061
{
5162
return $this->morphToMany();
5263
}

tests/_mocks/Items/WithoutRelationshipsItem.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,5 @@
88

99
class WithoutRelationshipsItem extends Item
1010
{
11-
/**
12-
* @var string
13-
*/
1411
protected $type = 'item-without-relationships';
1512
}

tests/_mocks/MockRepository.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use Swis\JsonApi\Client\Repository;
88

9+
/**
10+
* @implements \Swis\JsonApi\Client\Interfaces\RepositoryInterface<\Swis\JsonApi\Client\Tests\Mocks\ItemStub>
11+
*/
912
class MockRepository extends Repository
1013
{
1114
protected $endpoint = 'mocks';

0 commit comments

Comments
 (0)