Skip to content

Commit

Permalink
Merge branch 'hotfix/55' into develop
Browse files Browse the repository at this point in the history
Forward port #55
  • Loading branch information
michalbundyra committed Nov 11, 2019
2 parents d01b569 + dfa6682 commit ea6ddbb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ All notable changes to this project will be documented in this file, in reverse

- [#40](https://github.com/webimpress/coding-standard/pull/40) fixes `PHP\RedundantSemicolon` sniff to remove redundant semicolon after colon and goto label

- [#55](https://github.com/webimpress/coding-standard/pull/55) fixes `Namespaces\AlphabeticallySortedUses` sniff to work with files without namespaces

## 1.0.5 - 2019-10-06

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use const T_NAMESPACE;
use const T_NS_SEPARATOR;
use const T_OPEN_TAG;
use const T_SEMICOLON;
use const T_STRING;
use const T_USE;
Expand All @@ -31,17 +32,26 @@ class AlphabeticallySortedUsesSniff implements Sniff
*/
public function register() : array
{
return [T_NAMESPACE];
return [T_OPEN_TAG, T_NAMESPACE];
}

/**
* @param int $stackPtr
* @return int
*/
public function process(File $phpcsFile, $stackPtr)
{
$uses = $this->getUseStatements($phpcsFile, $stackPtr);
$tokens = $phpcsFile->getTokens();

if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
$namespace = $phpcsFile->findNext(T_NAMESPACE, $stackPtr + 1);
if ($namespace) {
return $namespace;
}
}

$uses = $this->getUseStatements($phpcsFile, $stackPtr);

$lastUse = null;
foreach ($uses as $use) {
if (! $lastUse) {
Expand All @@ -61,7 +71,7 @@ public function process(File $phpcsFile, $stackPtr)
$this->fixAlphabeticalOrder($phpcsFile, $uses);
}

return;
return $stackPtr + 1;
}

// Check empty lines between use statements.
Expand Down Expand Up @@ -130,6 +140,10 @@ public function process(File $phpcsFile, $stackPtr)

$lastUse = $use;
}

return $tokens[$stackPtr]['code'] === T_OPEN_TAG
? $phpcsFile->numTokens + 1
: $stackPtr + 1;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use const D;
use const C;
use B;
use A;
use function Y;
use function X;
use A\TagManager;
use A\Tag;
use A\Tag\Tags;
use C\Response as MyResponse;
use C\Response\HtmlResponse;

class J {
use MyTrait;

public function closure($foo)
{
return function ($x) use ($foo) {
return $x <=> $foo;
};
}

public function anonym()
{
return new class() {
use AnotherTrait;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use A;
use A\Tag;
use A\Tag\Tags;
use A\TagManager;
use B;
use C\Response as MyResponse;
use C\Response\HtmlResponse;

use function X;
use function Y;

use const C;
use const D;

class J {
use MyTrait;

public function closure($foo)
{
return function ($x) use ($foo) {
return $x <=> $foo;
};
}

public function anonym()
{
return new class() {
use AnotherTrait;
};
}
}
4 changes: 4 additions & 0 deletions test/Sniffs/Namespaces/AlphabeticallySortedUsesUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ protected function getErrorList(string $testFile = '') : array
return [
6 => 1,
];
case 'AlphabeticallySortedUsesUnitTest.2.inc':
return [
4 => 1,
];
}

return [
Expand Down

0 comments on commit ea6ddbb

Please sign in to comment.