-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add baseline for old projects #2543
Comments
@jrfnl kinda, related to #2137 (from my code you can also add The with baseline feature I will not only see the numbers of errors, but also filter out the old one.
there are 2 problems with the thing I've described above
|
+1 for implementing a baseline feature. Looking at Psalm's implementation, that seems to a great way to gradually improve an existing code base. |
by the way - it is now implemented here https://github.com/DaveLiddament/sarb you can now give a link to it |
We have the desire to move our codebase from PSR-2 to PSR-12, but we are getting a fair amount of errors, mostly on the header structure. I'm not so keen on making a PR that changes a lot of files at once, as it interferes with the Git history a bit too much to my taste. So I'm very interested in a baseline feature. This would allow gradual adoption of a (new) code style without blocking existing work on our codebase. I have been playing around a little bit to see if this is feasible within the current CodeSniffer codebase and I've come to a few realisations/conclusions; I think the typical location for this would be in the reporting part of the application, because that's were you will have all the information needed. But because the check if an issue is fixable and the subsequent fix is executed the moment The approach described above would make the baseline be taken into account when running I have made a very simple proof of concept of how this could be implemented: https://github.com/timoschinkel/PHP_CodeSniffer/tree/poc/baseline What I did was add a A few side notes to the PoC; I have tried to keep it PHP 5, but I probably still used a few PHP 7 features and I did not bother to follow the CodeSniffer codestyle. The names of properties and classes are also completely open for discussion. I did attempt to extract the functionality into a separate entity as much as possible in an attempt to centralise the functionality and make it easily reusable if a refactor/rewrite of the codebase should occur in the future. By using an interface there is no need to add more conditionals to the method. My goal is to get a bit of a discussion going about this feature and whether this might be a viable approach. If so I am willing to free up some time to try and implement this. NB I have looked into creating a filter, but from the code I got the impression that the filters live on a file level, not on an issue level. |
That's not actually how it works. PHPCBF works using the reporting system. When you run PHPCBF, the PHPCBF report class is called to report on the file that was just checked. All fixing kicks off from there: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/src/Reports/Cbf.php At this point, the file has been checked and all errors and warnings are known. Ones that are to be ignored could be excluded at this point, although I don't know if the error should be excluded while adding it or after the file has been processed. That's really not the issue for me though. The real issue is that for a feature like this to go in, it needs to be accurate. Simply matching error codes to line numbers is not going to do it. A single new line of code near the top of the file would throw out the entire file baseline and show all the file's error messages again. Similarly, you can't attach errors to tokens as the token numbers will change with even the slightest modification. The bit to get right for a feature like this is detecting the pattern of the errors, so you can tell that a block of errors is the same as in the baseline, even if the line and token numbers have changed. You could attempt a diff of the old and new file contents, but that would require VCS tools to be available (to big of an assumption), or for the old file content to be cached by PHPCS (too much data). I think a better solution is to look at the relative token difference between error messages to determine if a block is still valid. So if you find a series of errors like this:
But on the next pass you see this:
You can tell that those second 2 errors are the same as the ones in the baseline, and can be ignored. This kind of checking could only be done after the entire file has been processed, so that's when I think a baseline comparison should be kicking in. |
A false assumption from my end, apologies. And thank you for adding some clarification so fast. I will dive into the code some more and try to get a better understanding of it. |
Maybe the baseline functionality in Psalm and PHPStan can be an inspiration on how to implement it in PHPCS, and having a similar behavior as those two libraries could also be good because many people will use PHPCS with either Psalm and/or PHPStan. |
If I may add on that note: Psalm's is much better than PhpStan. With psalm, With Phpstan, you can't just do a "remove only" baseline update. You can only generate a brand new baseline, and I found diffing to prevent additions to the baseline to be much harder. |
In reference to #3387 Problem:Find a way to uniquely identify where a violation originated from. Options:
Proposal:
When comparing against the baseline, calculate the same signature when a violation occurs and see if it matches. Pro:
Cons:
|
I've found that it's hard introduce PHP_CodeSniffer to old projects because it finds a lot of issues that can't be fixed at once.
Currently, as a workaround I use something like this (the code is console command from laravel app)
CodeSnifferCommand.php
CodeBeautifierCommand.php
GitCommandHelper.php
and the doc's says something like this
But the approach that I've shown above is not fully correct, since I can't change all the methods in the file and this way I need to manually find issues that where introduced in current branch/feature.
Also the baseline will be useful for CI
as examples of baselines we can look at psalm and also I've opened similar issue at DaveLiddament/sarb#26
The text was updated successfully, but these errors were encountered: