GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Specifiy `json_decode` default return type by herndlm · Pull Request #1283 · phpstan/phpstan-src

Conversation

@herndlm

@herndlm herndlm commented May 4, 2022

Copy link
Copy Markdown
Contributor

mixed is too vague IMO, this should be fine to specify with more details, see also https://3v4l.org/hEDja
But please somebody double and triple check that I didn't miss anything here. Together with #993 this allows us to get rid of stdClass for example.

I was also hoping that it would "fix" phpstan/phpstan#7073 but apparently NonexistentOffsetInArrayDimFetchRule is not happy with scalar null coalescing access. But not sure if that's coming from that rule or somewhere else CC @rajyan

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@herndlm
herndlm changed the base branch from 1.7.x to 1.6.x May 4, 2022 07:36
@herndlm
Specifiy json_decode default return type
d43c845
@herndlm
herndlm force-pushed the specify-json-decode-default-return-type branch from b31db0e to d43c845 Compare May 4, 2022 07:36
@ondrejmirtes

ondrejmirtes commented May 4, 2022

Copy link
Copy Markdown
Member

I've rejected this idea in phpstan/phpstan-nette#89 (comment) and that's why I suggested to subtract from MixedType instead.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@herndlm

herndlm commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

I've rejected this idea in phpstan/phpstan-nette#89 (comment) and that's why I suggested to subtract from MixedType instead.

ah I see, makes sense. ok then

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@herndlm

herndlm commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

I always forget that adding more type information / getting rid of mixed will potentially lead to more errors below level 9. I instinctively want to get rid of mixed everywhere :D

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@herndlm herndlm closed this May 4, 2022
@herndlm
herndlm deleted the specify-json-decode-default-return-type branch May 4, 2022 07:41
@rajyan

rajyan commented May 4, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

@herndlm
https://phpstan.org/r/de94db92-8cac-49f9-bac4-4eb8b7e76ec8
As you mentioned, this case (not related to json_decode) can be solved, but we have to be careful that the union type doesn't contain object related type, because array access on object throws a fatal error.
We can change here

phpstan-src/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php

Line 59 in 356250a

if (($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) && $isOffsetAccessible->yes()) {

like I changed in AccessPropertiesRule with additional check for object? I think.

phpstan-src/src/Rules/Properties/AccessPropertiesRule.php

Line 82 in f13c2c7

if ($type->canAccessProperties()->no() || $type->canAccessProperties()->maybe() && !$scope->isUndefinedExpressionAllowed($node)) {

It's just not fixed yet :)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rajyan

rajyan commented May 4, 2022

Copy link
Copy Markdown
Contributor

I'll send a pull request for it!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@herndlm

herndlm commented May 4, 2022

Copy link
Copy Markdown
Contributor Author

ok sounds interesting, just don't spend too much time on the json_decode case. since mixed or mixed~stdClass is returned it might not work. but that's fine :)

rajyan reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rajyan

rajyan commented May 4, 2022

Copy link
Copy Markdown
Contributor

My assumption was not correct...
https://phpstan.org/r/ff9a5441-1529-42a3-b1f6-3b7cb318a7da
While null and string is offsetAccessible. IntegerType, FloatType, BooleanType is not.
This means that we need a little more complicated check in case of $scope->isUndefinedExpressionAllowed($node) === true

phpstan-src/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php

Line 59 in 356250a

if (($scope->isInExpressionAssign($node) || $scope->isUndefinedExpressionAllowed($node)) && $isOffsetAccessible->yes()) {

I'm not gonna try it now...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rajyan

rajyan commented May 4, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

so, in case of $scope->isUndefinedExpressionAllowed($node) === true what we have to check is not $isOffsetAccessible->yes() but whether there is a Type that throws a fatal error on offset access even in null-coalesce scope. I will address this issue if more related issues are reported.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@herndlm herndlm mentioned this pull request Nov 18, 2022
Cannot access offset on mixed when using isset() phpstan/phpstan#8388
Closed
@rajyan rajyan mentioned this pull request Nov 19, 2022
Improve isOffsetAccessible->no phpstan/phpstan#8393
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@herndlm @ondrejmirtes @rajyan
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.