PHP-CS-Fixer: Comment wrongly indented
CS fixer version: latest 2.16
Original code and expected result:
class CrudHelper
{
public function makeCrudFromModel($model): MyCrud
{
// set model options
if ($model instanceof Model\Invoice) {
$model->getField($model->fieldName()->my_address_id)->default = 123456879;
// @TODO
} elseif ($model instanceof Model\InvoiceItem) {
// @TODO
$a = [
// 'test',
];
}
// test
}
}
badly fixed to:
class CrudHelper
{
public function makeCrudFromModel($model): MyCrud
{
// set model options
if ($model instanceof Model\Invoice) {
$model->getField($model->fieldName()->my_address_id)->default = 123456879;
// @TODO
} elseif ($model instanceof Model\InvoiceItem) {
// @TODO
$a = [
// 'test',
];
}
// test
}
}
there are two indent issues:
- 4 spaces are removed badly for the first
// @TODOcomment - related with https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/6490#issuecomment-1186203584 - code commented with
//right after a new line should never be moved, even if inside an array
config:
<?php
$finder = PhpCsFixer\Finder::create()
->in([__DIR__])
->exclude([
'cache',
'build',
'vendor',
]);
return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' =>true,
'@PHP71Migration:risky' => true,
'@PHP73Migration' => true,
// required by PSR-12
'concat_space' => [
'spacing' => 'one',
],
// disable some too strict rules
'phpdoc_types' => [
// keep enabled, but without "alias" group to not fix
// "Callback" to "callback" in phpdoc
'groups' => ['simple', 'meta']
],
'phpdoc_types_order' => [
'null_adjustment' => 'always_last',
'sort_algorithm' => 'none',
],
'single_line_throw' => false,
'yoda_style' => [
'equal' => false,
'identical' => false,
],
'native_function_invocation' => false,
'non_printable_character' => [
'use_escape_sequences_in_strings' => true,
],
'void_return' => false,
'combine_consecutive_issets' => false,
'combine_consecutive_unsets' => false,
'multiline_whitespace_before_semicolons' => false,
'no_superfluous_elseif' => false,
'ordered_class_elements' => false,
'php_unit_internal_class' => false,
'php_unit_test_case_static_method_calls' => [
'call_type' => 'this',
],
'php_unit_test_class_requires_covers' => false,
'phpdoc_add_missing_param_annotation' => false,
'return_assignment' => false,
'comment_to_phpdoc' => false,
'list_syntax' => ['syntax' => 'short'],
'general_phpdoc_annotation_remove' => [
'annotations' => ['author', 'copyright', 'throws'],
],
'nullable_type_declaration_for_default_null_value' => [
'use_nullable_type_declaration' => false,
],
])
->setFinder($finder)
->setCacheFile(__DIR__ . '/.php_cs.cache');
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 16 (10 by maintainers)
The default value doesn’t matter. If we want to have such option, we need to implement it and maintain it in time. I disagree we should work on that here. If really important, one can still fork the fixer and tweak it to their needs.
Keeping commented-out code is a bad practice IMO. I don’t want it to be supported by the tool.
array_indentationwill indent comments inside arrays.Should add some config that leave all comments untouched.
But why? I totally don’t get the reasoning. What’s the advantage of non-indented code?
In this particular case I see it mostly as an IDE issue, which can’t toggle comments with indentation in mind, but adds
//at the beginning of the line. And since I am against keeping commented code in the project (Git is for that), I don’t see a reason to support it in Fixer as a core rule/option.@julienfalque can we rediscuss this?
aren’t there more usecases as “Keeping commented-out code” for allowing developers to configure the handling of comments?
for example i use tools like phpstan or php-coding-standard which allow us to ignore a line or the next line via comment. i prefer to have this comment sitting on the left as i don’t “count” them to the code itself.
i would love to have a setting where i can define that comments which are on the start of a line are kept where they are.
as php-cs-fixer doesn’t have a feature (yet) where i can ignore the next line the only way to allow this specific coding standard is to disable
statement_indentationwhich is far worse than also allow developers to keep commented-out code (or in my case style my phpstan ignores)what do you think? is this usecase relevant enough to rediscuss this?
Or the comments do not represent code that is commented out (like your own provided example here
// test) but are just improperly indented comments and therefore should be fixed by the rule. I don’t think we should support the case of improperly indented comment out code, I agree with Julien. I think this issue can be closed.This part should be fixed by #4884.