The Wayback Machine - https://web.archive.org/web/20221008014444/https://github.com/composer/composer/issues/10159
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

Raising the PHPStan level to 6 #10159

Closed
45 tasks done
Seldaek opened this issue Oct 14, 2021 · 38 comments
Closed
45 tasks done

Raising the PHPStan level to 6 #10159

Seldaek opened this issue Oct 14, 2021 · 38 comments

Comments

@Seldaek
Copy link
Member

Seldaek commented Oct 14, 2021

I have laid down the ground work for reaching level 6 by typing all class properties and interface methods throughout the codebase, which should already help ensure that types are correct when they get added/copied to getters and such.

I definitely could use help though in reaching level 6, because it's a ton of work left (1465 errors for src/, 400 errors for tests/).

If you are interested in helping, here is how to go about it:

Setup a Composer environment first if you haven't

  • git clone https://github.com/composer/composer
  • cd composer && wget https://getcomposer.org/composer.phar && php composer.phar install

Setup the env for PHPStan-usage

This requires PHP 7.4 (it won't run with php8 currently due to reasons, don't try to make it do that)

  • php7.4 composer.phar phpstan-setup to get phpstan installed in the project - make sure you have a composer.phar or some global composer install to run this command with, you can't run phpstan-setup with bin/composer as it replaces parts of the vendor dir and it'll break the install

Running PHPStan

  • Pick a single sub-namespace and focus work on one area, otherwise it's too many errors and also we risk having multiple people doing the same work. Ideally you can comment here to claim a namespace if you're gonna work on it.
  • Change level from 5 to 6 in phpstan/config.neon
  • Run it with the sub-path you are interested in e.g. php7.4 vendor/bin/phpstan analyse --configuration=phpstan/config.neon src/Composer/Package/

Keep in mind

  • If you are unsure about some type, rather leave it undefined/erroring than use mixed or something equally vague, leave it for someone else to fill the gap later, because reviewing the whole code to find inappropriate mixed-usage is going to be near impossible.
  • The main focus is src/ IMO. Typing the tests can't hurt, but it might be a mess in places as some of that code is very old and using php 5.3-compatible patterns.

Thanks in advance to any volunteer :)

Status

Pick one that's not checked, and claim below in comments please.

@puka-tchou
Copy link

puka-tchou commented Oct 15, 2021

You might want to consider using rectorphp/rector. It can automatically complete and/or correct existing type declaration and even infer type return for functions and add them to your PHPdoc tags.

The rules that might help with this daunting task of typing are in the TypeDeclaration section:

Good luck!

@Seldaek
Copy link
Member Author

Seldaek commented Oct 15, 2021

Ah I wasn't aware it did type inference too, thanks, will definitely try that before wasting anyone's time doing it by hand :)

@Seldaek
Copy link
Member Author

Seldaek commented Oct 15, 2021

Hm yeah so I tried rector and the output is just way too wild and inaccurate to be useful, I end up having to double check everything and undo so much that it's really not saving any time IMO. I'm sure it's great at doing other things and it may well help later to convert docblocks to real types once we support php 7.2+.

So back to the OP plan :)

@puka-tchou
Copy link

puka-tchou commented Oct 15, 2021

Sorry that it didn't work, I haven't had a chance to test it but I was hoping it could be useful. Good luck then!

@stof
Copy link
Contributor

stof commented Oct 15, 2021

Well, Rector might help for next steps (adding type info in methods) once the properties have types. The main issue today is that all types are missing.

@Seldaek
Copy link
Member Author

Seldaek commented Oct 15, 2021

@stof do you mean PHP 7.4 style types properties? Because that's a long way out..

As for var annotations as i wrote in the op i did spend time typing all properties throughout the codebase, which should be enough for static analysis and tools like rector. But it seems to do too much magic with other things which isn't desirable IMO. Maybe I'll try to run it again only with one or two very specific rules.

@stof
Copy link
Contributor

stof commented Oct 15, 2021

@Seldaek no, I'm not talking about PHP 7.4 typed properties here, just about property types from any source.
But maybe those rector rules actually do too much magic indeed.

@samfelgar
Copy link
Contributor

samfelgar commented Oct 17, 2021

Working on src/Composer/Command

@imme-emosol
Copy link
Contributor

imme-emosol commented Oct 17, 2021

Working on DependencyResolver ..
Pool / PoolBuilder .. some issues with PackageInterface vs. BasePackage, using ->id in places "forces" the need for BasePackage as interface are not allowed to hold members.

@Seldaek
Copy link
Member Author

Seldaek commented Oct 17, 2021

@imme-emosol make sure you rebase on latest main branch. But otherwise if you still see problems, feel free to switch to require BasePackage.

@imme-emosol
Copy link
Contributor

imme-emosol commented Oct 18, 2021

Maybe it could be handy to make an extra checkbox to indicate if a namespace has been merged?

Nevermind, that is what those icons are for :s

@stof
Copy link
Contributor

stof commented Oct 18, 2021

Well, when the PR is referenced, github shows whether it is merged or no (the icon is different). And we cannot have multiple checkboxes per item in a GFM task list.

@jakobvibe
Copy link
Contributor

jakobvibe commented Oct 26, 2021

I'll have a go on some of the tests

This was referenced Oct 26, 2021
@herndlm
Copy link
Contributor

herndlm commented Oct 26, 2021

I'll work on Question, Repository, Script and Util

@ChristianBengstrom
Copy link
Contributor

ChristianBengstrom commented Oct 26, 2021

I'll try fixing the Command test.

@ChristianBengstrom
Copy link
Contributor

ChristianBengstrom commented Oct 26, 2021

As I see the Fixtures and IO tests has no errors and can be marked as done.

@megubyte
Copy link
Contributor

megubyte commented Oct 26, 2021

Happy to take a look at Installer, Downloader and DependencyResolver if these aren't taken.

@Seldaek
Copy link
Member Author

Seldaek commented Oct 26, 2021

BTW for everyone working on tests, if you see dataProvider methods which PHPStan complains about, I'd say instead of adding types feel free to rename them to provideFoo or fooProvider, which will then make PHPStan ignore the errors. I don't see much point in typing those return values as they're usually super weird nested arrays and not directly called anywhere.

@megubyte all yours, I marked them claimed now.

@jakobvibe
Copy link
Contributor

jakobvibe commented Oct 27, 2021

I'll take a go on Mock test

@jakobvibe
Copy link
Contributor

jakobvibe commented Oct 27, 2021

I'll grap Platform and Plugin tests as well

@herndlm
Copy link
Contributor

herndlm commented Oct 27, 2021

I'll work on Config and EventDispatcher

@herndlm
Copy link
Contributor

herndlm commented Oct 30, 2021

I'll go for Package

@Seldaek
Copy link
Member Author

Seldaek commented Nov 2, 2021

Alright we are done here, level 6 is now enabled! 🥳

Many many thanks to everyone involved here writing and reviewing PRs, that was a great help!

Now only 770 errors left to level 7 😉 But that'll require some more code cleanups I think and will have to wait a little.

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

No branches or pull requests