Skip to content

PHPStan Level 3 #385

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

Merged
merged 1 commit into from
Nov 21, 2022
Merged

PHPStan Level 3 #385

merged 1 commit into from
Nov 21, 2022

Conversation

dantleech
Copy link
Contributor

@dantleech dantleech commented Sep 22, 2022

👋 in Phpactor one of the most common types of error is accessing a member on NULL on TP parser nodes. So I thought I'd try and increase the type coverage.

This PR:

  • Updates PHPStan to level 3.
  • Fixes type hints (avoiding refactoring code).
  • Adds PHPStan as a dev dependency (it ships as a PHAR now, so no conflicts).
  • Removes the overridden public opeand property on the children of UnaryExpression

As a follow up question is it worth ditching Travis and adding PHPStan to the main GHA workflow?

We should be able to get to PHPStan level 5 pretty easy (30 errors) but level 6 is 368 errors 😅

@@ -625,6 +628,7 @@ public function getNamespaceDefinition() {
$namespaceDefinition = null;
}

/** @var NamespaceDefinition|null $namespaceDefinition */
return $namespaceDefinition;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

we could add a runtime check for stuff like this or refactor but it's probably a risky move considering the number of similar changes needed across the whole code base.

@dantleech
Copy link
Contributor Author

Rebased my changes on main after making them on master...

@@ -27,7 +27,6 @@ cache:
- validation/frameworks

before_script:
- if [[ $STATIC_ANALYSIS = true ]]; then composer require phpstan/phpstan --no-update; fi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

required in --dev now as PHPStan is a PHAR

