From f61109c4066843aee1c9ae6a5b299ea7c9af45f8 Mon Sep 17 00:00:00 2001 From: fix-issue-bot Date: Mon, 25 May 2026 13:20:21 +0200 Subject: [PATCH] fix: LineLengthFixer shortens constructors with promoted properties (fixes #74) Move the promoted-property skip from LineLengthTransformer (unconditional) into LineLengthFixer where it is only applied when StandaloneLinePromotedPropertyFixer is registered. Standalone use of LineLengthFixer can now inline short constructors with promoted properties, while the combined-fixer behavior remains unchanged. --- src/Fixer/LineLength/LineLengthFixer.php | 23 +++++++++++++- .../LineLengthTransformer.php | 19 ------------ ...rivate_property_promotion_issue_74.php.inc | 29 ++++++++++++++++++ .../Fixture/inline_property_promotion.php.inc | 30 +++++++++++++++++++ .../Fixture/skip_property_promotion.php.inc | 14 --------- .../Fixture/line_length_parentheses.php.inc | 4 +-- 6 files changed, 82 insertions(+), 37 deletions(-) create mode 100644 tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_private_property_promotion_issue_74.php.inc create mode 100644 tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_property_promotion.php.inc delete mode 100644 tests/Fixer/LineLength/LineLengthFixer/Fixture/skip_property_promotion.php.inc diff --git a/src/Fixer/LineLength/LineLengthFixer.php b/src/Fixer/LineLength/LineLengthFixer.php index 5a20cf46f..0c00a5c37 100644 --- a/src/Fixer/LineLength/LineLengthFixer.php +++ b/src/Fixer/LineLength/LineLengthFixer.php @@ -15,6 +15,7 @@ use Symplify\CodingStandard\Exception\ShouldNotHappenException; use Symplify\CodingStandard\Fixer\AbstractSymplifyFixer; use Symplify\CodingStandard\Fixer\Spacing\StandaloneLineConstructorParamFixer; +use Symplify\CodingStandard\Fixer\Spacing\StandaloneLinePromotedPropertyFixer; use Symplify\CodingStandard\TokenAnalyzer\FunctionCallNameMatcher; use Symplify\CodingStandard\TokenAnalyzer\HeredocAnalyzer; use Symplify\CodingStandard\TokenAnalyzer\Naming\MethodNameResolver; @@ -68,7 +69,8 @@ public function __construct( private readonly FunctionCallNameMatcher $functionCallNameMatcher, private readonly MethodNameResolver $methodNameResolver, private readonly HeredocAnalyzer $heredocAnalyzer, - private readonly ?StandaloneLineConstructorParamFixer $standaloneLineConstructorParamFixer = null + private readonly ?StandaloneLineConstructorParamFixer $standaloneLineConstructorParamFixer = null, + private readonly ?StandaloneLinePromotedPropertyFixer $standaloneLinePromotedPropertyFixer = null ) { } @@ -208,6 +210,11 @@ private function processFunctionOrArray(Tokens $tokens, int $position): void return; } + // StandaloneLinePromotedPropertyFixer enforces standalone lines, do not undo its work + if ($this->standaloneLinePromotedPropertyFixer && $this->hasPromotedProperty($tokens, $blockInfo)) { + return; + } + if ($this->shouldSkip($tokens, $blockInfo)) { return; } @@ -244,4 +251,18 @@ private function shouldSkip(Tokens $tokens, BlockInfo $blockInfo): bool // has comments => dangerous to change: https://github.com/symplify/symplify/issues/973 return (bool) $tokens->findGivenKind(T_COMMENT, $blockInfo->getStart(), $blockInfo->getEnd()); } + + /** + * @param Tokens $tokens + */ + private function hasPromotedProperty(Tokens $tokens, BlockInfo $blockInfo): bool + { + $resultByKind = $tokens->findGivenKind([ + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, + CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, + ], $blockInfo->getStart(), $blockInfo->getEnd()); + + return (bool) array_filter($resultByKind); + } } diff --git a/src/TokenRunner/Transformer/FixerTransformer/LineLengthTransformer.php b/src/TokenRunner/Transformer/FixerTransformer/LineLengthTransformer.php index 8cfb81649..24b1dae39 100644 --- a/src/TokenRunner/Transformer/FixerTransformer/LineLengthTransformer.php +++ b/src/TokenRunner/Transformer/FixerTransformer/LineLengthTransformer.php @@ -4,7 +4,6 @@ namespace Symplify\CodingStandard\TokenRunner\Transformer\FixerTransformer; -use PhpCsFixer\Tokenizer\CT; use PhpCsFixer\Tokenizer\Token; use PhpCsFixer\Tokenizer\Tokens; use Symplify\CodingStandard\TokenRunner\Enum\LineKind; @@ -30,10 +29,6 @@ public function fixStartPositionToEndPosition( bool $breakLongLines, bool $inlineShortLine ): void { - if ($this->hasPromotedProperty($tokens, $blockInfo)) { - return; - } - $firstLineLength = $this->firstLineLengthResolver->resolveFromTokensAndStartPosition($tokens, $blockInfo); if ($firstLineLength > $lineLength && $breakLongLines) { $this->tokensNewliner->breakItems($blockInfo, $tokens, LineKind::CALLS); @@ -51,18 +46,4 @@ public function fixStartPositionToEndPosition( $this->tokensInliner->inlineItems($tokens, $blockInfo); } - - /** - * @param Tokens $tokens - */ - private function hasPromotedProperty(Tokens $tokens, BlockInfo $blockInfo): bool - { - $resultByKind = $tokens->findGivenKind([ - CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PUBLIC, - CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PROTECTED, - CT::T_CONSTRUCTOR_PROPERTY_PROMOTION_PRIVATE, - ], $blockInfo->getStart(), $blockInfo->getEnd()); - - return (bool) array_filter($resultByKind); - } } diff --git a/tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_private_property_promotion_issue_74.php.inc b/tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_private_property_promotion_issue_74.php.inc new file mode 100644 index 000000000..ae89a3a65 --- /dev/null +++ b/tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_private_property_promotion_issue_74.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_property_promotion.php.inc b/tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_property_promotion.php.inc new file mode 100644 index 000000000..5687d3f16 --- /dev/null +++ b/tests/Fixer/LineLength/LineLengthFixer/Fixture/inline_property_promotion.php.inc @@ -0,0 +1,30 @@ + +----- + diff --git a/tests/Fixer/LineLength/LineLengthFixer/Fixture/skip_property_promotion.php.inc b/tests/Fixer/LineLength/LineLengthFixer/Fixture/skip_property_promotion.php.inc deleted file mode 100644 index 4d58395b2..000000000 --- a/tests/Fixer/LineLength/LineLengthFixer/Fixture/skip_property_promotion.php.inc +++ /dev/null @@ -1,14 +0,0 @@ -