Skip to content

Commit

Permalink
[Privatization] Fix crash on assign inside Arg on ChangeReadOnlyVaria…
Browse files Browse the repository at this point in the history
…bleWithDefaultValueToConstantRector (#3423)

* [Privatization] Skip crash on assign inside Arg on ChangeReadOnlyVariableWithDefaultValueToConstantRector

* Fixed 🎉

* use Expression to cover other use case
  • Loading branch information
samsonasik committed Feb 28, 2023
1 parent a85934b commit fe3c6ff
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\Privatization\Rector\Class_\ChangeReadOnlyVariableWithDefaultValueToConstantRector\Fixture;

final class SkipAssignInsideArg
{
public function run()
{
$c = base64_decode(str_replace(['--', '_'], ['+', '/'], $key));

$ivlen = openssl_cipher_iv_length($cipher = "AES-128-CFB");
$hmac = substr($c, $ivlen, $sha2len = 32);
$ciphertext_raw = substr($c, $ivlen + $sha2len);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\BetterPhpDocParser\PhpDocManipulator\VarAnnotationManipulator;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeManipulator\ClassMethodAssignManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Rector\Privatization\Naming\ConstantNaming;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -203,6 +205,11 @@ private function refactorClassMethod(ClassMethod $classMethod, Class_ $class, ar
}
}

$parentAssign = $readOnlyVariableAssign->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentAssign instanceof Expression) {
continue;
}

$this->removeNode($readOnlyVariableAssign);
$classConst = $this->createPrivateClassConst($variable, $readOnlyVariableAssign->expr);

Expand Down

0 comments on commit fe3c6ff

Please sign in to comment.