Skip to content

Commit 4db549c

Browse files
committed
Fixed array for data collections bug
1 parent 1ae8c6a commit 4db549c

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/SpatieLaravelData/Rules/ValidTypeRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Rules\RuleErrorBuilder;
1111
use PHPStan\ShouldNotHappenException;
1212
use Cego\phpstan\TypeSystem\UnionType;
13+
use Spatie\LaravelData\DataCollection;
1314
use Cego\phpstan\TypeSystem\TypeSystem;
1415
use Cego\phpstan\SpatieLaravelData\Data\Call;
1516
use Cego\phpstan\SpatieLaravelData\Data\Constructor;
@@ -180,7 +181,9 @@ public static function getErrorMessage(string $property, string $class, string $
180181
*/
181182
private function expectedTypesMatchesExactlyCast(array $casts, UnionType $expectedTypes): bool
182183
{
183-
foreach ($casts as $castType) {
184+
$systemCasts = [UnionType::fromString(DataCollection::class)];
185+
186+
foreach ([...$casts, ...$systemCasts] as $castType) {
184187
if (TypeSystem::isSubtypeOf($expectedTypes, $castType)) {
185188
return true;
186189
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Test\Samples;
4+
5+
use Spatie\LaravelData\Data;
6+
use Spatie\LaravelData\DataCollection;
7+
use Test\Samples\Enum\BackedEnumExample;
8+
9+
class DataCollectionSpatieLaravelData extends Data
10+
{
11+
/**
12+
* @param DataCollection<int, BackedEnumExample> $collectionProperty
13+
*/
14+
public function __construct(
15+
public readonly DataCollection $collectionProperty,
16+
) {
17+
}
18+
19+
public function initDefault(): self
20+
{
21+
return self::from(
22+
[
23+
'collectionProperty' => [],
24+
],
25+
);
26+
}
27+
}

test/SpatieLaravelData/ValidTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ public function it_does_not_care_about_generics(): void
100100
], []);
101101
}
102102

103+
/** @test */
104+
public function it_accepts_arrays_for_data_collections(): void
105+
{
106+
$this->analyse([
107+
__DIR__ . '/../Samples/DataCollectionSpatieLaravelData.php',
108+
], []);
109+
}
110+
103111
private function expectError(int $line, string $property, string $class, string $expectedType, string $actualType): array
104112
{
105113
return [

0 commit comments

Comments
 (0)