Skip to content

[Types] Add JsonDecodeDynamicReturnTypeExtension #89

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

Open
wants to merge 9 commits into
base: 1.1.x
Choose a base branch
from
86 changes: 86 additions & 0 deletions src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use Nette\Utils\Json;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ConstantTypeHelper;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\FloatType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use stdClass;

final class JsonDecodeDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
// @todo - finds only "assertType", but not "json_decode" :/
dump($functionReflection->getName());

return $functionReflection->getName() === 'json_decode';
}
Comment on lines +25 to +31
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the json_decode is never passed :/

Copy link
Contributor Author

@TomasVotruba TomasVotruba Jan 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reproduce localy, run PHPUnit:

vendor/bin/phpunit tests/Type/Php/JsonDecodeDynamicReturnTypeExtensionTest.php

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because there's already JsonThrowOnErrorDynamicReturnTypeExtension in phpstan-src whcih changes the result based on JSON_THROW_ON_ERROR. You need to modify it first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try it


public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): Type
{
$args = $funcCall->getArgs();

dump('___');
die;

$isForceArray = $this->isForceArray($funcCall);

$firstArgValue = $args[0]->value;
$firstValueType = $scope->getType($firstArgValue);

if ($firstValueType instanceof ConstantStringType) {
$resolvedType = $this->resolveConstantStringType($firstValueType, $isForceArray);
} else {
$resolvedType = new MixedType();
}

if (! $resolvedType instanceof MixedType) {
return $resolvedType;
}

// fallback type
if ($isForceArray) {
return new UnionType([
new ArrayType(new MixedType(), new MixedType()),
new StringType(),
new FloatType(),
new IntegerType(),
new BooleanType(),
]);
}

// scalar types with stdClass
return new UnionType([
new ObjectType(stdClass::class),
new StringType(),
new FloatType(),
new IntegerType(),
new BooleanType(),
]);
}

private function resolveConstantStringType(ConstantStringType $constantStringType, bool $isForceArray): Type
{
if ($isForceArray) {
$decodedValue = Json::decode($constantStringType->getValue(), Json::FORCE_ARRAY);
} else {
$decodedValue = Json::decode($constantStringType->getValue());
}

return ConstantTypeHelper::getTypeFromValue($decodedValue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testAsserts(string $assertType, string $file, ...$args): void
*/
public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/config/json_decode_extension.neon'];
return [__DIR__ . '/config/nette_json_decode_extension.neon'];
}

}
42 changes: 42 additions & 0 deletions tests/Type/Php/JsonDecodeDynamicReturnTypeExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace PHPStan\Type\Php;

use PHPStan\Testing\TypeInferenceTestCase;

final class JsonDecodeDynamicReturnTypeExtensionTest extends TypeInferenceTestCase
{

/**
* @return iterable<mixed>
*/
public function dataAsserts(): iterable
{
yield from $this->gatherAssertTypes(__DIR__ . '/data/json_decode.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/json_decode_force_array.php');
}

/**
* @dataProvider dataAsserts()
* @param string $assertType
* @param string $file
* @param mixed ...$args
*/
public function testAsserts(string $assertType, string $file, ...$args): void
{
$this->assertFileAsserts($assertType, $file, ...$args);
}

/**
* @return string[]
*/
public static function getAdditionalConfigFiles(): array
{
return [__DIR__ . '/config/json_decode_extension.neon'];
}



}
5 changes: 5 additions & 0 deletions tests/Type/Php/config/json_decode_extension.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
-
class: PHPStan\Type\Php\JsonDecodeDynamicReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension
21 changes: 21 additions & 0 deletions tests/Type/Php/data/json_decode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use function PHPStan\Testing\assertType;

$value = json_decode('true');
assertType('true', $value);

$value = json_decode('1');
assertType('1', $value);

$value = json_decode('1.5');
assertType('1.5', $value);

$value = json_decode('false');
assertType('false', $value);

$value = json_decode('{}');
assertType('stdClass', $value);

$value = json_decode('[1, 2, 3]');
assertType('array{1, 2, 3}', $value);
21 changes: 21 additions & 0 deletions tests/Type/Php/data/json_decode_force_array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use function PHPStan\Testing\assertType;

$value = json_decode('true', true);
assertType('true', $value);

$value = json_decode('1', true);
assertType('1', $value);

$value = json_decode('1.5', true);
assertType('1.5', $value);

$value = json_decode('false', true);
assertType('false', $value);

$value = json_decode('{}', true);
assertType('array{}', $value);

$value = json_decode('[1, 2, 3]', true);
assertType('array{1, 2, 3}', $value);