Skip to content

Commit cdf9cb5

Browse files
committed
Scalar type in PHPDoc can mean an existing class
1 parent 8f2e45c commit cdf9cb5

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
156156
return new BenevolentUnionType([new IntegerType(), new StringType()]);
157157

158158
case 'scalar':
159+
$type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope);
160+
161+
if ($type !== null) {
162+
return $type;
163+
}
164+
159165
return new UnionType([new IntegerType(), new FloatType(), new StringType(), new BooleanType()]);
160166

161167
case 'number':

tests/PHPStan/Rules/Generics/ClassAncestorsRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,9 @@ public function testCrossCheckInterfaces(): void
216216
]);
217217
}
218218

219+
public function testScalarClassName(): void
220+
{
221+
$this->analyse([__DIR__ . '/data/scalar-class-name.php'], []);
222+
}
223+
219224
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace MaxGoryunov\SavingIterator\Src;
4+
/**
5+
* @template T
6+
*/
7+
interface Scalar
8+
{
9+
/**
10+
* @return T
11+
*/
12+
public function value(): mixed;
13+
}
14+
15+
namespace MaxGoryunov\SavingIterator\Fakes;
16+
17+
use Closure;
18+
use MaxGoryunov\SavingIterator\Src\Scalar;
19+
20+
/**
21+
* @template T
22+
* @implements Scalar<T>
23+
*/
24+
class The implements Scalar
25+
{
26+
/**
27+
* @param T $subject
28+
* @param Closure(T): mixed $context
29+
*/
30+
public function __construct(
31+
/**
32+
* @var T
33+
*/
34+
private mixed $subject,
35+
/**
36+
* @var Closure(T): mixed
37+
*/
38+
private Closure $context
39+
) {
40+
}
41+
/**
42+
* @return T
43+
*/
44+
public function value(): mixed
45+
{
46+
($this->context)($this->subject);
47+
return $this->subject;
48+
}
49+
}

0 commit comments

Comments
 (0)