From f88cd42b045faf25c551f6aa9f5e63a794aaf3d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= <9282069+kubawerlos@users.noreply.github.com> Date: Wed, 28 Oct 2020 22:58:50 +0100 Subject: [PATCH] Bump friendsofphp/php-cs-fixer (#405) --- README.md | 2 +- composer.json | 2 +- .../NoLeadingSlashInGlobalNamespaceFixer.php | 41 ++++++++++++++++--- src/Fixer/NoUselessCommentFixer.php | 2 +- .../PhpdocNoIncorrectVarAnnotationFixer.php | 2 +- src/Fixer/PhpdocNoSuperfluousParamFixer.php | 2 +- .../PhpdocOnlyAllowedAnnotationsFixer.php | 2 +- ...LeadingSlashInGlobalNamespaceFixerTest.php | 18 +++++++- 8 files changed, 58 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index cc4678a1..4ba83568 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/composer.json b/composer.json index a10c1d30..e724d0c1 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Fixer/NoLeadingSlashInGlobalNamespaceFixer.php b/src/Fixer/NoLeadingSlashInGlobalNamespaceFixer.php index 8c23aa4a..dff82519 100644 --- a/src/Fixer/NoLeadingSlashInGlobalNamespaceFixer.php +++ b/src/Fixer/NoLeadingSlashInGlobalNamespaceFixer.php @@ -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; } @@ -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); + } } diff --git a/src/Fixer/NoUselessCommentFixer.php b/src/Fixer/NoUselessCommentFixer.php index 0df9cf74..9c36920f 100644 --- a/src/Fixer/NoUselessCommentFixer.php +++ b/src/Fixer/NoUselessCommentFixer.php @@ -48,7 +48,7 @@ function getBar() {} */ public function getPriority(): int { - return 6; + return 4; } public function isCandidate(Tokens $tokens): bool diff --git a/src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php b/src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php index 3ed189c2..49f24ef8 100644 --- a/src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php +++ b/src/Fixer/PhpdocNoIncorrectVarAnnotationFixer.php @@ -40,7 +40,7 @@ public function getDefinition(): FixerDefinitionInterface */ public function getPriority(): int { - return 6; + return 4; } public function isCandidate(Tokens $tokens): bool diff --git a/src/Fixer/PhpdocNoSuperfluousParamFixer.php b/src/Fixer/PhpdocNoSuperfluousParamFixer.php index 8ed2c9c3..5cdcaa0d 100644 --- a/src/Fixer/PhpdocNoSuperfluousParamFixer.php +++ b/src/Fixer/PhpdocNoSuperfluousParamFixer.php @@ -45,7 +45,7 @@ function foo($b, $s) {} */ public function getPriority(): int { - return 6; + return 4; } public function isCandidate(Tokens $tokens): bool diff --git a/src/Fixer/PhpdocOnlyAllowedAnnotationsFixer.php b/src/Fixer/PhpdocOnlyAllowedAnnotationsFixer.php index ac2e9726..3cd864ed 100644 --- a/src/Fixer/PhpdocOnlyAllowedAnnotationsFixer.php +++ b/src/Fixer/PhpdocOnlyAllowedAnnotationsFixer.php @@ -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 diff --git a/tests/Fixer/NoLeadingSlashInGlobalNamespaceFixerTest.php b/tests/Fixer/NoLeadingSlashInGlobalNamespaceFixerTest.php index b74ff7f5..43bee2fe 100644 --- a/tests/Fixer/NoLeadingSlashInGlobalNamespaceFixerTest.php +++ b/tests/Fixer/NoLeadingSlashInGlobalNamespaceFixerTest.php @@ -35,6 +35,10 @@ public function testFix(string $expected, ?string $input = null): void public static function provideFixCases(): iterable { + yield [ + '