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

chore(deps): phpstan major version 2 #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

phil-davis
Copy link
Contributor

@phil-davis phil-davis commented Dec 2, 2024

Fixes #304

Copy link

codecov bot commented Dec 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.77%. Comparing base (6ab6543) to head (4ca7f07).
Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #305      +/-   ##
============================================
+ Coverage     96.71%   96.77%   +0.05%     
  Complexity      117      117              
============================================
  Files            13       13              
  Lines           487      496       +9     
============================================
+ Hits            471      480       +9     
  Misses           16       16              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

* @param string|int|float|bool|array<int|string, mixed>|object $value
* @param string|int|float|bool|array<int|string, mixed>|object|null $value
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function actually does let you pass null to it, and will just "do nothing" to the Writer object. In that case there is nothing to serialize but it does not complain about that.

Telling PHPstan that null is possible, stops it complaining about elseif (is_object($value)) below - it now understands that $value could be nullor an object at that point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the "good thing" to do for situations like this?
An input parameter allows some "complicated" set of inputs/types.
It is too complicated to be expressed in the actual PHP function declaration.
So at run-time PHP will actually let the caller pass anything in the parameter.
The function code checks the input parameter, doing "the expected thing" to process inputs of each of the "officially allowed" inputs.

The function code throws an exception if the passed-in value is not one of the "officially allowed" inputs. But PHPstan is going to consider that that is "useless" code.

Copy link
Member

Choose a reason for hiding this comment

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

For this defensive style of programming you should set treatPhpDocTypesAsCertain: false in the PHPStan config

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I do that, it will apply to all the code. So I just added the entry to ignoreErrors

Copy link
Member

Choose a reason for hiding this comment

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

sure, if you are fine with ignoring the error thats fine, too.

} elseif (is_string($name) && is_array($item) && isset($item['attributes'])) {
} elseif (is_array($item) && isset($item['attributes'])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Array keys (name) have to be int or string. And we tested for is_int above. So PHPstan correctly reports that the is_string check here is useless.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But as a result of looking into this, I don't understand why PHPstan allows the code at line 193, elseif (is_string($name)), because we know that $name must be a string at that point.
And the else at line 198 looks useless. The exception can never be thrown, because PHP only allows array keys of type int or string, so we can never get to that InvalidArgumentException

Copy link
Member

@staabm staabm Dec 2, 2024

Choose a reason for hiding this comment

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

array keys are pretty magic and some auto-conversions are going on in the background in php-src (some strings are turned into numbers)

https://3v4l.org/amqMg

Copy link
Contributor Author

Choose a reason for hiding this comment

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

array keys are pretty magic and some auto-conversions are going on in the background in php-src (some strings are turned into numbers)

If someone tries to have a key $someArray['42'] then PHP is going to treat it as $someArray[42], and the code (both before and after this PR) is going to go into the is_int() case a few lines up. So that's a "feature" of PHP. And I don't think that there will be any callers who actually want/need to force a string key that is an ASCII decimal number.

Comment on lines +4 to +6
message: "#^Call to function is_null\\(\\) with null will always evaluate to true\\.$#"
path: lib/Serializer/functions.php
count: 1
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This message is emitted because of line 204

    } elseif (!is_null($value)) {
        throw new \InvalidArgumentException('The writer cannot serialize values of type: '.gettype($value));
    }

The if-else cases already processed all the possible types according to the type declaration of $value except for null. So PHPstan thinks that $value must be null at that point, and so we can never get that InvalidArgumentException

But someone could pass some other unusual type of value to standardSerializer The type would not fit with the PHPdoc declared set of types, but the PHP run-time is not going to complain. So "catch-all" exceptions like that are actually valid/useful.

Comment on lines -185 to -187
/**
* @var PropFindTestAsset
*/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's better not to tell the test code what $result should be.
The test code does asserts itself to check that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bump phpstan to major version 2
2 participants