Skip to content

Commit 4bad93a

Browse files
authored
NoLeadingSlashInGlobalNamespaceFixer - fix for type hints (#437)
1 parent 0982f02 commit 4bad93a

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
[![CI Status](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/workflows/CI/badge.svg?branch=master&event=push)](https://github.com/kubawerlos/php-cs-fixer-custom-fixers/actions)
1010
[![Code coverage](https://img.shields.io/coveralls/github/kubawerlos/php-cs-fixer-custom-fixers/master.svg)](https://coveralls.io/github/kubawerlos/php-cs-fixer-custom-fixers?branch=master)
11-
![Tests](https://img.shields.io/badge/tests-2304-brightgreen.svg)
11+
![Tests](https://img.shields.io/badge/tests-2307-brightgreen.svg)
1212
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
1313
[![Psalm type coverage](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers/coverage.svg)](https://shepherd.dev/github/kubawerlos/php-cs-fixer-custom-fixers)
1414

src/Fixer/NoLeadingSlashInGlobalNamespaceFixer.php

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PhpCsFixer\FixerDefinition\CodeSample;
1717
use PhpCsFixer\FixerDefinition\FixerDefinition;
1818
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
19+
use PhpCsFixer\Tokenizer\CT;
1920
use PhpCsFixer\Tokenizer\Token;
2021
use PhpCsFixer\Tokenizer\Tokens;
2122

@@ -54,33 +55,47 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
5455
while (++$index < $tokens->count()) {
5556
$index = $this->skipNamespacedCode($tokens, $index);
5657

57-
/** @var Token $token */
58-
$token = $tokens[$index];
59-
60-
if (!$token->isGivenKind(T_NS_SEPARATOR)) {
58+
if (!$this->isToRemove($tokens, $index)) {
6159
continue;
6260
}
6361

64-
/** @var int $prevIndex */
65-
$prevIndex = $tokens->getPrevMeaningfulToken($index);
62+
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
63+
}
64+
}
6665

67-
/** @var Token $prevToken */
68-
$prevToken = $tokens[$prevIndex];
66+
private function isToRemove(Tokens $tokens, int $index): bool
67+
{
68+
/** @var Token $token */
69+
$token = $tokens[$index];
6970

70-
if ($prevToken->isGivenKind(T_STRING)) {
71-
continue;
72-
}
71+
if (!$token->isGivenKind(T_NS_SEPARATOR)) {
72+
return false;
73+
}
7374

74-
/** @var int $nextIndex */
75-
$nextIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_COMMENT], [T_DOC_COMMENT], [T_NS_SEPARATOR], [T_STRING], [T_WHITESPACE]]);
75+
/** @var int $prevIndex */
76+
$prevIndex = $tokens->getPrevMeaningfulToken($index);
7677

77-
/** @var Token $nextToken */
78-
$nextToken = $tokens[$nextIndex];
78+
/** @var Token $prevToken */
79+
$prevToken = $tokens[$prevIndex];
7980

80-
if ($prevToken->isGivenKind(T_NEW) || $nextToken->isGivenKind(T_DOUBLE_COLON)) {
81-
$tokens->clearTokenAndMergeSurroundingWhitespace($index);
82-
}
81+
if ($prevToken->isGivenKind(T_STRING)) {
82+
return false;
8383
}
84+
if ($prevToken->isGivenKind([T_NEW, CT::T_NULLABLE_TYPE, CT::T_TYPE_COLON])) {
85+
return true;
86+
}
87+
88+
/** @var int $nextIndex */
89+
$nextIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[T_COMMENT], [T_DOC_COMMENT], [T_NS_SEPARATOR], [T_STRING], [T_WHITESPACE]]);
90+
91+
/** @var Token $nextToken */
92+
$nextToken = $tokens[$nextIndex];
93+
94+
if ($nextToken->isGivenKind(T_DOUBLE_COLON)) {
95+
return true;
96+
}
97+
98+
return $prevToken->equalsAny(['(', ',']) && $nextToken->isGivenKind(T_VARIABLE);
8499
}
85100

86101
private function skipNamespacedCode(Tokens $tokens, int $index): int

tests/Fixer/NoLeadingSlashInGlobalNamespaceFixerTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ public function testFix(string $expected, ?string $input = null): void
3535

3636
public static function provideFixCases(): iterable
3737
{
38-
yield [
39-
'<?php namespace Foo; $y = new \\Bar();',
40-
];
38+
yield ['<?php namespace Foo; $y = new \\Bar();'];
39+
yield ['<?php \foo(\bar(), \baz());'];
4140

4241
yield [
4342
'<?php $foo = new Bar();',
@@ -59,6 +58,16 @@ public static function provideFixCases(): iterable
5958
'<?php $foo = \\Bar\\Baz::NAME;',
6059
];
6160

61+
yield [
62+
'<?php function f(Bar $bar, Baz $baz, ?Qux $qux) {};',
63+
'<?php function f(\\Bar $bar, \\Baz $baz, ?\\Qux $qux) {};',
64+
];
65+
66+
yield [
67+
'<?php function f(): Bar {};',
68+
'<?php function f(): \\Bar {};',
69+
];
70+
6271
yield [
6372
'<?php
6473
namespace { $x = new Foo(); }

0 commit comments

Comments
 (0)