Skip to content

Commit

Permalink
[TypeDeclaration] Skip callable on return type of control (#1572)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 26, 2021
1 parent 3a2e85d commit dbd934e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector\Fixture;

use Rector\Tests\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector\Source\Nette\SomeControl;

interface SkipCallableAsConstructorControl
{
/**
* @param callable $callable
*/
public function create($callable): SomeControl;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\FunctionLike\ParamTypeDeclarationRector\Source\Nette;

use Nette\Application\UI\Control;

final class SomeControl extends Control
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
namespace Rector\TypeDeclaration\Rector\FunctionLike;

use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\UnionType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
Expand Down Expand Up @@ -209,6 +211,17 @@ private function shouldSkipParam(Param $param, ClassMethod | Function_ $function
return true;
}

// is nette return type?
$returnType = $functionLike->returnType;
if ($returnType instanceof FullyQualified) {
$objectType = new ObjectType('Nette\Application\UI\Control');
$returnObjectType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($returnType);

if ($objectType->isSuperTypeOf($returnObjectType)->yes()) {
return true;
}
}

// no type → check it
if ($param->type === null) {
return false;
Expand Down

0 comments on commit dbd934e

Please sign in to comment.