Skip to content

Commit

Permalink
[TypeDeclaration] Skip set type by @param doc at TypedPropertyFromAss…
Browse files Browse the repository at this point in the history
…ignsRector (#1822)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 17, 2022
1 parent ecce9b3 commit 6c0a532
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector\Fixture;

final class SkipSetTypeByParamDoc
{
private $property;

/**
* @param int $property
*/
public function __construct($property)
{
$this->property = $property;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use Rector\Core\NodeAnalyzer\ExprAnalyzer;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
Expand All @@ -31,6 +32,7 @@ public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly TypeFactory $typeFactory,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly ExprAnalyzer $exprAnalyzer
) {
}

Expand All @@ -51,6 +53,10 @@ public function inferPropertyInClassLike(string $propertyName, ClassLike $classL
return null;
}

if ($this->exprAnalyzer->isNonTypedFromParam($node->expr)) {
return null;
}

$assignedExprTypes[] = $this->resolveExprStaticTypeIncludingDimFetch($node);

return null;
Expand Down
17 changes: 16 additions & 1 deletion src/NodeAnalyzer/ExprAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;

final class ExprAnalyzer
{
public function __construct(
private readonly NodeComparator $nodeComparator,
private readonly BetterNodeFinder $betterNodeFinder,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly NodeNameResolver $nodeNameResolver
) {
}

Expand All @@ -34,13 +39,23 @@ public function isNonTypedFromParam(Expr $expr): bool
return false;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);

$params = $functionLike->getParams();
foreach ($params as $param) {
if (! $this->nodeComparator->areNodesEqual($param->var, $expr)) {
continue;
}

return $param->type === null;
$paramName = $this->nodeNameResolver->getName($param->var);

if ($paramName === null) {
continue;
}

$paramTag = $phpDocInfo->getParamTagValueByName($paramName);

return $paramTag instanceof ParamTagValueNode && $param->type === null;
}

return false;
Expand Down

0 comments on commit 6c0a532

Please sign in to comment.