Skip to content

Commit c75fc95

Browse files
committed
Merge branch 'complex_types_support'
2 parents 6b0e22f + 46fcbd7 commit c75fc95

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
### Fixed
9+
- Bug #6: `ErickSkrauch/align_multiline_parameters` not working correctly with unions and intersections.
810

911
## [1.2.1] - 2023-11-16
1012
### Fixed

src/FunctionNotation/AlignMultilineParametersFixer.php

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,33 +183,27 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
183183
}
184184
}
185185

186-
private function getFullTypeLength(Tokens $tokens, ?int $typeIndex): int {
187-
/** @var \PhpCsFixer\Tokenizer\Token $typeToken */
188-
$typeToken = $tokens[$typeIndex];
189-
$typeLength = strlen($typeToken->getContent());
190-
191-
if ($typeToken->isGivenKind(CT::T_NULLABLE_TYPE)) {
192-
$possiblyWhitespace = $tokens[$typeIndex + 1];
193-
if ($possiblyWhitespace->isWhitespace()) {
194-
$typeLength += strlen($possiblyWhitespace->getContent());
195-
}
196-
197-
$realTypeToken = $tokens[$tokens->getNextMeaningfulToken($typeIndex)];
198-
$typeLength += strlen($realTypeToken->getContent());
186+
/**
187+
* TODO: The declaration might be split across multiple lines.
188+
* In such case we need to find the longest line and return it as the full type length
189+
*
190+
* @param int $typeIndex points to the beginning of the type
191+
*/
192+
private function getFullTypeLength(Tokens $tokens, int $typeIndex): int {
193+
$typeLength = 0;
194+
$varNameTokenIndex = $tokens->getNextTokenOfKind($typeIndex, [[T_VARIABLE]]);
195+
for ($i = $typeIndex; $i < $varNameTokenIndex - 1; $i++) { // -1 to avoid whitespace between param name and type
196+
$typeLength += strlen($tokens[$i]->getContent());
199197
}
200198

201-
/** @var \PhpCsFixer\Tokenizer\Token $possiblyReadonlyToken */
202199
$possiblyReadonlyToken = $tokens[$typeIndex - 2];
203200
if ($possiblyReadonlyToken->isGivenKind($this->parameterModifiers)) {
204-
/** @var \PhpCsFixer\Tokenizer\Token $whitespaceToken */
205201
$whitespaceToken = $tokens[$typeIndex - 1];
206202
$typeLength += strlen($possiblyReadonlyToken->getContent() . $whitespaceToken->getContent());
207203
}
208204

209-
/** @var \PhpCsFixer\Tokenizer\Token $possiblyPromotionToken */
210205
$possiblyPromotionToken = $tokens[$typeIndex - 4];
211206
if ($possiblyPromotionToken->isGivenKind($this->parameterModifiers)) {
212-
/** @var \PhpCsFixer\Tokenizer\Token $whitespaceToken */
213207
$whitespaceToken = $tokens[$typeIndex - 3];
214208
$typeLength += strlen($possiblyPromotionToken->getContent() . $whitespaceToken->getContent());
215209
}

tests/FunctionNotation/AlignMultilineParametersFixerTest.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ public function __construct(
344344
}
345345
',
346346
];
347+
yield 'constructor with union types, mixed whitespace' => [
348+
'<?php
349+
class Test {
350+
public function __construct(
351+
string $string = "string",
352+
protected readonly int | string|null $int = 0
353+
) {}
354+
}
355+
',
356+
'<?php
357+
class Test {
358+
public function __construct(
359+
string $string = "string",
360+
protected readonly int | string|null $int = 0
361+
) {}
362+
}
363+
',
364+
];
347365
}
348366

349367
/**
@@ -368,6 +386,61 @@ public function provideFalse81Cases(): iterable {
368386
}
369387
}
370388

389+
/**
390+
* @dataProvider provide82TrueCases
391+
* @requires PHP 8.2
392+
*/
393+
public function test82BothTrue(string $expected, ?string $input = null): void {
394+
$this->fixer->configure([
395+
AlignMultilineParametersFixer::C_VARIABLES => true,
396+
AlignMultilineParametersFixer::C_DEFAULTS => true,
397+
]);
398+
$this->doTest($expected, $input);
399+
}
400+
401+
public function provide82TrueCases(): iterable {
402+
yield 'constructor with union types, mixed whitespace' => [
403+
'<?php
404+
class Test {
405+
public function __construct(
406+
string $string = "string",
407+
protected readonly int | (JsonSerializable& Stringable)|null $int = 0
408+
) {}
409+
}
410+
',
411+
'<?php
412+
class Test {
413+
public function __construct(
414+
string $string = "string",
415+
protected readonly int | (JsonSerializable& Stringable)|null $int = 0
416+
) {}
417+
}
418+
',
419+
];
420+
}
421+
422+
/**
423+
* @dataProvider provideFalse82Cases
424+
* @requires PHP 8.2
425+
*/
426+
public function test82BothFalse(string $expected, ?string $input = null): void {
427+
$this->fixer->configure([
428+
AlignMultilineParametersFixer::C_VARIABLES => false,
429+
AlignMultilineParametersFixer::C_DEFAULTS => false,
430+
]);
431+
$this->doTest($expected, $input);
432+
}
433+
434+
public function provideFalse82Cases(): iterable {
435+
foreach ($this->provide82TrueCases() as $key => $case) {
436+
if (isset($case[1])) {
437+
yield $key => [$case[1], $case[0]];
438+
} else {
439+
yield $key => $case;
440+
}
441+
}
442+
}
443+
371444
protected function createFixer(): AbstractFixer {
372445
return new AlignMultilineParametersFixer();
373446
}

0 commit comments

Comments
 (0)