-
Notifications
You must be signed in to change notification settings - Fork 38
[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
TomasVotruba
wants to merge
9
commits into
phpstan:1.1.x
Choose a base branch
from
TomasVotruba:tv-nette-json-return-type
base: 1.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3f248d8
[composer] autoload tests with PSR-4
TomasVotruba 6535f0e
add JsonDecodeDynamicReturnTypeExtension
TomasVotruba ca0ce35
[ci] add downgrade of nette/utils to use json_decode() for old PHP
TomasVotruba 1041390
make use of ConstantTypeHelper
TomasVotruba 024bd78
cleanup
TomasVotruba 43b9215
add array types
TomasVotruba 62a2ec9
split unknown types test
TomasVotruba 85221fa
rename JsonDecode... to NetteUtilsJsonDecode..
TomasVotruba 172e2fb
propose JsonDecodeDynamicReturnTypeExtension
TomasVotruba File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
42 changes: 42 additions & 0 deletions
42
tests/Type/Php/JsonDecodeDynamicReturnTypeExtensionTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']; | ||
} | ||
|
||
|
||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
services: | ||
- | ||
class: PHPStan\Type\Php\JsonDecodeDynamicReturnTypeExtension | ||
tags: | ||
- phpstan.broker.dynamicFunctionReturnTypeExtension |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :/There was a problem hiding this comment.
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:
↓
There was a problem hiding this comment.
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 onJSON_THROW_ON_ERROR
. You need to modify it first.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll try it