* @return InterfaceMembers
*/
private function parseInterfaceMembers($parentNode) : Node {
private function parseInterfaceMembers($parentNode) : InterfaceMembers {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

probably dozens of other places we can do this, but figure PHPStan will pick this up on level 4 or 5

@dantleech
Copy link
Contributor Author

Updated

roblourens
roblourens previously approved these changes Sep 25, 2022
Copy link
Member

@roblourens roblourens 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 doing this and thanks for the review @TysonAndre. lgtm but I'll wait for the final ✔️ from @TysonAndre

@roblourens
Copy link
Member

And yeah, if we can get all the same stuff into github actions that exists in Travis, I would like to get rid of Travis.

TysonAndre
TysonAndre previously approved these changes Sep 25, 2022
if ($expression instanceof Node) {
$expression->parent = $scopedPropertyAccessExpression;
$scopedPropertyAccessExpression->scopeResolutionQualifier = $expression; // TODO ensure always a Node

// scopeResolutionQualifier does not accept `Node` but
Copy link
Contributor

@TysonAndre TysonAndre Sep 25, 2022

Choose a reason for hiding this comment

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

LGTM overall for latest commits; they addressed review comments/nits.


In followup PRs, that should hopefully be addressed by making parameter types more precise as well, assuming that phpstan can narrow a mix of subclasses of Node and definite non-subclassess to just the expected subclasses Expression|QualifiedName (haven't checked)

(Strict typing overall is useful so we don't accidentally write to an undeclared property, which would be a notice in 8.2 since the classes declared here aren't using #[\AllowDynamicProperties], though I don't need AllowDynamicProperties for my own use case)

TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Sep 25, 2022
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- After microsoft#385 is merged we can probably start running phpstan in GitHub
  Actions for a single php version and remove .travis.yml entirely
  (as a followup PR)
TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Sep 25, 2022
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- Remove travis config
- After microsoft#385 is merged run_phpstan.sh can remove the separate install
  step (as a followup PR)
TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Sep 25, 2022
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- Remove travis config
- After microsoft#385 is merged run_phpstan.sh can remove the separate install
  step (as a followup PR)
@TysonAndre
Copy link
Contributor

This should be rebased against main now that other changes were merged yesterday, to re-run tests/analysis steps. Optionally, It'd be useful to also squash changes (https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History Squashing Commits) either here or when maintainers merge this in the web ui


Other than not being rebased, all of the changes here should be safe to release, though I'd rather release 0.1.2 first if you expect merging this to delay the process of publishing a release - 0.1.2 is getting rather large

Removes the overridden public operand property on the children of UnaryExpression

Looks correct and the property is still declared on the subclasses. Double checking: This is confirmed by the fact that tests continue to pass, so CHILD_NAMES obviously still refers to properties that exist.

@dantleech
Copy link
Contributor Author

Updated and squashed

TysonAndre
TysonAndre previously approved these changes Sep 25, 2022
Copy link
Contributor

@TysonAndre TysonAndre left a comment

Choose a reason for hiding this comment

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

lgtm again; I confirmed locally that there are no new/missing changes in the rebase

@TysonAndre
Copy link
Contributor

 ------ ---------------------------------------------------------------------- 
  Line   Parser.php                                                            
 ------ ---------------------------------------------------------------------- 
  3544   Property                                                              
         Microsoft\PhpParser\Node\Statement\NamespaceDefinition::$name         
         (Microsoft\PhpParser\Node\QualifiedName|null) does not accept         
         Microsoft\PhpParser\MissingToken.                                     
  3579   Property Microsoft\PhpParser\Node\NamespaceUseClause::$namespaceName  
         (Microsoft\PhpParser\Node\QualifiedName) does not accept              
         Microsoft\PhpParser\MissingToken. 

Property declarations should say MissingToken now?

@dantleech
Copy link
Contributor Author

Property declarations should say MissingToken now?

seems like it:

https://github.com/dantleech/tolerant-php-parser/blob/9bcf16fb6da32c6d504d9e1e5b0dcfe0762efdd5/src/Parser.php#L3544

updated

@dantleech
Copy link
Contributor Author

dantleech commented Sep 28, 2022

FTR I've created a PR on my repo against this branch for level 4: dantleech#1 although probably best to merge this first before reviewing.

Added ReturnTypeWillChange

Fix return type for Expression

Add type hints for returned variables

Add generic parameter for delimted list

Fixing type hints

Ignore co-variance error

Remove TODO - it already must always be a Node

Ignore error

Add missing types

Update phpstan to level 3

Add phpstan to dev reqs

No need to install phpstan independently

Do not override unary expression operand

It seems to me that the operand can be any expression

Added token to operand types -- should this be MissingToken?

Include tokens in return types

ExpressionStatement => EchoStatement

It seems this is always an EchoStatement not an ExpressionStatement

unaryExpressionOrHigher can return a ThrowExpression

Remove overridden property

Add Token to union

Remove QualifiedName and rename local variable

Remove unused import

Add return types

Remove trailing whitespace

Add MissingToken type
Copy link
Contributor

@TysonAndre TysonAndre left a comment

Choose a reason for hiding this comment

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

Not a maintainer, but familiar with this repo - I don't see any issues in the latest patch set. The changed types look accurate

TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Oct 8, 2022
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- Remove travis config
- After microsoft#385 is merged run_phpstan.sh can remove the separate install
  step (as a followup PR)
- Require a newer phpunit patch version in devDependencies
  Make it harder for contributors to have any inconsistencies with CI
  when running tests (e.g. php version support, fixed phpunit bugs, etc)
TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Oct 8, 2022
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- Remove travis config
- After microsoft#385 is merged run_phpstan.sh can remove the separate install
  step (as a followup PR)
- Require a newer phpunit patch version in devDependencies
  Make it harder for contributors to have any inconsistencies with CI
  when running tests (e.g. php version support, fixed phpunit bugs, etc)
@dantleech
Copy link
Contributor Author

appologetic ping cc @roblourens

@dantleech
Copy link
Contributor Author

...

@roblourens
Copy link
Member

Sorry @dantleech. Will take a look ASAP

@dantleech
Copy link
Contributor Author

No pressure :) thanks.

@roblourens roblourens merged commit 6bd3e3b into microsoft:main Nov 21, 2022
TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Jan 3, 2024
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- Remove travis config
- After microsoft#385 is merged run_phpstan.sh can remove the separate install
  step (as a followup PR)
- Require a newer phpunit patch version in devDependencies
  Make it harder for contributors to have any inconsistencies with CI
  when running tests (e.g. php version support, fixed phpunit bugs, etc)
TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Jan 3, 2024
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- Remove travis config
- After microsoft#385 is merged run_phpstan.sh can remove the separate install
  step (as a followup PR)
- Require a newer phpunit patch version in devDependencies
  Make it harder for contributors to have any inconsistencies with CI
  when running tests (e.g. php version support, fixed phpunit bugs, etc)
TysonAndre added a commit to TysonAndre/tolerant-php-parser that referenced this pull request Jan 3, 2024
- Perform a shallow clone of submodules analyzed in validation
  test suite in GitHub workflows (should take around 30 seconds)
- Switch badge in README.md to GitHub Workflows and update default
  branch for badge.
- Remove travis config
- After microsoft#385 is merged run_phpstan.sh can remove the separate install
  step (as a followup PR)
- Require a newer phpunit patch version in devDependencies
  Make it harder for contributors to have any inconsistencies with CI
  when running tests (e.g. php version support, fixed phpunit bugs, etc)
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.

4 participants