Skip to content

Commit e4f93c2

Browse files
committed
ErickSkrauch/align_multiline_parameters now supports non-latin types and variable names
1 parent 690a28d commit e4f93c2

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ 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+
### Added
9+
- `ErickSkrauch/align_multiline_parameters` now correctly handle non-latin types and variables names (but you still shouldn't do that).
10+
811
### Fixed
912
- Bug #6: `ErickSkrauch/align_multiline_parameters` not working correctly with unions and intersections.
1013

src/FunctionNotation/AlignMultilineParametersFixer.php

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

7676
/**
77-
* Must run after StatementIndentationFixer, MethodArgumentSpaceFixer, CompactNullableTypehintFixer
77+
* Must run after StatementIndentationFixer, MethodArgumentSpaceFixer, CompactNullableTypehintFixer,
78+
* SingleSpaceAroundConstructFixer
7879
*/
7980
public function getPriority(): int {
8081
return -10;
@@ -132,7 +133,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
132133
}
133134
}
134135

135-
$variableNameLength = strlen($argument->getName());
136+
$variableNameLength = mb_strlen($argument->getName());
136137
if ($variableNameLength > $longestVariableName) {
137138
$longestVariableName = $variableNameLength;
138139
}
@@ -170,7 +171,7 @@ protected function applyFix(SplFileInfo $file, Tokens $tokens): void {
170171
/** @var \PhpCsFixer\Tokenizer\Token $equalToken */
171172
$equalToken = $tokens[$tokens->getNextMeaningfulToken($argument->getNameIndex())];
172173
if ($equalToken->getContent() === '=') {
173-
$nameLen = strlen($argument->getName());
174+
$nameLen = mb_strlen($argument->getName());
174175
$whitespaceIndex = $argument->getNameIndex() + 1;
175176
if ($this->configuration[self::C_DEFAULTS] === true) {
176177
$tokens->ensureWhitespaceAtIndex($whitespaceIndex, 0, str_repeat(' ', $longestVariableName - $nameLen + 1));
@@ -193,7 +194,7 @@ private function getFullTypeLength(Tokens $tokens, int $typeIndex): int {
193194
$typeLength = 0;
194195
$varNameTokenIndex = $tokens->getNextTokenOfKind($typeIndex, [[T_VARIABLE]]);
195196
for ($i = $typeIndex; $i < $varNameTokenIndex - 1; $i++) { // -1 to avoid whitespace between param name and type
196-
$typeLength += strlen($tokens[$i]->getContent());
197+
$typeLength += mb_strlen($tokens[$i]->getContent());
197198
}
198199

199200
$possiblyReadonlyToken = $tokens[$typeIndex - 2];

tests/FunctionNotation/AlignMultilineParametersFixerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,23 @@ public function foo(
196196
) => $int;
197197
',
198198
];
199+
200+
yield 'non-latin type and variable name' => [
201+
'<?php
202+
interface Жыве {}
203+
function test(
204+
string $string = "string",
205+
Жыве $Беларусь = null
206+
): void {}
207+
',
208+
'<?php
209+
interface Жыве {}
210+
function test(
211+
string $string = "string",
212+
Жыве $Беларусь = null
213+
): void {}
214+
',
215+
];
199216
}
200217

201218
/**

0 commit comments

Comments
 (0)