Skip to content

Commit

Permalink
[Php73] Handle crash on JsonThrowOnErrorRector + SensitiveConstantNam…
Browse files Browse the repository at this point in the history
…eRector (#5253)

* [Php73] Handle crash on JsonThrowOnErrorRector + SensitiveConstantNameRector

* Fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Nov 16, 2023
1 parent 52ef7b8 commit 60047ce
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
9 changes: 4 additions & 5 deletions rules/Php73/Rector/ConstFetch/SensitiveConstantNameRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -21,7 +20,7 @@
*
* @see \Rector\Tests\Php73\Rector\ConstFetch\SensitiveConstantNameRector\SensitiveConstantNameRectorTest
*/
final class SensitiveConstantNameRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
final class SensitiveConstantNameRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @see http://php.net/manual/en/reserved.constants.php
Expand Down Expand Up @@ -129,7 +128,7 @@ public function getNodeTypes(): array
/**
* @param ConstFetch $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
public function refactor(Node $node): ?Node
{
$constantName = $this->getName($node);
if ($constantName === null) {
Expand All @@ -144,7 +143,7 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
}

// constant is defined in current lower/upper case
if ($this->reflectionProvider->hasConstant(new Name($constantName), $scope)) {
if ($this->reflectionProvider->hasConstant(new Name($constantName), null)) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Rector\Core\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

class Fixture
{
public static function run()
{
$user = $_REQUEST['param'];

$user = json_decode($user);

$condition = true;
if ($condition) {
$output = [];
echo(json_encode($output));
exit;
}

$output = [];
echo(json_encode($output));
}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\ScopeNotAvailable\FixtureJsonThrowCaseSensitiveConstFetch;

class Fixture
{
public static function run()
{
$user = $_REQUEST['param'];

$user = json_decode($user, null, 512, JSON_THROW_ON_ERROR);

$condition = true;
if ($condition) {
$output = [];
echo(json_encode($output));
exit;
}

$output = [];
echo(json_encode($output, JSON_THROW_ON_ERROR));
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\ScopeNotAvailable;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class JsonThrowCaseSensitiveConstFetchTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/FixtureJsonThrowCaseSensitiveConstFetch');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/json_throw_case_sensitive_const_fetch.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Php73\Rector\FuncCall\JsonThrowOnErrorRector;
use Rector\Php73\Rector\ConstFetch\SensitiveConstantNameRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
JsonThrowOnErrorRector::class,
SensitiveConstantNameRector::class
]);
};

0 comments on commit 60047ce

Please sign in to comment.