Skip to content

Resolve edge cases cought by PHPStan level 7#672

Merged
kylekatarnls merged 11 commits intomasterfrom
phpstan7
Aug 12, 2023
Merged

Resolve edge cases cought by PHPStan level 7#672
kylekatarnls merged 11 commits intomasterfrom
phpstan7

Conversation

@AJenbo
Copy link
Collaborator

@AJenbo AJenbo commented Jul 9, 2023

Type: bugfix / refactoring / documentation update
Breaking change: no

Resolve about half of the issues detected by PHPStan at level 7. This mostly involves handles a few edge cases and improve some PHPDoc. And a few assert() to validate what is asserted by peek().

@codecov-commenter
Copy link

codecov-commenter commented Jul 9, 2023

Codecov Report

Patch coverage: 71.53% and project coverage change: +0.37% 🎉

Comparison is base (7ab5e96) 82.09% compared to head (a1c76c5) 82.46%.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #672      +/-   ##
============================================
+ Coverage     82.09%   82.46%   +0.37%     
- Complexity     3443     3460      +17     
============================================
  Files           154      154              
  Lines          9471     9491      +20     
============================================
+ Hits           7775     7827      +52     
+ Misses         1696     1664      -32     
Files Changed Coverage Δ
...epend/Metrics/Analyzer/NPathComplexityAnalyzer.php 98.41% <ø> (ø)
...PDepend/Source/Language/PHP/PHPParserVersion70.php 88.44% <0.00%> (ø)
src/main/php/PDepend/Util/MathUtil.php 0.00% <ø> (ø)
...rc/main/php/PDepend/Source/AST/AbstractASTType.php 92.00% <50.00%> (-0.69%) ⬇️
.../PDepend/Source/Language/PHP/AbstractPHPParser.php 88.10% <58.57%> (+0.13%) ⬆️
.../php/PDepend/Util/Cache/Driver/FileCacheDriver.php 95.45% <70.00%> (-4.55%) ⬇️
...PDepend/Source/Language/PHP/PHPParserVersion80.php 79.22% <72.72%> (+0.55%) ⬆️
...main/php/PDepend/Source/AST/ASTCompilationUnit.php 98.36% <100.00%> (+0.05%) ⬆️
...ain/php/PDepend/Source/AST/AbstractASTCallable.php 96.46% <100.00%> (+0.06%) ⬆️
...PDepend/Source/Language/PHP/PHPParserVersion53.php 100.00% <100.00%> (+18.75%) ⬆️
... and 9 more

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

kylekatarnls
kylekatarnls previously approved these changes Jul 10, 2023
@AJenbo
Copy link
Collaborator Author

AJenbo commented Aug 10, 2023

@kylekatarnls thanks, lets merge it once you approve it

@kylekatarnls
Copy link
Member

We have coverage decreasing since those edge-cases are not covered by tests.

It would be nice to see if we can actually reproduce (if those are real edge-cases then we could have tests for them), or if they are only theoretical but cannot happen in practice, if so it would be better to handle with assert() rather than actual exceptions.

@AJenbo
Copy link
Collaborator Author

AJenbo commented Aug 10, 2023

Yeah, I tried doing my best to deduce if they where possible or not and used assert() in that way. I currently do not have time to work on this, but I'm happy to review it if you have time to do so.

Copy link
Collaborator Author

@AJenbo AJenbo left a comment

Choose a reason for hiding this comment

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

Thanks for updating the pr 🙂

- getUnexpectedTokenException now expects a token
- getUnexpectedNextTokenException checks next token
Copy link
Collaborator Author

@AJenbo AJenbo left a comment

Choose a reason for hiding this comment

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

Nice change regarding getUnexpectedNextTokenException() 👍

... and requireNextToken(), I see coverage is now positive and you might even have addressed some more of the level 7 issues then I originally did.

@kylekatarnls
Copy link
Member

I hope I don't get carried too far away. I introduced some methods for repeated logic and split getUnexpectedTokenException because realizing it breaks the PSR, and has unexpected behavior when a null value is the value of the passed argument and not the default value because no argument passed.

And now when we need the next token but no possible end-of-file expected, then we can use requireNextToken() so TokenStreamEndException is taken care of, and the result is guaranteed to be a Token.

@kylekatarnls
Copy link
Member

Oh and I forgot, I also replaced:

$tokenType = $this->tokenizer->peek();

if (...) {
    return ...
} elseif ($tokenType === Tokenizer::T_EOF) {
    throw new TokenStreamEndException($this->tokenizer);
}

throw $this->getUnexpectedNextTokenException();

With just:

$tokenType = $this->tokenizer->peek();

if (...) {
    return ...
}

throw $this->getUnexpectedNextTokenException();

Because TokenStreamEndException is already handled by getUnexpectedNextTokenException. This redundant pattern is actually not new.

kylekatarnls
kylekatarnls previously approved these changes Aug 12, 2023
Copy link
Member

@kylekatarnls kylekatarnls left a comment

Choose a reason for hiding this comment

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

Coverage should be improved, but first we should run it with PHP 8.2, it's still on 7.4 which make some coverage missing just because tests are skipped.

So we'll handle in a next PR.

kylekatarnls
kylekatarnls previously approved these changes Aug 12, 2023
@kylekatarnls kylekatarnls merged commit 3b7082f into master Aug 12, 2023
@kylekatarnls kylekatarnls deleted the phpstan7 branch August 12, 2023 17:55
@kylekatarnls kylekatarnls added this to the 2.15.0 milestone Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants