Skip to content

Commit

Permalink
Fixes #9. ErickSkrauch/align_multiline_parameters now correctly align…
Browse files Browse the repository at this point in the history
…s variadic arguments
  • Loading branch information
erickskrauch committed Jan 13, 2024
1 parent ec5f8a4 commit 5458830
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Bug #9: `ErickSkrauch/align_multiline_parameters` now correctly aligns variadic arguments.

## [1.2.3] - 2024-01-09
### Fixed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -41,8 +41,10 @@ Forces aligned or not aligned multiline function parameters:
string $string,
- int $index = 0,
- $arg = 'no type',
- ...$variadic,
+ int $index = 0,
+ $arg = 'no type',
+ ...$variadic
): void {}
```

Expand Down
42 changes: 39 additions & 3 deletions src/FunctionNotation/AlignMultilineParametersFixer.php
Expand Up @@ -30,6 +30,7 @@
* typeLength: non-negative-int,
* nameLength: positive-int,
* nameIndex: int,
* isVariadic: bool,
* }
*/
final class AlignMultilineParametersFixer extends AbstractFixer implements ConfigurableFixerInterface, WhitespacesAwareFixerInterface {
Expand Down Expand Up @@ -83,7 +84,8 @@ public function isCandidate(Tokens $tokens): bool {

/**
* Must run after StatementIndentationFixer, MethodArgumentSpaceFixer, CompactNullableTypehintFixer,
* SingleSpaceAroundConstructFixer, TypesSpacesFixer
* SingleSpaceAroundConstructFixer, TypesSpacesFixer, UnaryOperatorSpacesFixer,
* FunctionTypehintSpaceFixer, TypeDeclarationSpacesFixer
*/
public function getPriority(): int {
return -10;
Expand Down Expand Up @@ -131,6 +133,8 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
$longestType = 0;
$longestVariableName = 0;
$hasAtLeastOneTypedArgument = false;
/** @var bool|null $isVariadicArgTypeLong */
$isVariadicArgTypeLong = null;
/** @var list<DeclarationAnalysis> $analysedArguments */
$analysedArguments = [];
foreach ($arguments as $argument) {
Expand All @@ -148,6 +152,10 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
$longestVariableName = $declarationAnalysis['nameLength'];
}

if ($declarationAnalysis['isVariadic']) {
$isVariadicArgTypeLong = $longestType === $declarationAnalysis['typeLength'];
}

$analysedArguments[] = $declarationAnalysis;
}

Expand All @@ -170,9 +178,27 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
}

if ($this->configuration[self::C_VARIABLES] !== null) {
$whitespaceIndex = $argument['nameIndex'] - 1;
if ($argument['isVariadic']) {
$whitespaceIndex = $tokens->getPrevMeaningfulToken($argument['nameIndex']) - 1;
} else {
$whitespaceIndex = $argument['nameIndex'] - 1;
}

if ($this->configuration[self::C_VARIABLES] === true) {
$appendix = str_repeat(' ', $longestType - $argument['typeLength'] + (int)$hasAtLeastOneTypedArgument);
$alignLength = $longestType - $argument['typeLength'] + (int)$hasAtLeastOneTypedArgument;
if ($isVariadicArgTypeLong !== null) {
if ($isVariadicArgTypeLong) {
if (!$argument['isVariadic']) {
$alignLength += 3;
}
} else {
if ($argument['isVariadic']) {
$alignLength -= 3;
}
}
}

$appendix = str_repeat(' ', $alignLength);
if ($argument['typeLength'] > 0) {
$whitespaceToken = $appendix;
} else {
Expand All @@ -197,6 +223,15 @@ private function getDeclarationAnalysis(Tokens $tokens, int $nameIndex, ?TypeAna
$searchIndex = $nameIndex;
$includeNextWhitespace = false;
$typeLength = 0;

$isVariadic = false;
$variadicTokenIndex = $tokens->getPrevMeaningfulToken($searchIndex);
$variadicToken = $tokens[$variadicTokenIndex];
if ($variadicToken->isGivenKind(T_ELLIPSIS)) {
$isVariadic = true;
$searchIndex = $variadicTokenIndex;
}

if ($typeAnalysis !== null) {
$searchIndex = $typeAnalysis->getStartIndex();
$includeNextWhitespace = true;
Expand Down Expand Up @@ -237,6 +272,7 @@ private function getDeclarationAnalysis(Tokens $tokens, int $nameIndex, ?TypeAna
'typeLength' => $typeLength,
'nameLength' => $nameLength,
'nameIndex' => $nameIndex,
'isVariadic' => $isVariadic,
];
}

Expand Down
45 changes: 45 additions & 0 deletions tests/FunctionNotation/AlignMultilineParametersFixerTest.php
Expand Up @@ -216,6 +216,51 @@ function test(
): void {}
',
];

yield 'variadic parameter (short)' => [
'<?php
function test(
Stringable $string,
int ...$params
): void {}
',
'<?php
function test(
Stringable $string,
int ...$params
): void {}
',
];

yield 'variadic parameter (long)' => [
'<?php
function test(
Stringable $string,
DateTimeImmutable ...$params
): void {}
',
'<?php
function test(
Stringable $string,
DateTimeImmutable ...$params
): void {}
',
];

yield 'variadic parameter (untyped)' => [
'<?php
function test(
Stringable $string,
...$params
): void {}
',
'<?php
function test(
Stringable $string,
...$params
): void {}
',
];
}

/**
Expand Down

0 comments on commit 5458830

Please sign in to comment.