-
-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TypeDeclaratoin] Add class method param type resolving by property
- Loading branch information
1 parent
ac29fa4
commit 4360401
Showing
4 changed files
with
156 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
rules/type-declaration/src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\TypeDeclaration\TypeInferer\ParamTypeInferer; | ||
|
||
use PhpParser\Node\FunctionLike; | ||
use PhpParser\Node\Param; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\Type; | ||
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Rector\TypeDeclaration\Contract\TypeInferer\ParamTypeInfererInterface; | ||
use Rector\TypeDeclaration\TypeInferer\AbstractTypeInferer; | ||
|
||
final class FunctionLikeDocParamTypeInferer extends AbstractTypeInferer implements ParamTypeInfererInterface | ||
{ | ||
public function __construct(NodeNameResolver $nodeNameResolver) | ||
{ | ||
$this->nodeNameResolver = $nodeNameResolver; | ||
} | ||
|
||
public function inferParam(Param $param): Type | ||
{ | ||
$functionLike = $this->resolveScopeNode($param); | ||
if ($functionLike === null) { | ||
return new MixedType(); | ||
} | ||
|
||
/** @var PhpDocInfo|null $phpDocInfo */ | ||
$phpDocInfo = $functionLike->getAttribute(AttributeKey::PHP_DOC_INFO); | ||
if ($phpDocInfo === null) { | ||
return new MixedType(); | ||
} | ||
|
||
$paramWithTypes = $phpDocInfo->getParamTypesByName(); | ||
if ($paramWithTypes === []) { | ||
return new MixedType(); | ||
} | ||
|
||
return $this->matchParamNodeFromDoc($paramWithTypes, $param); | ||
} | ||
|
||
/** | ||
* @param Type[] $paramWithTypes | ||
*/ | ||
private function matchParamNodeFromDoc(array $paramWithTypes, Param $param): Type | ||
{ | ||
$paramNodeName = '$' . $this->nodeNameResolver->getName($param->var); | ||
return $paramWithTypes[$paramNodeName] ?? new MixedType(); | ||
} | ||
|
||
private function resolveScopeNode(Param $param): ?FunctionLike | ||
{ | ||
return $param->getAttribute(AttributeKey::METHOD_NODE) ?? $param->getAttribute(AttributeKey::FUNCTION_NODE); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...s/Rector/FunctionLike/ParamTypeDeclarationRector/Fixture/infer_from_property_type.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ParamTypeDeclarationRector\Fixture; | ||
|
||
class InferFromPropertyType | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $id; | ||
|
||
public function setId($id) | ||
{ | ||
$this->id = $id; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\TypeDeclaration\Tests\Rector\ClassMethod\ParamTypeDeclarationRector\Fixture; | ||
|
||
class InferFromPropertyType | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $id; | ||
|
||
public function setId(int $id) | ||
{ | ||
$this->id = $id; | ||
} | ||
} | ||
|
||
?> |