Skip to content

Commit

Permalink
cleanup (#4787)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 14, 2023
1 parent 29d9b76 commit 90b832d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,7 @@ public function processNodes(
}

if ($node instanceof Trait_) {
$traitName = $this->resolveClassName($node);

$traitClassReflection = $this->reflectionProvider->getClass($traitName);

$traitScope = clone $mutatingScope;

/** @var ScopeContext $scopeContext */
$scopeContext = $this->privatesAccessor->getPrivateProperty($traitScope, self::CONTEXT);

$traitContext = clone $scopeContext;

// before entering the class/trait again, we have to tell scope no class was set, otherwise it crashes
$this->privatesAccessor->setPrivateProperty(
$traitContext,
'classReflection',
$traitClassReflection,
);

$this->privatesAccessor->setPrivateProperty($traitScope, self::CONTEXT, $traitContext);

$node->setAttribute(AttributeKey::SCOPE, $traitScope);
$this->nodeScopeResolver->processNodes($node->stmts, $traitScope, $nodeCallback);
$this->decorateTraitAttrGroups($node, $traitScope);

$this->processTrait($node, $mutatingScope, $nodeCallback);
return;
}

Expand Down Expand Up @@ -410,4 +387,29 @@ private function resolveClassName(Class_ | Interface_ | Trait_| Enum_ $classLike

return $classLike->name->toString();
}

/**
* @param callable(Node $node, MutatingScope $scope): void $nodeCallback
*/
private function processTrait(Trait_ $node, MutatingScope $mutatingScope, callable $nodeCallback): void
{
$traitName = $this->resolveClassName($node);

$traitClassReflection = $this->reflectionProvider->getClass($traitName);

$traitScope = clone $mutatingScope;

/** @var ScopeContext $scopeContext */
$scopeContext = $this->privatesAccessor->getPrivateProperty($traitScope, self::CONTEXT);
$traitContext = clone $scopeContext;

// before entering the class/trait again, we have to tell scope no class was set, otherwise it crashes
$this->privatesAccessor->setPrivateProperty($traitContext, 'classReflection', $traitClassReflection);

$this->privatesAccessor->setPrivateProperty($traitScope, self::CONTEXT, $traitContext);

$node->setAttribute(AttributeKey::SCOPE, $traitScope);
$this->nodeScopeResolver->processNodes($node->stmts, $traitScope, $nodeCallback);
$this->decorateTraitAttrGroups($node, $traitScope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@
*/
final class ClosureTypeMapper implements TypeMapperInterface
{
public function __construct(private readonly PhpVersionProvider $phpVersionProvider)
{
public function __construct(
private readonly PhpVersionProvider $phpVersionProvider
) {
}

/**
Expand Down Expand Up @@ -69,12 +70,16 @@ static function (AstNode $astNode): ?FullyQualifiedIdentifierTypeNode {
public function mapToPhpParserNode(Type $type, string $typeKind): ?Node
{
// ref https://3v4l.org/iKMK6#v5.3.29
if ($typeKind === TypeKind::PARAM && $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ANONYMOUS_FUNCTION_PARAM_TYPE)) {
if ($typeKind === TypeKind::PARAM && $this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::ANONYMOUS_FUNCTION_PARAM_TYPE
)) {
return new FullyQualified('Closure');
}

// ref https://3v4l.org/g8WvW#v7.4.0
if ($typeKind === TypeKind::PROPERTY && $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::TYPED_PROPERTIES)) {
if ($typeKind === TypeKind::PROPERTY && $this->phpVersionProvider->isAtLeastPhpVersion(
PhpVersionFeature::TYPED_PROPERTIES
)) {
return new FullyQualified('Closure');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
*/
final class NullTypeMapper implements TypeMapperInterface
{
public function __construct(private readonly PhpVersionProvider $phpVersionProvider)
{
public function __construct(
private readonly PhpVersionProvider $phpVersionProvider
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,21 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
/** @var Ternary $ternaryExpression */
$ternaryExpression = $node;
if (! $ternaryExpression->if instanceof Expr) {
if (! $node->if instanceof Expr) {
return null;
}

$ifExpression = $ternaryExpression->if;
$ifExpression = $node->if;
if (! $this->valueResolver->isTrueOrFalse($ifExpression)) {
return null;
}

$elseExpression = $ternaryExpression->else;
$elseExpression = $node->else;
if (! $this->valueResolver->isTrueOrFalse($elseExpression)) {
return null;
}

$condition = $ternaryExpression->cond;
$condition = $node->cond;
if (! $condition instanceof BinaryOp) {
return $this->processNonBinaryCondition($ifExpression, $elseExpression, $condition);
}
Expand Down

0 comments on commit 90b832d

Please sign in to comment.