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
feat(EXPERIMENTAL): ClassKeywordFixer #2918
Conversation
I'm glad you've created this PR. It will get things moving |
Feel free to collaborate |
I'll be testing my fixer for edge cases at the moment, that's fair enough for me. |
sadly, not solving any of mentioned issues. |
Well, the following is not a solution, but an idea. What we use in our business is to put the logic/algorithm in a unit-test, that can be inserted in the SUT of the specific project. Any SUT must be content-aware by definition: In particular, this way we can have two checks, that can be (I don't know how) turned in a fixer:
I really don't know as this time how could we write a neat and simple solution for PHP-CS-Fixer, but here's how we do it. |
Could you refer specific code lines in your package you talk about? |
The code is raw and does something that is 90% alike of your project (written long before I knew it), although is 100% tested:
|
Thanks for sharing this, that's a lot of good work, especially with tests. I wonder, what would be the 10% different behavior? I think Symplify Fixer respects both conditions you mentioned. |
I think we are going a bit off topic, by the way:
|
I find this algorithm relevant to architecture of Class Fixer. I see you Fixer is more advanced than mine 🙇♂️ . Would you be able to put that into PR Fixer? |
No because, as said, a Fixer must be content-unaware, but this type of check needs to be content-aware. |
I see. Do you have an idea how to make use of your code and keep that rule to improve current situation in way? |
Yes, we could re-use a lot of PHP-CS-Fixer code in a new PHP-CS-Fixer+PHPUnit test component/plugin, in order to have checks/sniffs that are content-aware, because test suites are by definition content-aware. |
I meant in improving this PR, this Fixer here. Or do you think there is nothing we could do about that? |
There is something we can do.
We could in theory detect the two scenarios, and adapt this fixer behaviour accordingly - by throwing an error or simply dinamically disabling the single fixer - but I think @keradus considers this solution not appropriate for this project |
I think that option 1 (being context-aware when possible) could be a good solution, but the mantainer seems against that approach. Maybe we can pass the project's autoloader's file path as a rule option? By default it should be always |
@Jean85 yup, this can be a solution, making the Reminder: if we are going to do so, after requiring the autoload it should immediately be disabled, and queried without classes being effectively loaded: $loader = require $option['vendor_autoload_path']; // Composer\Autoload\ClassLoader instance
$loader->unregister(); // because register() is calles by default while requiring
if ($loader->findFile($classString)) { // and not loadClass()
// ...
} So we don't mess autoloading up and can avoid runtime autoloading issues. Of course this couple the fixer to che composer project as a dependency. |
I think you jump to conclusions here a bit :) We would very much like it, however it must be done nicely and within the scope of this project (remember it is mainly about fixing CS not a SCA tool with fix options). Including the autoload from composer can cause problems when there a conflicts with classes already loaded/defined by the tool itself (especially when different versions are used). A better solution would be to build a AST from the project that is being fixed. However non is available that supports white space. Since we deal with white space a lot we cannot leave the Token stream solution (simply because there is no good alternative available) and having both the Tokens and the AST will become very complex. So again, we would like it and it is discussed and looked into regularly, however a nice way to do it has not yet been found. |
@SpacePossum wait, either I miss something or you do. Le'ts sum up a real example. Suppose:
With the proposed solution what will happen is:
(*) = statement valid with current Composer version ^1.5 As you can see
But the are some issues:
The latter point may represent a real problem, but has mitigations:
Spawning a thread to do the entire I don't know why you are talking about ASTs here, I see no need for them. |
Other two points came to my mind:
|
Thanks for the detailed concept, I'll have a better look on it later.
Because it will provide the data we need, similar to |
UPDATE The autoload files is not an issue since composer v1 due to this check: In the next days I would like to commit my idea: @keradus your |
Just a friendly warning; |
@Slamdunk , yeah, please do not start work yet. I pushed the idea as there were rising costum fixers for it in the ecosystem. It's out of date, it's old, idea is shown. |
19a38d3
to
c8a9996
Compare
c8a9996
to
d26e21a
Compare
bb15db7
to
0f17b14
Compare
Is this feature dead? |
Can we close it, or do we still want it? |
d54c94d
to
c27eaf7
Compare
… this feature would be reusable, but for experimental feature, let's not promote usage of this logic by extracting it to standalone class
I decided to merge this PR. We finally have an aligned approach how to mark fixers as EXPERIMENTAL. This one is exactly like this. This rule is not safe to use, it will not detect all cases, and it will potentially change cases you do not want to change. It is not planned to get this fixer out of EXPERIMENTAL zone - JohnDoe should likely NOT use this fixer at all. The issue of "lack of context" (ie "is that class exists in given codebase) is out of scope for PHP CS Fixer and the fact that we did not bring it since 2017 (creation of this PR) leads to conclusion that we will rather never do so (and I have no intention to work on that). I do not plan to act anyhow further on this rule other than keeping it under main project, ensuring compatibility with all changes happening around over time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most probably should be marked as risky 😉.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good eye thanks
maybe isRisky => false
shouldn't be in abstract class 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-=> #7550
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe most of the rules are non-risky, so this is sane default. Probably would be too much to put it explicitly in every fixer.
var_dump('error with parsing class', $name); | ||
var_dump($e->getMessage()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if var_dump
s should be here 😅.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-=> #7550
creates ages ago, yet problematic due to keradus@33be691#commitcomment-23110574
if you have any nice idea how to deal with it, feel free to propose.