Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodeQuality] Handle crash attribute used on trait on CallableThisArrayToAnonymousFunctionRector #2675

Merged
merged 7 commits into from Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -110,7 +110,7 @@ public function processNodes(
$isScopeRefreshing,
$smartFileInfo
): void {
if ($node instanceof Expression) {
if (($node instanceof Expression || $node instanceof Return_) && $node->expr instanceof Expr) {
$node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

Expand Down Expand Up @@ -140,30 +140,11 @@ public function processNodes(
}

if ($node instanceof Switch_) {
// decorate value as well
foreach ($node->cases as $case) {
$case->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
$this->processSwitch($node, $mutatingScope);
}

if ($node instanceof TryCatch) {
foreach ($node->catches as $catch) {
$varName = $catch->var instanceof Variable
? $this->nodeNameResolver->getName($catch->var)
: null;
$type = TypeCombinator::union(
...array_map(
static fn (Name $class): ObjectType => new ObjectType((string) $class),
$catch->types
)
);
$catchMutatingScope = $mutatingScope->enterCatchType($type, $varName);
$this->processNodes($catch->stmts, $smartFileInfo, $catchMutatingScope);
}

if ($node->finally instanceof Finally_) {
$node->finally->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
$this->processTryCatch($node, $smartFileInfo, $mutatingScope);
}

if ($node instanceof Assign) {
Expand All @@ -172,11 +153,6 @@ public function processNodes(
$node->var->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

// decorate value as well
if ($node instanceof Return_ && $node->expr instanceof Expr) {
$node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}

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

Expand Down Expand Up @@ -207,8 +183,9 @@ public function processNodes(
);

$node->setAttribute(AttributeKey::SCOPE, $traitScope);

$this->nodeScopeResolver->processNodes($node->stmts, $traitScope, $nodeCallback);
$this->decorateTraitAttrGroups($node, $traitScope);

return;
}

Expand All @@ -232,6 +209,48 @@ public function processNodes(
return $this->processNodesWithDependentFiles($smartFileInfo, $stmts, $scope, $nodeCallback);
}

private function decorateTraitAttrGroups(Trait_ $trait, MutatingScope $mutatingScope): void
{
foreach ($trait->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
foreach ($attr->args as $arg) {
$arg->value->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}
}
}

private function processSwitch(Switch_ $switch, MutatingScope $mutatingScope): void
{
// decorate value as well
foreach ($switch->cases as $case) {
$case->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}

private function processTryCatch(
TryCatch $tryCatch,
SmartFileInfo $smartFileInfo,
MutatingScope $mutatingScope
): void {
foreach ($tryCatch->catches as $catch) {
$varName = $catch->var instanceof Variable
? $this->nodeNameResolver->getName($catch->var)
: null;

$type = TypeCombinator::union(
...array_map(static fn (Name $class): ObjectType => new ObjectType((string) $class), $catch->types)
);

$catchMutatingScope = $mutatingScope->enterCatchType($type, $varName);
$this->processNodes($catch->stmts, $smartFileInfo, $catchMutatingScope);
}

if ($tryCatch->finally instanceof Finally_) {
$tryCatch->finally->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}

private function processUnreachableStatementNode(
UnreachableStatementNode $unreachableStatementNode,
SmartFileInfo $smartFileInfo,
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Expand Up @@ -672,3 +672,11 @@ parameters:
-
message: '#Call to static method Webmozart\\Assert\\Assert\:\:allString\(\) with array<string> will always evaluate to true#'
path: rules/Transform/ValueObject/ParentClassToTraits.php

-
message: '#Relative file path ".+" is not allowed, use absolute one with __DIR__#'
paths:
- bin/rector.php
- src/Bootstrap/RectorConfigsResolver.php
- src/ValueObject/StaticNonPhpFileSuffixes.php
- tests/FileSystem/FilesFinder/FilesFinderTest.php
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture;

use Doctrine\ORM\Mapping as ORM;

#[ORM\UniqueConstraint(columns: ['public_id', 'tenant_id'])]
trait SkipAttributeOnTrait
{
}