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

[Php73] Skip JsonThrowOnErrorRector on passed exact value #1051

Merged
merged 6 commits into from
Oct 24, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function match(Array_ $array): null | ArrayCallableDynamicMethod | ArrayC
$className = $calleeType->getClassName();
$secondItemValue = $items[1]->value;

if ($values === []) {
if ($values === null) {
return new ArrayCallableDynamicMethod($firstItemValue, $className, $secondItemValue);
}

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

namespace Rector\Tests\Php73\Rector\FuncCall\JsonThrowOnErrorRector\Fixture;

function processContentFromParam(array $content = [])
{
json_encode($content);
}

function processContentFromParam2(string $json)
{
json_decode($json);
}

?>
-----
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\JsonThrowOnErrorRector\Fixture;

function processContentFromParam(array $content = [])
{
json_encode($content, JSON_THROW_ON_ERROR);
}

function processContentFromParam2(string $json)
{
json_decode($json, null, 512, JSON_THROW_ON_ERROR);
}

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

namespace Rector\Tests\Php73\Rector\FuncCall\JsonThrowOnErrorRector\Fixture;

function processMixedContent(array $message, int $code, string $detail)
{
$jsonData = [
'validation_messages' => $message,
'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html',
'title' => 'Unprocessable Entity',
'status' => $code,
'detail' => $detail,
];

$response = json_encode($jsonData);
assert(is_string($response));
}

function processMixedContent2($data){
$json = "{$data}";

$response = json_decode($json);
assert(is_array($response));
}

?>
-----
<?php

namespace Rector\Tests\Php73\Rector\FuncCall\JsonThrowOnErrorRector\Fixture;

function processMixedContent(array $message, int $code, string $detail)
{
$jsonData = [
'validation_messages' => $message,
'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html',
'title' => 'Unprocessable Entity',
'status' => $code,
'detail' => $detail,
];

$response = json_encode($jsonData, JSON_THROW_ON_ERROR);
assert(is_string($response));
}

function processMixedContent2($data){
$json = "{$data}";

$response = json_decode($json, null, 512, JSON_THROW_ON_ERROR);
assert(is_array($response));
}

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

namespace Rector\Tests\Php73\Rector\FuncCall\JsonThrowOnErrorRector\Fixture;

function skipExactValue(){
$jsonData = [
'validation_messages' =>
[
'foo' => [
'regexNotMatch' => 'The input does not match against pattern \'/^[a-zA-Z0-9 .\-]+$/\''
]
],
'type' => 'http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html',
'title' => 'Unprocessable Entity',
'status' => 422,
'detail' => 'Failed Validation'
];

$response = json_encode($jsonData);
assert(is_string($response));
}

function skipExactValue2(){
$json = '{}';

$response = json_decode($json);
assert(is_array($response));
}
9 changes: 9 additions & 0 deletions rules/Php73/Rector/FuncCall/JsonThrowOnErrorRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ private function shouldSkip(FuncCall $funcCall): bool
}
}

$value = $this->valueResolver->getValue($args[0]->value);
if (is_string($value)) {
return true;
}

if (is_array($value)) {
return true;
}

return (bool) $this->betterNodeFinder->findFirstNext($funcCall, function (Node $node): bool {
if (! $node instanceof FuncCall) {
return false;
Expand Down
8 changes: 5 additions & 3 deletions src/PhpParser/Node/Value/ValueResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ConstFetchAnalyzer;
use Rector\Core\Provider\CurrentFileProvider;
Expand Down Expand Up @@ -199,7 +200,7 @@ private function getConstExprEvaluator(): ConstExprEvaluator
/**
* @return mixed[]
*/
private function extractConstantArrayTypeValue(ConstantArrayType $constantArrayType): array
private function extractConstantArrayTypeValue(ConstantArrayType $constantArrayType): ?array
{
$keys = [];
foreach ($constantArrayType->getKeyTypes() as $i => $keyType) {
Expand All @@ -213,9 +214,10 @@ private function extractConstantArrayTypeValue(ConstantArrayType $constantArrayT
$value = $this->extractConstantArrayTypeValue($valueType);
} elseif ($valueType instanceof ConstantScalarType) {
$value = $valueType->getValue();
} else {
// not sure about value
} elseif ($valueType instanceof TypeWithClassName) {
continue;
} else {
return null;
}

$values[$keys[$i]] = $value;
Expand Down