Skip to content

Commit

Permalink
Fixes #3. ErickSkrauch/align_multiline_parameters not working correct…
Browse files Browse the repository at this point in the history
…ly with nullable type hints
  • Loading branch information
erickskrauch committed Nov 16, 2023
1 parent 0258fee commit 69382a9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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 #3: `ErickSkrauch/align_multiline_parameters` not working correctly with nullable type hints.

## [1.2.0] - 2023-07-20
### Changed
Expand Down
14 changes: 12 additions & 2 deletions src/FunctionNotation/AlignMultilineParametersFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
28 changes: 26 additions & 2 deletions tests/FunctionNotation/AlignMultilineParametersFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function test(string $a, int $b): void {}
',
];

yield 'function, no defaults' => [
yield 'function, no defaults, no nulls' => [
'<?php
function test(
string $a,
Expand All @@ -64,7 +64,7 @@ function test(
',
];

yield 'function, one has default' => [
yield 'function, one has default, no nulls' => [
'<?php
function test(
string $a,
Expand All @@ -79,6 +79,30 @@ function test(
',
];

yield 'function, no defaults, nullable types' => [
'<?php
function test(
string $a,
?int $b = 0
): void {}
',
'<?php
function test(
string $a,
?int $b = 0
): void {}
',
];

yield 'function, no defaults, nullable types with space' => [
'<?php
function test(
string $a,
? int $b = 0
): void {}
',
];

yield 'function, one has no type' => [
'<?php
function test(
Expand Down

0 comments on commit 69382a9

Please sign in to comment.