From 1b33a8c8298ce62d07d4743034aa2e8da228233d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 19 Jun 2023 09:35:21 +0100 Subject: [PATCH] Remove DocBlockLineLengthFixer, as not used and often breaking, better handle manually (#24) --- README.md | 5 +- docs/rules_overview.md | 42 +--- .../LineLength/DocBlockLineLengthFixer.php | 235 ------------------ src/ValueObject/DocBlockLines.php | 45 ---- .../DocBlockLinesFactory.php | 80 ------ .../DocBlockLineLengthFixerTest.php | 29 --- .../do_not_reformat_annotations.php.inc | 26 -- .../do_not_reformat_inheritdoc.php.inc | 25 -- .../Fixture/indented_doc_block.php.inc | 28 --- .../Fixture/inline_doc_block.php.inc | 22 -- .../Fixture/make_multi_line_if_needed.php.inc | 20 -- .../Fixture/skip_bullets.php.inc | 10 - ...ip_doc_block_with_only_annotations.php.inc | 21 -- ...ne_doc_block_with_only_annotations.php.inc | 17 -- .../Fixture/word_wrapping.php.inc | 22 -- .../word_wrapping_with_multiple_lines.php.inc | 24 -- ..._wrapping_with_multiple_paragraphs.php.inc | 31 --- .../config/configured_rule.php | 12 - 18 files changed, 7 insertions(+), 687 deletions(-) delete mode 100644 src/Fixer/LineLength/DocBlockLineLengthFixer.php delete mode 100644 src/ValueObject/DocBlockLines.php delete mode 100644 src/ValueObjectFactory/DocBlockLinesFactory.php delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/DocBlockLineLengthFixerTest.php delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_annotations.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_inheritdoc.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/indented_doc_block.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/inline_doc_block.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/make_multi_line_if_needed.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_bullets.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_doc_block_with_only_annotations.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_single_line_doc_block_with_only_annotations.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_lines.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_paragraphs.php.inc delete mode 100644 tests/Fixer/LineLength/DocBlockLineLengthFixer/config/configured_rule.php diff --git a/README.md b/README.md index 6ee1fe4a5..4b28d9490 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,11 @@ composer require symplify/easy-coding-standard --dev ```diff # ecs.php + use Symplify\EasyCodingStandard\Config\ECSConfig; +use Symplify\EasyCodingStandard\ValueObject\Set\SetList; - return static function (ContainerConfigurator $containerConfigurator): void { -+ $containerConfigurator->import(SetList::SYMPLIFY); + return static function (ECSConfig $ecsConfig): void { ++ $ecsConfig->sets([SetList::SYMPLIFY]); ``` ## Rules Overview diff --git a/docs/rules_overview.md b/docs/rules_overview.md index ad94c56fe..5a929cad1 100644 --- a/docs/rules_overview.md +++ b/docs/rules_overview.md @@ -1,4 +1,4 @@ -# 13 Rules Overview +# 12 Rules Overview ## ArrayListItemNewlineFixer @@ -43,42 +43,6 @@ Strict type declaration has to be followed by empty line
-## DocBlockLineLengthFixer - -Docblock lenght should fit expected width - -:wrench: **configure it!** - -- class: [`Symplify\CodingStandard\Fixer\LineLength\DocBlockLineLengthFixer`](../src/Fixer/LineLength/DocBlockLineLengthFixer.php) - -```php -ruleWithConfiguration(DocBlockLineLengthFixer::class, [DocBlockLineLengthFixer::LINE_LENGTH => 40]); -}; -``` - -↓ - -```diff - /** -- * Super long doc block description -+ * Super long doc -+ * block description - */ - function some() - { - } -``` - -
- ## LineLengthFixer Array items, method parameters, method call arguments, new arguments should be on same/standalone line to fit line length. @@ -96,7 +60,9 @@ use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer; use Symplify\EasyCodingStandard\Config\ECSConfig; return static function (ECSConfig $ecsConfig): void { - $ecsConfig->ruleWithConfiguration(LineLengthFixer::class, [LineLengthFixer::LINE_LENGTH => 40]); + $ecsConfig->ruleWithConfiguration(LineLengthFixer::class, [ + LineLengthFixer::LINE_LENGTH => 40, + ]); }; ``` diff --git a/src/Fixer/LineLength/DocBlockLineLengthFixer.php b/src/Fixer/LineLength/DocBlockLineLengthFixer.php deleted file mode 100644 index fef005bbf..000000000 --- a/src/Fixer/LineLength/DocBlockLineLengthFixer.php +++ /dev/null @@ -1,235 +0,0 @@ -\s*) \*/m'; - - /** - * @var string - */ - private const INDENTATION_PART = 'indentation_part'; - - /** - * @var int - */ - private const DEFAULT_LINE_LENGHT = 120; - - private int $lineLength = self::DEFAULT_LINE_LENGHT; - - public function __construct( - private readonly DocBlockLinesFactory $docBlockLinesFactory - ) { - } - - public function getDefinition(): FixerDefinitionInterface - { - return new FixerDefinition(self::ERROR_MESSAGE, []); - } - - /** - * @param Tokens $tokens - */ - public function isCandidate(Tokens $tokens): bool - { - return $tokens->isTokenKindFound(T_DOC_COMMENT); - } - - /** - * @param Tokens $tokens - */ - public function fix(SplFileInfo $fileInfo, Tokens $tokens): void - { - // function arguments, function call parameters, lambda use() - for ($position = count($tokens) - 1; $position >= 0; --$position) { - /** @var Token $token */ - $token = $tokens[$position]; - if (! $token->isGivenKind(T_DOC_COMMENT)) { - continue; - } - - $docBlock = $token->getContent(); - $docBlockLines = $this->docBlockLinesFactory->createFromDocBlock($docBlock); - - // The available line length is the configured line length, minus the existing indentation, minus ' * ' - $indentationString = $this->resolveIndentationStringFor($docBlock); - $maximumLineLength = $this->lineLength - strlen($indentationString) - 3; - - $descriptionLines = $docBlockLines->getDescriptionLines(); - if ($descriptionLines === []) { - continue; - } - - if ($docBlockLines->hasListDescriptionLines()) { - continue; - } - - $paragraphs = $this->extractParagraphsFromDescriptionLines($descriptionLines); - - $lineWrappedParagraphs = $this->wrapParagraphs($paragraphs, $maximumLineLength); - - $wrappedDescription = implode(PHP_EOL . PHP_EOL, $lineWrappedParagraphs); - $otherLines = $docBlockLines->getOtherLines(); - if ($otherLines !== []) { - $wrappedDescription .= "\n"; - } - - $reformattedLines = array_merge($this->getLines($wrappedDescription), $otherLines); - - $newDocBlockContent = $this->formatLinesAsDocBlockContent($reformattedLines, $indentationString); - if ($docBlock === $newDocBlockContent) { - continue; - } - - $tokens[$position] = new Token([T_DOC_COMMENT, $newDocBlockContent]); - } - } - - /** - * @param array $configuration - */ - public function configure(array $configuration): void - { - $this->lineLength = $configuration[self::LINE_LENGTH] ?? self::DEFAULT_LINE_LENGHT; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition(self::ERROR_MESSAGE, [ - new ConfiguredCodeSample( - <<<'CODE_SAMPLE' -/** - * Super long doc block description - */ -function some() -{ -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -/** - * Super long doc - * block description - */ -function some() -{ -} -CODE_SAMPLE - , - [ - self::LINE_LENGTH => 40, - ] - ), - ]); - } - - public function getConfigurationDefinition(): FixerConfigurationResolverInterface - { - throw new ShouldNotHappenException(); - } - - private function resolveIndentationStringFor(string $docBlock): string - { - $matches = Strings::match($docBlock, self::INDENTATION_BEFORE_ASTERISK_REGEX); - return $matches[self::INDENTATION_PART] ?? ''; - } - - /** - * @param array $docBlockLines - */ - private function formatLinesAsDocBlockContent(array $docBlockLines, string $indentationString): string - { - foreach ($docBlockLines as $index => $docBlockLine) { - $docBlockLines[$index] = $indentationString . ' *' . ($docBlockLine !== '' ? ' ' : '') . $docBlockLine; - } - - array_unshift($docBlockLines, '/**'); - $docBlockLines[] = $indentationString . ' */'; - - return implode(PHP_EOL, $docBlockLines); - } - - /** - * @param array $descriptionLines - * @return array - */ - private function extractParagraphsFromDescriptionLines(array $descriptionLines): array - { - $paragraphLines = []; - $paragraphIndex = 0; - - foreach ($descriptionLines as $descriptionLine) { - if (! isset($paragraphLines[$paragraphIndex])) { - $paragraphLines[$paragraphIndex] = []; - } - - $descriptionLine = trim($descriptionLine); - if ($descriptionLine === '') { - ++$paragraphIndex; - } else { - $paragraphLines[$paragraphIndex][] = $descriptionLine; - } - } - - return array_map(static fn (array $lines): string => implode(' ', $lines), $paragraphLines); - } - - /** - * @return string[] - */ - private function getLines(string $string): array - { - return explode(PHP_EOL, $string); - } - - /** - * @param string[] $lines - * @return string[] - */ - private function wrapParagraphs(array $lines, int $maximumLineLength): array - { - $wrappedLines = []; - foreach ($lines as $line) { - $wrappedLines[] = wordwrap($line, $maximumLineLength); - } - - return $wrappedLines; - } -} diff --git a/src/ValueObject/DocBlockLines.php b/src/ValueObject/DocBlockLines.php deleted file mode 100644 index aa8b47129..000000000 --- a/src/ValueObject/DocBlockLines.php +++ /dev/null @@ -1,45 +0,0 @@ - $descriptionLines - * @param array $otherLines - */ - public function __construct( - private readonly array $descriptionLines, - private readonly array $otherLines - ) { - } - - /** - * @return array - */ - public function getDescriptionLines(): array - { - return $this->descriptionLines; - } - - /** - * @return array - */ - public function getOtherLines(): array - { - return $this->otherLines; - } - - public function hasListDescriptionLines(): bool - { - foreach ($this->descriptionLines as $descriptionLine) { - if (\str_starts_with($descriptionLine, '-')) { - return true; - } - } - - return false; - } -} diff --git a/src/ValueObjectFactory/DocBlockLinesFactory.php b/src/ValueObjectFactory/DocBlockLinesFactory.php deleted file mode 100644 index 1c6089d2d..000000000 --- a/src/ValueObjectFactory/DocBlockLinesFactory.php +++ /dev/null @@ -1,80 +0,0 @@ -splitToLines($docBlock); - $docBlockLines = array_map( - static function (string $line): string { - $noWhitespace = Strings::trim($line, Strings::TRIM_CHARACTERS); - // Remove asterisks on the left side, plus additional whitespace - return ltrim($noWhitespace, Strings::TRIM_CHARACTERS . '*'); - }, - $docBlockLines - ); - - return $this->createFromLines($docBlockLines); - } - - /** - * @param string[] $docBlockLines - */ - private function createFromLines(array $docBlockLines): DocBlockLines - { - $descriptionLines = []; - $otherLines = []; - - $collectDescriptionLines = true; - - foreach ($docBlockLines as $docBlockLine) { - if (\str_starts_with($docBlockLine, '@') - || \str_starts_with($docBlockLine, '{@')) { - // The line has a special meaning (it's an annotation, or something like {@inheritdoc}) - $collectDescriptionLines = false; - } - - if ($collectDescriptionLines) { - $descriptionLines[] = $docBlockLine; - } else { - $otherLines[] = $docBlockLine; - } - } - - return new DocBlockLines($descriptionLines, $otherLines); - } - - /** - * @return string[] - */ - private function splitToLines(string $string): array - { - return explode(PHP_EOL, $string); - } -} diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/DocBlockLineLengthFixerTest.php b/tests/Fixer/LineLength/DocBlockLineLengthFixer/DocBlockLineLengthFixerTest.php deleted file mode 100644 index 6f92437c4..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/DocBlockLineLengthFixerTest.php +++ /dev/null @@ -1,29 +0,0 @@ -doTestFile($filePath); - } - - public function provideData(): Iterator - { - return self::yieldFiles(__DIR__ . '/Fixture'); - } - - public function provideConfig(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_annotations.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_annotations.php.inc deleted file mode 100644 index 212448b70..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_annotations.php.inc +++ /dev/null @@ -1,26 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_inheritdoc.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_inheritdoc.php.inc deleted file mode 100644 index 5d33c3ab9..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/do_not_reformat_inheritdoc.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/indented_doc_block.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/indented_doc_block.php.inc deleted file mode 100644 index 3d7883ec0..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/indented_doc_block.php.inc +++ /dev/null @@ -1,28 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/inline_doc_block.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/inline_doc_block.php.inc deleted file mode 100644 index 9d192b916..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/inline_doc_block.php.inc +++ /dev/null @@ -1,22 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/make_multi_line_if_needed.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/make_multi_line_if_needed.php.inc deleted file mode 100644 index a448ef771..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/make_multi_line_if_needed.php.inc +++ /dev/null @@ -1,20 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_bullets.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_bullets.php.inc deleted file mode 100644 index bcae9c70e..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_bullets.php.inc +++ /dev/null @@ -1,10 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_single_line_doc_block_with_only_annotations.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_single_line_doc_block_with_only_annotations.php.inc deleted file mode 100644 index a7b19a0ed..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/skip_single_line_doc_block_with_only_annotations.php.inc +++ /dev/null @@ -1,17 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping.php.inc deleted file mode 100644 index c9699006a..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping.php.inc +++ /dev/null @@ -1,22 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_lines.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_lines.php.inc deleted file mode 100644 index 735481d21..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_lines.php.inc +++ /dev/null @@ -1,24 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_paragraphs.php.inc b/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_paragraphs.php.inc deleted file mode 100644 index 848b0082e..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/Fixture/word_wrapping_with_multiple_paragraphs.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/tests/Fixer/LineLength/DocBlockLineLengthFixer/config/configured_rule.php b/tests/Fixer/LineLength/DocBlockLineLengthFixer/config/configured_rule.php deleted file mode 100644 index 869fe5d3d..000000000 --- a/tests/Fixer/LineLength/DocBlockLineLengthFixer/config/configured_rule.php +++ /dev/null @@ -1,12 +0,0 @@ -ruleWithConfiguration(DocBlockLineLengthFixer::class, [ - DocBlockLineLengthFixer::LINE_LENGTH => 40, - ]); -};