-
-
Notifications
You must be signed in to change notification settings - Fork 78
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
Coding standard (via PHP CS Fixer) #44
base: master
Are you sure you want to change the base?
Conversation
I'm not sure if you have a global standard for |
8488088
to
8ef4953
Compare
8ef4953
to
227d30d
Compare
Hi, a couple questions:
|
Another one: I've run php-cs-fixer with your config, and I can see that amongst the stuff that was "fixed", a lot goes against what I would choose as a coding standard for this project. Some examples:
What would be the best approach here? Start with the Symfony rules and override some of them? Use another base coding standard? Start a list of fixes from scratch? |
I think it would make sense to try and find a coding standard that aligns well with how @BenMorel writes PHP or wants to write it, and then merge in something to enforce that. That PR would need to do any remaining fixes at the same time, since as far as I know there isn't any base-lining facility in a code style checker. It has been requested for PHP_CodeSniffer: squizlabs/PHP_CodeSniffer#2543 You could start with a config like the following, which the code here follows almost 100%. There's just one violation in src and five in tests. <?php
return \PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'binary_operator_spaces' => false,
'concat_space' => false,
'increment_style' => false,
'native_function_invocation' => false,
'no_superfluous_phpdoc_tags' => false,
'phpdoc_align' => false,
'phpdoc_annotation_without_dot' => false,
'phpdoc_inline_tag' => false,
'phpdoc_summary' => false,
'phpdoc_to_comment' => false,
'return_type_declaration' => false,
'self_accessor' => false,
'single_line_throw' => false,
'trailing_comma_in_multiline_array' => false,
'unary_operator_spaces' => false,
'yoda_style' => false,
])
->setFinder(PhpCsFixer\Finder::create()->files()->in([__DIR__ . '/src', __DIR__ . '/tests'])->name('*.php')); Then subsequent PRs could add more rules a few at a time either by putting back some of those from the Symfony set or by adding other rules, e.g. perhaps to ban yoda conditions and assignment in conditionals if that's your style. Happy to PR this including the CI implementation and try to make sure it's easy to understand for other contributors. Ideally I think an automatic fixer might be set up to see style problems and fix them itself instead of asking humans to do the fixes. |
You can also ban yoda-style if you want, using |
@bdsl there is baselining functionally for php codeSniffer. See https://github.com/DaveLiddament/sarb |
@vladyslavstartsev Ah nice. I actually heard Dave Liddamen give a talk about static analysis and Sarb at PHP London before it was released. Didn't realize it supported PHP CodeSniffer. That could be a good option then. |
I've used both, although recently only php-cs-fixer. I think either one is probably fine. I think the big differentiator is that php-cs-fixer is all about the automatic fixing and generally only reports issues that it can automatically fix. So people just need to run the fix command before submitting their PRs if it fails. PHP CodeSniffer more often reports problems that it doesn't have an automatic fix for, like lines being too long. |
Also with PHP CodeSniffer you can use comments to turn it off if there are sections of code where you think the usual rules shouldn't apply. PHP-CS-Fixer does not allow that. |
if i hadn't setup php-cs-fixer and php-code-sniffer in my projects already i would give https://github.com/symplify/easy-coding-standard a try as it combines php-cs-fixer and php-code-sniffer and allows to exclude files. so i would go for ecs & phpstan |
@c33s Hi, just came across this randomly. Let me know if you need any help with setting up ECS. |
@TomasVotruba You're welcome to send a PR! 🙂 I actually tried ECS the other day on another project, but didn't go very far. I'd be interested to see a setup that gets as close as possible to the current coding style of this project. Or, if there's a de-facto standard that's close enough to the current coding style, that this project could benefit from at the cost of minor coding style changes, I'm all ears as well (it may be better than having a complex set of rules). |
250664c
to
8fded77
Compare
860a906
to
de84657
Compare
3b78067
to
f2a9108
Compare
@BenMorel, what do you think about it? We can also include PHP CS Fixer as a checker, to break the build if the code doesn't follow chosen rules.