Skip to content

Commit

Permalink
Bump friendsofphp/php-cs-fixer (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos committed Oct 28, 2020
1 parent ae96519 commit f88cd42
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@

[![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)
[![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)
![Tests](https://img.shields.io/badge/tests-2299-brightgreen.svg)
![Tests](https://img.shields.io/badge/tests-2300-brightgreen.svg)
[![Mutation testing badge](https://badge.stryker-mutator.io/github.com/kubawerlos/php-cs-fixer-custom-fixers/master)](https://stryker-mutator.github.io)
[![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)

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^7.2",
"ext-tokenizer": "*",
"friendsofphp/php-cs-fixer": "^2.16.4 <2.16.5",
"friendsofphp/php-cs-fixer": "^2.16.7",
"symfony/finder": "^3.0 || ^4.0 || ^5.0"
},
"require-dev": {
Expand Down
41 changes: 36 additions & 5 deletions src/Fixer/NoLeadingSlashInGlobalNamespaceFixer.php
Expand Up @@ -50,14 +50,13 @@ public function isRisky(): bool

public function fix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 0; $index < $tokens->count(); $index++) {
$index = 0;
while (++$index < $tokens->count()) {
$index = $this->skipNamespacedCode($tokens, $index);

/** @var Token $token */
$token = $tokens[$index];

if ($token->isGivenKind(T_NAMESPACE)) {
return;
}

if (!$token->isGivenKind(T_NS_SEPARATOR)) {
continue;
}
Expand All @@ -83,4 +82,36 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
}
}
}

private function skipNamespacedCode(Tokens $tokens, int $index): int
{
/** @var Token $token */
$token = $tokens[$index];

if (!$token->isGivenKind(T_NAMESPACE)) {
return $index;
}

/** @var int $nextIndex */
$nextIndex = $tokens->getNextMeaningfulToken($index);

/** @var Token $nextToken */
$nextToken = $tokens[$nextIndex];

if ($nextToken->equals('{')) {
return $nextIndex;
}

/** @var int $nextIndex */
$nextIndex = $tokens->getNextTokenOfKind($index, ['{', ';']);

/** @var Token $nextToken */
$nextToken = $tokens[$nextIndex];

if ($nextToken->equals(';')) {
return $tokens->count() - 1;
}

return $tokens->findBlockEnd(Tokens::BLOCK_TYPE_CURLY_BRACE, $nextIndex);
}
}
2 changes: 1 addition & 1 deletion src/Fixer/NoUselessCommentFixer.php
Expand Up @@ -48,7 +48,7 @@ function getBar() {}
*/
public function getPriority(): int
{
return 6;
return 4;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php
Expand Up @@ -40,7 +40,7 @@ public function getDefinition(): FixerDefinitionInterface
*/
public function getPriority(): int
{
return 6;
return 4;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpdocNoSuperfluousParamFixer.php
Expand Up @@ -45,7 +45,7 @@ function foo($b, $s) {}
*/
public function getPriority(): int
{
return 6;
return 4;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpdocOnlyAllowedAnnotationsFixer.php
Expand Up @@ -73,7 +73,7 @@ public function configure(?array $configuration = null): void
*/
public function getPriority(): int
{
return 6;
return 4;
}

public function isCandidate(Tokens $tokens): bool
Expand Down
18 changes: 16 additions & 2 deletions tests/Fixer/NoLeadingSlashInGlobalNamespaceFixerTest.php
Expand Up @@ -35,6 +35,10 @@ public function testFix(string $expected, ?string $input = null): void

public static function provideFixCases(): iterable
{
yield [
'<?php namespace Foo; $y = new \\Bar();',
];

yield [
'<?php $foo = new Bar();',
'<?php $foo = new \\Bar();',
Expand All @@ -56,8 +60,18 @@ public static function provideFixCases(): iterable
];

yield [
'<?php $x = new Foo(); namespace Bar { $y = new \\Baz(); }',
'<?php $x = new \\Foo(); namespace Bar { $y = new \\Baz(); }',
'<?php
namespace { $x = new Foo(); }
namespace Bar { $y = new \\Baz(); }
namespace { $x = new Foo2(); }
namespace Bar2 { $y = new \\Baz2(); }
',
'<?php
namespace { $x = new \\Foo(); }
namespace Bar { $y = new \\Baz(); }
namespace { $x = new \\Foo2(); }
namespace Bar2 { $y = new \\Baz2(); }
',
];

yield [
Expand Down

0 comments on commit f88cd42

Please sign in to comment.