Skip to content

Commit

Permalink
Support class-const-fetch in ReturnTypeFromStrictTypedCallRector (#4520)
Browse files Browse the repository at this point in the history
* Support class-const-fetch in ReturnTypeFromStrictTypedCallRector

* Create skip_unknown_constant_int_returns.php.inc

* Update skip_unknown_constant_int_returns.php.inc

* Create skip_wrong_typed_constant.php.inc

* Create wrong_typed_constant_returns.php.inc
  • Loading branch information
staabm committed Jul 16, 2023
1 parent 818503e commit cebcea0
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class SameTypedIntConstantReturns
{
const INT = 0;

public function getData()
{
if (rand(0,1)) {
return self::INT;
}

return $this->getInt();
}

public function getInt(): int
{

}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class SameTypedIntConstantReturns
{
const INT = 0;

public function getData(): int
{
if (rand(0,1)) {
return self::INT;
}

return $this->getInt();
}

public function getInt(): int
{

}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class SkipUnknownConstantIntReturns
{
public function getData()
{
if (rand(0,1)) {
return self::NOT_DEFINED;
}

return $this->getInt();
}

public function getInt(): int
{

}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class SkipWrongTypedConstant
{
const STRING = 'hello';

public function getData()
{
if (rand(0,1)) {
return self::STRING;
}

return $this->getInt();
}

public function getInt(): int
{

}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class WrongTypedIntConstant
{
/**
* @var string
*/
const WRONG_TYPED = 0;

public function getData()
{
if (rand(0,1)) {
return self::WRONG_TYPED;
}

return $this->getInt();
}

public function getInt(): int
{

}
}

?>
-----
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector\Fixture;

final class WrongTypedIntConstant
{
/**
* @var string
*/
const WRONG_TYPED = 0;

public function getData(): int
{
if (rand(0,1)) {
return self::WRONG_TYPED;
}

return $this->getInt();
}

public function getInt(): int
{

}
}

?>
13 changes: 13 additions & 0 deletions rules/TypeDeclaration/TypeAnalyzer/ReturnStrictTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function collectStrictReturnTypes(array $returns, Scope $scope): array
if ($returnedExpr instanceof MethodCall || $returnedExpr instanceof StaticCall || $returnedExpr instanceof FuncCall) {
$containsStrictCall = true;
$returnNode = $this->resolveMethodCallReturnNode($returnedExpr);
} elseif ($returnedExpr instanceof Expr\ClassConstFetch) {
$returnNode = $this->resolveConstFetchReturnNode($returnedExpr, $scope);
} elseif (
$returnedExpr instanceof Array_
|| $returnedExpr instanceof String_
Expand Down Expand Up @@ -106,4 +108,15 @@ private function resolveLiteralReturnNode(Array_|Scalar $returnedExpr, Scope $sc
$returnType = $scope->getType($returnedExpr);
return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($returnType, TypeKind::RETURN);
}

private function resolveConstFetchReturnNode(Expr\ClassConstFetch $returnedExpr, Scope $scope): ?Node
{
$constType = $scope->getType($returnedExpr);

if ($constType instanceof MixedType) {
return null;
}

return $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($constType, TypeKind::RETURN);
}
}

0 comments on commit cebcea0

Please sign in to comment.