diff --git a/CHANGELOG.md b/CHANGELOG.md index 1fd61a6..8d99289 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 #3: `ErickSkrauch/align_multiline_parameters` not working correctly with nullable type hints. ## [1.2.0] - 2023-07-20 ### Changed diff --git a/src/FunctionNotation/AlignMultilineParametersFixer.php b/src/FunctionNotation/AlignMultilineParametersFixer.php index 40a7166..008d3b5 100644 --- a/src/FunctionNotation/AlignMultilineParametersFixer.php +++ b/src/FunctionNotation/AlignMultilineParametersFixer.php @@ -74,7 +74,7 @@ public function isCandidate(Tokens $tokens): bool { } /** - * Must run after StatementIndentationFixer, MethodArgumentSpaceFixer + * Must run after StatementIndentationFixer, MethodArgumentSpaceFixer, CompactNullableTypehintFixer */ public function getPriority(): int { return -10; @@ -183,11 +183,21 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void { } } - private function getFullTypeLength(Tokens $tokens, int $typeIndex): int { + private function getFullTypeLength(Tokens $tokens, ?int $typeIndex): int { /** @var \PhpCsFixer\Tokenizer\Token $typeToken */ $typeToken = $tokens[$typeIndex]; $typeLength = strlen($typeToken->getContent()); + if ($typeToken->isGivenKind(CT::T_NULLABLE_TYPE)) { + $possiblyWhitespace = $tokens[$typeIndex + 1]; + if ($possiblyWhitespace->isWhitespace()) { + $typeLength += strlen($possiblyWhitespace->getContent()); + } + + $realTypeToken = $tokens[$tokens->getNextMeaningfulToken($typeIndex)]; + $typeLength += strlen($realTypeToken->getContent()); + } + /** @var \PhpCsFixer\Tokenizer\Token $possiblyReadonlyToken */ $possiblyReadonlyToken = $tokens[$typeIndex - 2]; if ($possiblyReadonlyToken->isGivenKind($this->parameterModifiers)) { diff --git a/tests/FunctionNotation/AlignMultilineParametersFixerTest.php b/tests/FunctionNotation/AlignMultilineParametersFixerTest.php index 8ed02f5..66bc9d5 100644 --- a/tests/FunctionNotation/AlignMultilineParametersFixerTest.php +++ b/tests/FunctionNotation/AlignMultilineParametersFixerTest.php @@ -49,7 +49,7 @@ function test(string $a, int $b): void {} ', ]; - yield 'function, no defaults' => [ + yield 'function, no defaults, no nulls' => [ ' [ + yield 'function, one has default, no nulls' => [ ' [ + ' [ + ' [ '