Skip to content

Commit cd2ae90

Browse files
committed
Added fix for bug with class generics
1 parent 76ecff2 commit cd2ae90

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/TypeSystem/Type.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
namespace Cego\phpstan\TypeSystem;
44

55
use Stringable;
6+
use Illuminate\Support\Str;
67

78
class Type implements Stringable
89
{
10+
public readonly string $type;
11+
912
/**
1013
* Constructor
1114
*
1215
* @param string $type
1316
*/
14-
public function __construct(public readonly string $type)
17+
public function __construct(string $type)
1518
{
19+
if (empty($type)) {
20+
$this->type = 'mixed';
21+
} else {
22+
$this->type = Str::of($type)->replaceMatches('/<[^<>]*>/', '')->toString();
23+
}
1624
}
1725

1826
/**
@@ -32,8 +40,7 @@ public function isNotReal(): bool
3240
*/
3341
public function isMixed(): bool
3442
{
35-
return empty($this->type)
36-
|| strtolower($this->type) === 'mixed';
43+
return strtolower($this->type) === 'mixed';
3744
}
3845

3946
/**

test/Samples/EmptySpatieLaravelData.php

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

55
use Spatie\LaravelData\Data;
66

7+
/**
8+
* @template key
9+
*/
710
class EmptySpatieLaravelData extends Data
811
{
912
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Test\Samples;
4+
5+
use Spatie\LaravelData\Data;
6+
7+
class GenericsSpatieLaravelData extends Data
8+
{
9+
public function __construct(
10+
public readonly EmptySpatieLaravelData $genericProperty,
11+
) {
12+
}
13+
14+
public function initDefault(): self
15+
{
16+
/** @var EmptySpatieLaravelData<string> $var */
17+
$var = '';
18+
19+
return self::from([
20+
'genericProperty' => $var,
21+
]);
22+
}
23+
}

test/SpatieLaravelData/ValidTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ public function it_ignores_potential_problems_for_objects_with_casts(): void
7878
], []);
7979
}
8080

81+
/** @test */
82+
public function it_does_not_care_about_generics(): void
83+
{
84+
$this->analyse([
85+
__DIR__ . '/../Samples/GenericsSpatieLaravelData.php',
86+
], []);
87+
}
88+
8189
private function expectError(int $line, string $property, string $class, string $expectedType, string $actualType): array
8290
{
8391
return [

0 commit comments

Comments
 (0)