2020-06-08 09:28:03 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
|
namespace Rector\Php80\Rector\Class_;
|
2020-06-08 09:28:03 +00:00
|
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use PhpParser\Node;
|
2023-05-14 15:29:04 +00:00
|
|
|
|
use PhpParser\Node\Expr;
|
2023-12-24 00:51:27 +00:00
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
2023-05-19 13:30:27 +00:00
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use PhpParser\Node\Identifier;
|
|
|
|
|
use PhpParser\Node\NullableType;
|
|
|
|
|
use PhpParser\Node\Param;
|
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
|
use PhpParser\Node\UnionType;
|
|
|
|
|
use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
|
2023-07-20 07:29:04 +00:00
|
|
|
|
use PHPStan\Reflection\ClassReflection;
|
2023-06-06 10:31:04 +00:00
|
|
|
|
use PHPStan\Type\MixedType;
|
|
|
|
|
use PHPStan\Type\TypeCombinator;
|
2023-09-20 12:41:26 +00:00
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2024-01-02 02:40:38 +00:00
|
|
|
|
use Rector\Contract\Rector\ConfigurableRectorInterface;
|
2023-09-11 21:30:42 +00:00
|
|
|
|
use Rector\Naming\PropertyRenamer\PropertyPromotionRenamer;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use Rector\Naming\VariableRenamer;
|
2024-01-02 02:40:38 +00:00
|
|
|
|
use Rector\NodeAnalyzer\ParamAnalyzer;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2023-06-06 10:31:04 +00:00
|
|
|
|
use Rector\NodeTypeResolver\TypeComparator\TypeComparator;
|
2023-09-11 13:16:45 +00:00
|
|
|
|
use Rector\Php80\DocBlock\PropertyPromotionDocBlockMerger;
|
2022-11-29 15:48:49 +00:00
|
|
|
|
use Rector\Php80\Guard\MakePropertyPromotionGuard;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use Rector\Php80\NodeAnalyzer\PromotedPropertyCandidateResolver;
|
|
|
|
|
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
|
2024-01-02 02:40:38 +00:00
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
|
use Rector\Reflection\ReflectionResolver;
|
2023-09-29 12:48:57 +00:00
|
|
|
|
use Rector\StaticTypeMapper\StaticTypeMapper;
|
2024-01-02 02:40:38 +00:00
|
|
|
|
use Rector\ValueObject\MethodName;
|
|
|
|
|
use Rector\ValueObject\PhpVersionFeature;
|
2022-06-06 17:12:56 +00:00
|
|
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
2022-11-29 15:48:49 +00:00
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
2022-06-07 09:18:30 +00:00
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-06-08 09:28:03 +00:00
|
|
|
|
/**
|
2021-04-10 18:47:17 +00:00
|
|
|
|
* @changelog https://wiki.php.net/rfc/constructor_promotion https://github.com/php/php-src/pull/5291
|
2020-06-08 09:28:03 +00:00
|
|
|
|
*
|
2021-03-12 22:20:25 +00:00
|
|
|
|
* @see \Rector\Tests\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector\ClassPropertyAssignToConstructorPromotionRectorTest
|
2020-06-08 09:28:03 +00:00
|
|
|
|
*/
|
2023-07-19 11:08:33 +00:00
|
|
|
|
final class ClassPropertyAssignToConstructorPromotionRector extends AbstractRector implements MinPhpVersionInterface, ConfigurableRectorInterface
|
2020-06-08 09:28:03 +00:00
|
|
|
|
{
|
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
|
* @readonly
|
2021-06-10 10:46:24 +00:00
|
|
|
|
* @var \Rector\Php80\NodeAnalyzer\PromotedPropertyCandidateResolver
|
2020-06-08 09:28:03 +00:00
|
|
|
|
*/
|
2021-06-18 19:29:49 +00:00
|
|
|
|
private $promotedPropertyCandidateResolver;
|
2020-12-09 23:40:38 +00:00
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
|
* @var \Rector\Naming\VariableRenamer
|
2020-12-09 23:40:38 +00:00
|
|
|
|
*/
|
|
|
|
|
private $variableRenamer;
|
2021-07-14 07:23:54 +00:00
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
|
* @var \Rector\NodeAnalyzer\ParamAnalyzer
|
2021-07-14 07:23:54 +00:00
|
|
|
|
*/
|
|
|
|
|
private $paramAnalyzer;
|
2021-08-23 11:12:57 +00:00
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
|
* @readonly
|
2023-09-11 13:16:45 +00:00
|
|
|
|
* @var \Rector\Php80\DocBlock\PropertyPromotionDocBlockMerger
|
2021-08-23 11:12:57 +00:00
|
|
|
|
*/
|
2023-09-11 13:16:45 +00:00
|
|
|
|
private $propertyPromotionDocBlockMerger;
|
2021-10-29 14:22:26 +00:00
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
|
* @readonly
|
2022-11-29 15:48:49 +00:00
|
|
|
|
* @var \Rector\Php80\Guard\MakePropertyPromotionGuard
|
2021-10-29 14:22:26 +00:00
|
|
|
|
*/
|
2022-11-29 15:48:49 +00:00
|
|
|
|
private $makePropertyPromotionGuard;
|
2023-06-06 10:31:04 +00:00
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
|
* @readonly
|
2023-06-06 10:31:04 +00:00
|
|
|
|
* @var \Rector\NodeTypeResolver\TypeComparator\TypeComparator
|
|
|
|
|
*/
|
|
|
|
|
private $typeComparator;
|
2023-07-20 07:24:15 +00:00
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
|
* @var \Rector\Reflection\ReflectionResolver
|
2023-07-20 07:24:15 +00:00
|
|
|
|
*/
|
|
|
|
|
private $reflectionResolver;
|
2023-09-11 21:30:42 +00:00
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @var \Rector\Naming\PropertyRenamer\PropertyPromotionRenamer
|
|
|
|
|
*/
|
|
|
|
|
private $propertyPromotionRenamer;
|
2023-09-20 12:41:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
|
*/
|
|
|
|
|
private $phpDocInfoFactory;
|
2023-09-29 12:48:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* @readonly
|
|
|
|
|
* @var \Rector\StaticTypeMapper\StaticTypeMapper
|
|
|
|
|
*/
|
|
|
|
|
private $staticTypeMapper;
|
2023-06-08 22:00:17 +00:00
|
|
|
|
/**
|
|
|
|
|
* @api
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public const INLINE_PUBLIC = 'inline_public';
|
2024-01-11 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* @api
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
public const RENAME_PROPERTY = 'rename_property';
|
2023-06-08 22:00:17 +00:00
|
|
|
|
/**
|
|
|
|
|
* Default to false, which only apply changes:
|
|
|
|
|
*
|
|
|
|
|
* – private modifier property
|
|
|
|
|
* - protected/public modifier property when property typed
|
|
|
|
|
*
|
|
|
|
|
* Set to true will allow change whether property is typed or not as far as not forbidden, eg: callable type, null type, etc.
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $inlinePublic = \false;
|
2024-01-11 23:02:48 +00:00
|
|
|
|
/**
|
|
|
|
|
* Set to false will skip property promotion when parameter and property have different names.
|
|
|
|
|
* @var bool
|
|
|
|
|
*/
|
|
|
|
|
private $renameProperty = \true;
|
2023-09-29 12:48:57 +00:00
|
|
|
|
public function __construct(PromotedPropertyCandidateResolver $promotedPropertyCandidateResolver, VariableRenamer $variableRenamer, ParamAnalyzer $paramAnalyzer, PropertyPromotionDocBlockMerger $propertyPromotionDocBlockMerger, MakePropertyPromotionGuard $makePropertyPromotionGuard, TypeComparator $typeComparator, ReflectionResolver $reflectionResolver, PropertyPromotionRenamer $propertyPromotionRenamer, PhpDocInfoFactory $phpDocInfoFactory, StaticTypeMapper $staticTypeMapper)
|
2021-05-09 20:15:43 +00:00
|
|
|
|
{
|
2021-06-18 19:29:49 +00:00
|
|
|
|
$this->promotedPropertyCandidateResolver = $promotedPropertyCandidateResolver;
|
2020-12-09 23:40:38 +00:00
|
|
|
|
$this->variableRenamer = $variableRenamer;
|
2021-07-14 07:23:54 +00:00
|
|
|
|
$this->paramAnalyzer = $paramAnalyzer;
|
2023-09-11 13:16:45 +00:00
|
|
|
|
$this->propertyPromotionDocBlockMerger = $propertyPromotionDocBlockMerger;
|
2022-11-29 15:48:49 +00:00
|
|
|
|
$this->makePropertyPromotionGuard = $makePropertyPromotionGuard;
|
2023-06-06 10:31:04 +00:00
|
|
|
|
$this->typeComparator = $typeComparator;
|
2023-07-20 07:24:15 +00:00
|
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2023-09-11 21:30:42 +00:00
|
|
|
|
$this->propertyPromotionRenamer = $propertyPromotionRenamer;
|
2023-09-20 12:41:26 +00:00
|
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2023-09-29 12:48:57 +00:00
|
|
|
|
$this->staticTypeMapper = $staticTypeMapper;
|
2020-12-09 23:40:38 +00:00
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-06-08 09:28:03 +00:00
|
|
|
|
{
|
2022-11-29 15:48:49 +00:00
|
|
|
|
return new RuleDefinition('Change simple property init and assign to constructor promotion', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2020-06-08 09:28:03 +00:00
|
|
|
|
class SomeClass
|
|
|
|
|
{
|
2023-12-24 00:51:27 +00:00
|
|
|
|
public float $price;
|
2020-12-09 23:40:38 +00:00
|
|
|
|
|
2021-11-25 19:08:52 +00:00
|
|
|
|
public function __construct(
|
2023-12-24 00:51:27 +00:00
|
|
|
|
float $price = 0.0
|
2021-11-25 19:08:52 +00:00
|
|
|
|
) {
|
2023-12-24 00:51:27 +00:00
|
|
|
|
$this->price = $price;
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
|
, <<<'CODE_SAMPLE'
|
2020-06-08 09:28:03 +00:00
|
|
|
|
class SomeClass
|
|
|
|
|
{
|
2021-11-25 19:08:52 +00:00
|
|
|
|
public function __construct(
|
2023-12-24 00:51:27 +00:00
|
|
|
|
public float $price = 0.0
|
2021-11-25 19:08:52 +00:00
|
|
|
|
) {
|
2020-12-09 23:40:38 +00:00
|
|
|
|
}
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
|
CODE_SAMPLE
|
2024-01-11 23:02:48 +00:00
|
|
|
|
, [\Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::INLINE_PUBLIC => \false, \Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector::RENAME_PROPERTY => \true])]);
|
2022-11-29 15:48:49 +00:00
|
|
|
|
}
|
|
|
|
|
public function configure(array $configuration) : void
|
|
|
|
|
{
|
2024-01-11 23:02:48 +00:00
|
|
|
|
$this->inlinePublic = $configuration[self::INLINE_PUBLIC] ?? \false;
|
|
|
|
|
$this->renameProperty = $configuration[self::RENAME_PROPERTY] ?? \true;
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
|
* @return array<class-string<Node>>
|
2020-06-08 09:28:03 +00:00
|
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
|
public function getNodeTypes() : array
|
2020-06-08 09:28:03 +00:00
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
|
return [Class_::class];
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
|
* @param Class_ $node
|
2020-06-08 09:28:03 +00:00
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
|
public function refactor(Node $node) : ?Node
|
2020-06-08 09:28:03 +00:00
|
|
|
|
{
|
2023-06-05 15:41:37 +00:00
|
|
|
|
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
|
|
|
|
|
if (!$constructClassMethod instanceof ClassMethod) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$promotionCandidates = $this->promotedPropertyCandidateResolver->resolveFromClass($node, $constructClassMethod);
|
2020-06-08 09:28:03 +00:00
|
|
|
|
if ($promotionCandidates === []) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-09-11 13:16:45 +00:00
|
|
|
|
$constructorPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($constructClassMethod);
|
|
|
|
|
$classReflection = $this->reflectionResolver->resolveClassReflection($node);
|
|
|
|
|
if (!$classReflection instanceof ClassReflection) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-12-09 23:40:38 +00:00
|
|
|
|
foreach ($promotionCandidates as $promotionCandidate) {
|
2020-12-30 19:31:55 +00:00
|
|
|
|
$param = $promotionCandidate->getParam();
|
2021-06-30 13:08:35 +00:00
|
|
|
|
if ($this->shouldSkipParam($param)) {
|
2021-05-17 18:55:10 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-09-11 13:16:45 +00:00
|
|
|
|
$property = $promotionCandidate->getProperty();
|
2023-07-20 07:24:15 +00:00
|
|
|
|
if (!$this->makePropertyPromotionGuard->isLegal($node, $classReflection, $property, $param, $this->inlinePublic)) {
|
2021-10-29 14:22:26 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-01-11 23:02:48 +00:00
|
|
|
|
$paramName = $this->getName($param);
|
|
|
|
|
// rename also following calls
|
|
|
|
|
$propertyName = $this->getName($property->props[0]);
|
|
|
|
|
if (!$this->renameProperty && $paramName !== $propertyName) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-09-11 13:16:45 +00:00
|
|
|
|
// remove property from class
|
2023-06-05 15:41:37 +00:00
|
|
|
|
$propertyStmtKey = $property->getAttribute(AttributeKey::STMT_KEY);
|
|
|
|
|
unset($node->stmts[$propertyStmtKey]);
|
2023-09-11 13:16:45 +00:00
|
|
|
|
// remove assign in constructor
|
2023-06-05 15:41:37 +00:00
|
|
|
|
$assignStmtPosition = $promotionCandidate->getStmtPosition();
|
|
|
|
|
unset($constructClassMethod->stmts[$assignStmtPosition]);
|
2020-12-09 23:40:38 +00:00
|
|
|
|
/** @var string $oldName */
|
|
|
|
|
$oldName = $this->getName($param->var);
|
2021-05-29 22:10:59 +00:00
|
|
|
|
$this->variableRenamer->renameVariableInFunctionLike($constructClassMethod, $oldName, $propertyName, null);
|
2023-09-11 13:16:45 +00:00
|
|
|
|
$paramTagValueNode = $constructorPhpDocInfo->getParamTagValueByName($paramName);
|
2022-06-07 08:22:29 +00:00
|
|
|
|
if (!$paramTagValueNode instanceof ParamTagValueNode) {
|
2023-09-11 13:16:45 +00:00
|
|
|
|
$this->propertyPromotionDocBlockMerger->decorateParamWithPropertyPhpDocInfo($constructClassMethod, $property, $param, $paramName);
|
2021-05-06 10:59:29 +00:00
|
|
|
|
} elseif ($paramTagValueNode->parameterName !== '$' . $propertyName) {
|
2023-09-11 21:30:42 +00:00
|
|
|
|
$this->propertyPromotionRenamer->renameParamDoc($constructorPhpDocInfo, $constructClassMethod, $param, $paramTagValueNode->parameterName, $propertyName);
|
2021-05-06 10:59:29 +00:00
|
|
|
|
}
|
2020-06-08 09:28:03 +00:00
|
|
|
|
// property name has higher priority
|
2023-05-19 13:30:27 +00:00
|
|
|
|
$paramName = $this->getName($property);
|
|
|
|
|
$param->var = new Variable($paramName);
|
2020-06-08 09:28:03 +00:00
|
|
|
|
$param->flags = $property->flags;
|
2023-09-11 13:16:45 +00:00
|
|
|
|
// copy attributes of the old property
|
2022-12-10 11:57:52 +00:00
|
|
|
|
$param->attrGroups = \array_merge($param->attrGroups, $property->attrGroups);
|
2023-06-06 10:31:04 +00:00
|
|
|
|
$this->processUnionType($property, $param);
|
2023-09-11 13:16:45 +00:00
|
|
|
|
$this->propertyPromotionDocBlockMerger->mergePropertyAndParamDocBlocks($property, $param, $paramTagValueNode);
|
2023-12-24 00:51:27 +00:00
|
|
|
|
// update variable to property fetch references
|
|
|
|
|
$this->traverseNodesWithCallable((array) $constructClassMethod->stmts, function (Node $node) use($promotionCandidate, $propertyName) : ?PropertyFetch {
|
|
|
|
|
if (!$node instanceof Variable) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (!$this->isName($node, $promotionCandidate->getParamName())) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return new PropertyFetch(new Variable('this'), $propertyName);
|
|
|
|
|
});
|
2020-08-05 20:45:36 +00:00
|
|
|
|
}
|
2020-12-09 23:40:38 +00:00
|
|
|
|
return $node;
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|
2021-07-21 09:35:57 +00:00
|
|
|
|
public function provideMinPhpVersion() : int
|
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
|
return PhpVersionFeature::PROPERTY_PROMOTION;
|
2021-07-21 09:35:57 +00:00
|
|
|
|
}
|
2023-06-06 10:31:04 +00:00
|
|
|
|
private function processUnionType(Property $property, Param $param) : void
|
2021-02-22 09:59:51 +00:00
|
|
|
|
{
|
2023-06-06 10:31:04 +00:00
|
|
|
|
if ($property->type instanceof Node) {
|
|
|
|
|
$param->type = $property->type;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!$param->default instanceof Expr) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!$param->type instanceof Node) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$defaultType = $this->getType($param->default);
|
|
|
|
|
$paramType = $this->getType($param->type);
|
|
|
|
|
if ($this->typeComparator->isSubtype($defaultType, $paramType)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if ($this->typeComparator->areTypesEqual($defaultType, $paramType)) {
|
|
|
|
|
return;
|
2021-02-22 09:59:51 +00:00
|
|
|
|
}
|
2023-06-06 10:31:04 +00:00
|
|
|
|
if ($paramType instanceof MixedType) {
|
|
|
|
|
return;
|
2023-05-14 15:10:59 +00:00
|
|
|
|
}
|
2023-06-06 10:31:04 +00:00
|
|
|
|
$paramType = TypeCombinator::union($paramType, $defaultType);
|
|
|
|
|
$param->type = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($paramType, TypeKind::PARAM);
|
2021-02-22 09:59:51 +00:00
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
private function shouldSkipParam(Param $param) : bool
|
2021-06-30 13:08:35 +00:00
|
|
|
|
{
|
|
|
|
|
if ($param->variadic) {
|
|
|
|
|
return \true;
|
|
|
|
|
}
|
2021-07-14 07:23:54 +00:00
|
|
|
|
if ($this->paramAnalyzer->isNullable($param)) {
|
|
|
|
|
/** @var NullableType $type */
|
|
|
|
|
$type = $param->type;
|
|
|
|
|
$type = $type->type;
|
|
|
|
|
} else {
|
|
|
|
|
$type = $param->type;
|
|
|
|
|
}
|
2022-10-24 08:19:06 +00:00
|
|
|
|
if ($this->isCallableTypeIdentifier($type)) {
|
|
|
|
|
return \true;
|
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
|
if (!$type instanceof UnionType) {
|
2021-10-29 14:22:26 +00:00
|
|
|
|
return \false;
|
|
|
|
|
}
|
|
|
|
|
foreach ($type->types as $type) {
|
2022-10-24 08:19:06 +00:00
|
|
|
|
if ($this->isCallableTypeIdentifier($type)) {
|
|
|
|
|
return \true;
|
2021-10-29 14:22:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return \false;
|
2021-06-30 13:08:35 +00:00
|
|
|
|
}
|
2022-10-24 08:19:06 +00:00
|
|
|
|
private function isCallableTypeIdentifier(?Node $node) : bool
|
|
|
|
|
{
|
|
|
|
|
return $node instanceof Identifier && $this->isName($node, 'callable');
|
|
|
|
|
}
|
2020-06-08 09:28:03 +00:00
|
|
|
|
}
|