GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — [Types] Add JsonDecodeDynamicReturnTypeExtension by TomasVotruba · Pull Request #89 · phpstan/phpstan-nette Conversation
|
| # in PHP 7.1, the json_decode() 2nd parameter requires bool, while PHP 7.2 it's null|int | ||
| - name: "Downgrade nette/utils" | ||
| if: matrix.php-version == '7.1' | ||
| run: "composer require --dev nette/utils:^2.3 nette/forms:^2.4 --update-with-dependencies" |
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
2nd arg of json_decode() takes different types in PHP 7.1 and PHP 7.2+
Sorry, something went wrong.
There was an error while loading. Please reload this page.
7743920 to
f796944
Compare
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
What I'd like to see instead is to have this kind of logic for json_decode in phpstan-src. When that's done, this Nette extension can simply delegate the logic with $scope->getType(new FuncCall('json_decode', $args)). Of course $args would be composed based on the FORCE_ARRAY argument so that json_decode is called with $associative=true.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I actually tried to add function extension first but PHPStan analysed only non-basic function. Not sure why. Apart that, is the type resolution in tests here correct? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Instead of the whole |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
|
||
| // scalar types with stdClass | ||
| return new UnionType([ | ||
| new ObjectType(stdClass::class), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It can still be an array here.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
On what input?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I guess the stdclass could contain a array of other things?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You don't have to guess: https://3v4l.org/qsjVh
BTW I worry that these precise types would become really annoying. Right now people working with json_decode() get some errors on level 9, but with these union types it would be much sonner - on level 7. I think the best course of action is to return "MixedType without stdClass" with FORCE_ARRAY, and normal "MixedType" without FORCE_ARRAY.
MixedType is subtractable - new MixedType(true, new ObjectType(\stdClass::class)) means mixed without stdClass.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
MixedType is subtractable
Cool, that's a new trick to me.
I think the best course of action is to return "MixedType without stdClass" with FORCE_ARRAY, and normal "MixedType" without FORCE_ARRAY.
Do you mean like this?
if ($isForceArray) { return new MixedType(true, new ObjectType(\stdClass::class)); } return new MixedType(true);
It might be change with more strict approach, but it would be more correct. The reason we made this extension for our project is to make sure there is only scalar value or stdClass. Mixed includes anything, objects, array of object etc.
Yet the DX is important, I understand. It might be useful to enable this only since level 9+.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Just to reiterate - I still want this to be submitted as a |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@ondrejmirtes I understand that, I just want to make it work and confirm here, so we don't jump back and forth. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
f796944 to
1041390
Compare
There was an error while loading. Please reload this page.
|
@ondrejmirtes As for |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
No (https://phpstan.org/developing-extensions/dynamic-return-type-extensions):
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@ondrejmirtes Thanks 👍 I'm trying to add JsonDecodeDynamicReturnTypeExtension, but the only function that is found is What I'm doing wrong there? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
| { | ||
| // @todo - finds only "assertType", but not "json_decode" :/ | ||
| dump($functionReflection->getName()); | ||
|
|
||
| return $functionReflection->getName() === 'json_decode'; | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Here the json_decode is never passed :/
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem 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.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'll try it
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Requested changes must be addressed to merge this pull request.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.