From 35232b3bdac012be24704d04a613a07c180f64f2 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Wed, 3 Jan 2024 17:19:49 +0100 Subject: [PATCH] Fix crash when useless default comment and statement indentation collide (#37) --- .../RemoveUselessDefaultCommentFixer.php | 1 + .../todo_change_autogenerated_comment.php.inc | 1 - .../Fixture/useless_class_comment.php.inc | 5 ---- ..._comment_and_statement_indentation.php.inc | 12 ++++++++ ...ommentAndStatementIndentationFixerTest.php | 28 +++++++++++++++++++ ...ault_comment_and_statement_indentation.php | 12 ++++++++ 6 files changed, 53 insertions(+), 6 deletions(-) create mode 100644 tests/Issues/Fixture/remove_useless_default_comment_and_statement_indentation.php.inc create mode 100644 tests/Issues/RemoveUselessDefaultCommentAndStatementIndentationFixerTest.php create mode 100644 tests/Issues/config/config_remove_useless_default_comment_and_statement_indentation.php diff --git a/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php b/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php index ac3aa1c85..ef1f503e5 100644 --- a/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php +++ b/src/Fixer/Commenting/RemoveUselessDefaultCommentFixer.php @@ -69,6 +69,7 @@ public function fix(SplFileInfo $fileInfo, Tokens $tokens): void // remove token $tokens->clearAt($index); + $tokens->removeTrailingWhitespace($index, "\n"); } } diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_change_autogenerated_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_change_autogenerated_comment.php.inc index 84b83da35..45bbfb3cf 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_change_autogenerated_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/todo_change_autogenerated_comment.php.inc @@ -10,6 +10,5 @@ namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemovePHPStormTodoComme namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemovePHPStormTodoCommentFixer\Fixture; - // TODO some other notes ?> diff --git a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_comment.php.inc b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_comment.php.inc index 008540174..87e95f09f 100644 --- a/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_comment.php.inc +++ b/tests/Fixer/Commenting/RemoveUselessDefaultCommentFixer/Fixture/useless_class_comment.php.inc @@ -38,27 +38,22 @@ class SomeClass5 namespace Symplify\CodingStandard\Tests\Fixer\Commenting\RemoveUselessClassCommentFixer\Fixture; - class SomeClass1 { } - class SomeClass2 { } - class SomeClass3 { } - class SomeClass4 { } - class SomeClass5 { } diff --git a/tests/Issues/Fixture/remove_useless_default_comment_and_statement_indentation.php.inc b/tests/Issues/Fixture/remove_useless_default_comment_and_statement_indentation.php.inc new file mode 100644 index 000000000..1dcbf4358 --- /dev/null +++ b/tests/Issues/Fixture/remove_useless_default_comment_and_statement_indentation.php.inc @@ -0,0 +1,12 @@ +doTestFile($filePath); + } + + public static function provideData(): Iterator + { + yield [__DIR__ . '/Fixture/remove_useless_default_comment_and_statement_indentation.php.inc']; + } + + public function provideConfig(): string + { + return __DIR__ . '/config/config_remove_useless_default_comment_and_statement_indentation.php'; + } +} diff --git a/tests/Issues/config/config_remove_useless_default_comment_and_statement_indentation.php b/tests/Issues/config/config_remove_useless_default_comment_and_statement_indentation.php new file mode 100644 index 000000000..28b1a5f7e --- /dev/null +++ b/tests/Issues/config/config_remove_useless_default_comment_and_statement_indentation.php @@ -0,0 +1,12 @@ +rule(RemoveUselessDefaultCommentFixer::class); + $ecsConfig->rule(StatementIndentationFixer::class); +};