Skip to content

chore: fix plus.*NonNumeric errors from PHPStan#9260

Merged
kubawerlos merged 2 commits intoPHP-CS-Fixer:masterfrom
6b7562617765726c6f73:fix_plus_phpstan_errors
Dec 2, 2025
Merged

chore: fix plus.*NonNumeric errors from PHPStan#9260
kubawerlos merged 2 commits intoPHP-CS-Fixer:masterfrom
6b7562617765726c6f73:fix_plus_phpstan_errors

Conversation

@kubawerlos
Copy link
Copy Markdown
Member

No description provided.

@coveralls
Copy link
Copy Markdown

coveralls commented Dec 2, 2025

Coverage Status

coverage: 93.162% (-0.01%) from 93.172%
when pulling 00efc03 on 6b7562617765726c6f73:fix_plus_phpstan_errors
into f99d88e on PHP-CS-Fixer:master.

@kubawerlos kubawerlos enabled auto-merge (squash) December 2, 2025 18:50
@kubawerlos kubawerlos merged commit 5674c5d into PHP-CS-Fixer:master Dec 2, 2025
59 of 61 checks passed
@kubawerlos kubawerlos deleted the fix_plus_phpstan_errors branch December 2, 2025 19:00
if (substr_count($content, "\n") > 1) {
// the final bit of the whitespace must be the next statement's indentation
$tokens[$index] = new Token([\T_WHITESPACE, $this->whitespacesConfig->getLineEnding().substr($content, strrpos($content, "\n") + 1)]);
$tokens[$index] = new Token([\T_WHITESPACE, $this->whitespacesConfig->getLineEnding().substr($content, (int) strrpos($content, "\n") + 1)]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO it is not a good idea to cast the potential false value to int. I would still prefer something like #9058.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can change to thecodingmachine/safe, or we can create a custom wrapper.

IMO the bad idea is to keep them, which (with other baseline errors) is stopping from bumping PHPStan level up, which could uncover some actually buggy places in code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

refs the discussion about strict types and casting here: #8877 (reply in thread)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Each of the cases here are always integers, unless I was stupid and they could false.

I'm happy to be proven the latter with a test case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In #9059 I found a case where continuing with the false value (implicitly casted to 0) was a problem.
I think with the explicit casts it is harder to spot those issues. I found the bug after adding explicit validation via Str helper class.

IMO the bad idea is to keep them, which (with other baseline errors) is stopping from bumping PHPStan level up, which could uncover some actually buggy places in code.

I agree that we should work on increasing the phpstan level. And with #9058 and #9008 I also try to solve some pain points in higher levels.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

in most of the cases, that value should be int already. yet, it's easy to not see some edge case there, or get fooled after some refactoring (eg we now explicitly check \\ in string, so we know strpos won't be false, but after some refactoring, flow can be different),

but then, returning false is not error - it's that string was not found.

tbh, i would rather go with assertions of type before casting it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants