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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
}
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.64",
"phpstan/phpstan": "^1.12",
"friendsofphp/php-cs-fixer": "^3.65",
"phpstan/phpstan": "^2.0",
"phpunit/phpunit" : "^9.6"
},
"scripts": {
Expand Down
4 changes: 2 additions & 2 deletions lib/Serializer/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ function repeatingElements(Writer $writer, array $items, string $childElementNam
*
* You can even mix the two array syntaxes.
*
* @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.

*/
function standardSerializer(Writer $writer, $value): void
{
Expand Down Expand Up @@ -181,7 +181,7 @@ function standardSerializer(Writer $writer, $value): void
// This item has a numeric index. We just loop through the
// array and throw it back in the writer.
standardSerializer($writer, $item);
} 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.

// The key is used for a name, but $item has 'attributes' and
// possibly 'value'
$writer->startElement($name);
Expand Down
6 changes: 5 additions & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
parameters:
ignoreErrors:
ignoreErrors:
-
message: "#^Call to function is_null\\(\\) with null will always evaluate to true\\.$#"
path: lib/Serializer/functions.php
count: 1
Comment on lines +4 to +6
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.

2 changes: 1 addition & 1 deletion tests/Sabre/Xml/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ public function testParseProblem(): void
$output = $reader->parse();
$this->fail('We expected a ParseException to be thrown');
} catch (LibXMLException $e) {
self::assertIsArray($e->getErrors());
self::assertNotEmpty($e->getErrors());
}
}

Expand Down
3 changes: 0 additions & 3 deletions tests/Sabre/Xml/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ public function testEmptyPropfind(string $xml): void
$util->namespaceMap = [
'http://sabre.io/ns' => 'stdClass',
];
/**
* @var PropFindTestAsset
*/
Comment on lines -185 to -187
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.

$result = $util->expect('{DAV:}propfind', $xml);
self::assertIsObject($result);
self::assertInstanceOf(PropFindTestAsset::class, $result);
Expand Down