Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/SpatieLaravelData/Rules/ValidTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use Cego\phpstan\TypeSystem\UnionType;
use Spatie\LaravelData\DataCollection;
use Cego\phpstan\TypeSystem\TypeSystem;
use Cego\phpstan\SpatieLaravelData\Data\Call;
use Cego\phpstan\SpatieLaravelData\Data\Constructor;
Expand Down Expand Up @@ -180,7 +181,9 @@ public static function getErrorMessage(string $property, string $class, string $
*/
private function expectedTypesMatchesExactlyCast(array $casts, UnionType $expectedTypes): bool
{
foreach ($casts as $castType) {
$systemCasts = [UnionType::fromString(DataCollection::class)];

foreach ([...$casts, ...$systemCasts] as $castType) {
if (TypeSystem::isSubtypeOf($expectedTypes, $castType)) {
return true;
}
Expand Down
27 changes: 27 additions & 0 deletions test/Samples/DataCollectionSpatieLaravelData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Test\Samples;

use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Test\Samples\Enum\BackedEnumExample;

class DataCollectionSpatieLaravelData extends Data
{
/**
* @param DataCollection<int, BackedEnumExample> $collectionProperty
*/
public function __construct(
public readonly DataCollection $collectionProperty,
) {
}

public function initDefault(): self
{
return self::from(
[
'collectionProperty' => [],
],
);
}
}
8 changes: 8 additions & 0 deletions test/SpatieLaravelData/ValidTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public function it_does_not_care_about_generics(): void
], []);
}

/** @test */
public function it_accepts_arrays_for_data_collections(): void
{
$this->analyse([
__DIR__ . '/../Samples/DataCollectionSpatieLaravelData.php',
], []);
}

private function expectError(int $line, string $property, string $class, string $expectedType, string $actualType): array
{
return [
Expand Down