Skip to content

Commit

Permalink
Scalar type in PHPDoc can mean an existing class
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 30, 2021
1 parent 8f2e45c commit cdf9cb5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
return new BenevolentUnionType([new IntegerType(), new StringType()]);

case 'scalar':
$type = $this->tryResolvePseudoTypeClassType($typeNode, $nameScope);

if ($type !== null) {
return $type;
}

return new UnionType([new IntegerType(), new FloatType(), new StringType(), new BooleanType()]);

case 'number':
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Generics/ClassAncestorsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,9 @@ public function testCrossCheckInterfaces(): void
]);
}

public function testScalarClassName(): void
{
$this->analyse([__DIR__ . '/data/scalar-class-name.php'], []);
}

}
49 changes: 49 additions & 0 deletions tests/PHPStan/Rules/Generics/data/scalar-class-name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace MaxGoryunov\SavingIterator\Src;
/**
* @template T
*/
interface Scalar
{
/**
* @return T
*/
public function value(): mixed;
}

namespace MaxGoryunov\SavingIterator\Fakes;

use Closure;
use MaxGoryunov\SavingIterator\Src\Scalar;

/**
* @template T
* @implements Scalar<T>
*/
class The implements Scalar
{
/**
* @param T $subject
* @param Closure(T): mixed $context
*/
public function __construct(
/**
* @var T
*/
private mixed $subject,
/**
* @var Closure(T): mixed
*/
private Closure $context
) {
}
/**
* @return T
*/
public function value(): mixed
{
($this->context)($this->subject);
return $this->subject;
}
}

0 comments on commit cdf9cb5

Please sign in to comment.