Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Fixer/LineLength/LineLengthFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
) {
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<Token> $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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -51,18 +46,4 @@ public function fixStartPositionToEndPosition(

$this->tokensInliner->inlineItems($tokens, $blockInfo);
}

/**
* @param Tokens<Token> $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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\Fixture;

final class InlinePrivatePropertyPromotionIssue74
{
public function __construct(
private \stdClass $foo,
) {
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\Fixture;

final class InlinePrivatePropertyPromotionIssue74
{
public function __construct(private \stdClass $foo,) {
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\Fixture;

final class InlinePropertyPromotion
{
public function __construct(
public int $value,
public int $name
) {
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Symplify\CodingStandard\Tests\Fixer\LineLength\LineLengthFixer\Fixture;

final class InlinePropertyPromotion
{
public function __construct(public int $value, public int $name) {
}
}

?>

This file was deleted.

4 changes: 1 addition & 3 deletions tests/Issues/Fixture/line_length_parentheses.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ final class LineLengtParentheses

final class LineLengtParentheses
{
public function __construct(
private DateTimeImmutable $dateTimeImmutable = new DateTimeImmutable
) {}
public function __construct(private DateTimeImmutable $dateTimeImmutable = new DateTimeImmutable) {}
}

?>
Loading