Skip to content

Commit

Permalink
Fix just assigned variable in InlineArrayReturnAssignRector (#4363)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 27, 2023
1 parent a653146 commit 14d5c18
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
*/
final class PropertyTypeResolver implements NodeTypeResolverInterface
{
public function __construct(private readonly PropertyFetchTypeResolver $propertyFetchTypeResolver)
{
public function __construct(
private readonly PropertyFetchTypeResolver $propertyFetchTypeResolver
) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public function enterNode(Node $node): ?Node

$funcCallName = $node->name->toString();
foreach ($node->args as $arg) {
if (!$arg instanceof Arg) {
if (! $arg instanceof Arg) {
continue;
}

if (!$arg->value instanceof Array_) {
if (! $arg->value instanceof Array_) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public function enterNode(Node $node): ?Node
return null;
}

if (!$node instanceof StaticCall) {
if (! $node instanceof StaticCall) {
return null;
}

if (!$node->class instanceof Name) {
if (! $node->class instanceof Name) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function addFilesByDirectory(string $directory, array $files): void
$this->filesByDirectory[$directory] = $files;
}

public function isPathsEmpty(): bool {
public function isPathsEmpty(): bool
{
return $this->filePaths === [] && $this->filesByDirectory === [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ private function createFromExplicitProperties(
$annotationPropertyToAttributeClass
);

if ($annotationPropertyToAttributeClass->doesNeedNewImport() && count($attributeName->getParts()) === 1) {
if ($annotationPropertyToAttributeClass->doesNeedNewImport() && count(
$attributeName->getParts()
) === 1) {
$attributeName->setAttribute(
AttributeKey::EXTRA_USE_IMPORT,
$annotationPropertyToAttributeClass->getAttributeClass()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;

final class SkipAssignInTheMiddle
{
public function run()
{
$items = [];

$result = 'Timmy';
$items[] = $result . ' Bobby';

return $items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Variable;
Expand Down Expand Up @@ -83,6 +84,10 @@ public function refactor(Node $node): ?Node
return null;
}

if (! $this->areAssignExclusiveToDimFetch($stmts)) {
return null;
}

$lastStmt = array_pop($stmts);
if (! $lastStmt instanceof Stmt) {
return null;
Expand Down Expand Up @@ -154,4 +159,37 @@ private function createArray(array $keysAndExprs): Array_

return new Array_($arrayItems);
}

/**
* Only:
* $items['...'] = $result;
*
* @param Stmt[] $stmts
*/
private function areAssignExclusiveToDimFetch(array $stmts): bool
{
$lastKey = array_key_last($stmts);

foreach ($stmts as $key => $stmt) {
if ($key === $lastKey) {
// skip last item
continue;
}

if (! $stmt instanceof Expression) {
return false;
}

if (! $stmt->expr instanceof Assign) {
return false;
}

$assign = $stmt->expr;
if (! $assign->var instanceof ArrayDimFetch) {
return false;
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace Rector\Php82\Rector\Param;

use PhpParser\Node\Param;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Name\FullyQualified;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use PhpParser\Node;
use PhpParser\Node\Param;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

Expand All @@ -30,8 +30,9 @@ final class AddSensitiveParameterAttributeRector extends AbstractRector implemen
*/
private array $sensitiveParameters = [];

public function __construct(protected PhpAttributeAnalyzer $phpAttributeAnalyzer)
{
public function __construct(
protected PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}

/**
Expand Down Expand Up @@ -61,9 +62,7 @@ public function refactor(Node $node): ?Param
return null;
}

$node->attrGroups[] = new AttributeGroup([
new Attribute(new FullyQualified('SensitiveParameter')),
]);
$node->attrGroups[] = new AttributeGroup([new Attribute(new FullyQualified('SensitiveParameter'))]);

return $node;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
// 1. add files and directories to static locator
$this->dynamicSourceLocatorDecorator->addPaths($paths);
if ($this->dynamicSourceLocatorDecorator->isPathsEmpty()) {
$this->rectorOutputStyle->error("The given paths do not match any files");
$this->rectorOutputStyle->error('The given paths do not match any files');
return ExitCode::FAILURE;
}

Expand Down
3 changes: 2 additions & 1 deletion src/StaticReflection/DynamicSourceLocatorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public function addPaths(array $paths): void
}
}

public function isPathsEmpty(): bool {
public function isPathsEmpty(): bool
{
return $this->dynamicSourceLocatorProvider->isPathsEmpty();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

11 changes: 0 additions & 11 deletions tests/Issues/DisableAutoImportDocBlock/config/configure_rule.php

This file was deleted.

28 changes: 0 additions & 28 deletions tests/Issues/Issue6496/AutoImportDocBlockTest.php

This file was deleted.

34 changes: 0 additions & 34 deletions tests/Issues/Issue6496/Fixture/double_import_same_suffix.php.inc

This file was deleted.

27 changes: 0 additions & 27 deletions tests/Issues/Issue6496/Fixture/import_doc_after_rename.php.inc

This file was deleted.

0 comments on commit 14d5c18

Please sign in to comment.