Skip to content

Commit 69382a9

Browse files
committed
Fixes #3. ErickSkrauch/align_multiline_parameters not working correctly with nullable type hints
1 parent 0258fee commit 69382a9

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
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 #3: `ErickSkrauch/align_multiline_parameters` not working correctly with nullable type hints.
810

911
## [1.2.0] - 2023-07-20
1012
### Changed

src/FunctionNotation/AlignMultilineParametersFixer.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function isCandidate(Tokens $tokens): bool {
7474
}
7575

7676
/**
77-
* Must run after StatementIndentationFixer, MethodArgumentSpaceFixer
77+
* Must run after StatementIndentationFixer, MethodArgumentSpaceFixer, CompactNullableTypehintFixer
7878
*/
7979
public function getPriority(): int {
8080
return -10;
@@ -183,11 +183,21 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
183183
}
184184
}
185185

186-
private function getFullTypeLength(Tokens $tokens, int $typeIndex): int {
186+
private function getFullTypeLength(Tokens $tokens, ?int $typeIndex): int {
187187
/** @var \PhpCsFixer\Tokenizer\Token $typeToken */
188188
$typeToken = $tokens[$typeIndex];
189189
$typeLength = strlen($typeToken->getContent());
190190

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());
199+
}
200+
191201
/** @var \PhpCsFixer\Tokenizer\Token $possiblyReadonlyToken */
192202
$possiblyReadonlyToken = $tokens[$typeIndex - 2];
193203
if ($possiblyReadonlyToken->isGivenKind($this->parameterModifiers)) {

tests/FunctionNotation/AlignMultilineParametersFixerTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function test(string $a, int $b): void {}
4949
',
5050
];
5151

52-
yield 'function, no defaults' => [
52+
yield 'function, no defaults, no nulls' => [
5353
'<?php
5454
function test(
5555
string $a,
@@ -64,7 +64,7 @@ function test(
6464
',
6565
];
6666

67-
yield 'function, one has default' => [
67+
yield 'function, one has default, no nulls' => [
6868
'<?php
6969
function test(
7070
string $a,
@@ -79,6 +79,30 @@ function test(
7979
',
8080
];
8181

82+
yield 'function, no defaults, nullable types' => [
83+
'<?php
84+
function test(
85+
string $a,
86+
?int $b = 0
87+
): void {}
88+
',
89+
'<?php
90+
function test(
91+
string $a,
92+
?int $b = 0
93+
): void {}
94+
',
95+
];
96+
97+
yield 'function, no defaults, nullable types with space' => [
98+
'<?php
99+
function test(
100+
string $a,
101+
? int $b = 0
102+
): void {}
103+
',
104+
];
105+
82106
yield 'function, one has no type' => [
83107
'<?php
84108
function test(

0 commit comments

Comments
 (0)