Skip to content

Commit 2d20783

Browse files
[DowngradePhp81] Fix algo not on list on DowngradeHashAlgorithmXxHashRector (#200)
* [DowngradePhp81] Fix algo not on list on DowngradeHashAlgorithmXxHashRector * skip first class callable * fix exists * ad test * [ci-review] Rector Rectify * Fix phpstan --------- Co-authored-by: GitHub Action <[email protected]>
1 parent 9fb5952 commit 2d20783

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeHashAlgorithmXxHash\Fixture;
4+
5+
class SkipHashNotOnList
6+
{
7+
public function run($arg)
8+
{
9+
return hash($arg, 'some-data-to-hash');
10+
}
11+
}

rules/DowngradePhp81/Rector/FuncCall/DowngradeHashAlgorithmXxHashRector.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PhpParser\Node\Expr\ConstFetch;
1010
use PhpParser\Node\Expr\FuncCall;
1111
use PhpParser\Node\Scalar\String_;
12-
use Rector\Core\Exception\ShouldNotHappenException;
1312
use Rector\Core\NodeAnalyzer\ArgsAnalyzer;
1413
use Rector\Core\PhpParser\Node\Value\ValueResolver;
1514
use Rector\Core\Rector\AbstractRector;
@@ -88,31 +87,34 @@ public function refactor(Node $node): ?Node
8887
$this->argNamedKey = 0;
8988

9089
$algorithm = $this->getHashAlgorithm($node->getArgs());
91-
92-
if (! array_key_exists($algorithm, self::HASH_ALGORITHMS_TO_DOWNGRADE)) {
90+
if ($algorithm === null || ! array_key_exists($algorithm, self::HASH_ALGORITHMS_TO_DOWNGRADE)) {
9391
return null;
9492
}
9593

96-
$arg = $node->args[$this->argNamedKey];
97-
98-
if (! $arg instanceof Arg) {
99-
throw new ShouldNotHappenException();
94+
$args = $node->getArgs();
95+
if (! isset($args[$this->argNamedKey])) {
96+
return null;
10097
}
10198

99+
$arg = $args[$this->argNamedKey];
102100
$arg->value = new String_(self::REPLACEMENT_ALGORITHM);
103101

104102
return $node;
105103
}
106104

107105
private function shouldSkip(FuncCall $funcCall): bool
108106
{
107+
if ($funcCall->isFirstClassCallable()) {
108+
return true;
109+
}
110+
109111
return ! $this->nodeNameResolver->isName($funcCall, 'hash');
110112
}
111113

112114
/**
113115
* @param Arg[] $args
114116
*/
115-
private function getHashAlgorithm(array $args): string
117+
private function getHashAlgorithm(array $args): ?string
116118
{
117119
$arg = null;
118120

@@ -137,7 +139,7 @@ private function getHashAlgorithm(array $args): string
137139
$algorithmNode instanceof ConstFetch => $this->mapConstantToString(
138140
$this->valueResolver->getValue($algorithmNode)
139141
),
140-
default => throw new ShouldNotHappenException(),
142+
default => null,
141143
};
142144
}
143145

0 commit comments

Comments
 (0)