Skip to content

Commit

Permalink
UseInAlphabeticalOrder: fix for inline use statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas Votruba committed Dec 24, 2014
1 parent 6404788 commit 2e27379
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,30 +74,6 @@ public function process(PHP_CodeSniffer_File $file, $position)
}


/**
* @return int|NULL
*/
private function getUseStatementIncorrectOrderPosition(array $uses)
{
foreach ($uses as $scope => $used) {
$defined = $sorted = array_keys($used);

natcasesort($sorted);
$sorted = array_values($sorted);
if ($sorted === $defined) {
continue;
}

foreach ($defined as $i => $name) {
if ($name !== $sorted[$i]) {
return $used[$name];
}
}
}
return NULL;
}


/**
* @return array
*/
Expand All @@ -121,8 +97,11 @@ private function findAllUseStatements()
$scope = key($token['conditions']);
}

$content = $this->replaceBackSlashesBySlashes($content);
$uses[$scope][$content] = $index;
if ($this->isUseForNamespaceOrTrait($next)) {
$content = $this->replaceBackSlashesBySlashes($content);
$uses[$scope][$content] = $index;
}

$next = $this->file->findNext(T_USE, $end);
if ( ! $next) {
break;
Expand All @@ -132,6 +111,30 @@ private function findAllUseStatements()
}


/**
* @return int|NULL
*/
private function getUseStatementIncorrectOrderPosition(array $uses)
{
foreach ($uses as $scope => $used) {
$defined = $sorted = array_keys($used);

natcasesort($sorted);
$sorted = array_values($sorted);
if ($sorted === $defined) {
continue;
}

foreach ($defined as $i => $name) {
if ($name !== $sorted[$i]) {
return $used[$name];
}
}
}
return NULL;
}


/**
* @param string $content
* @return string
Expand All @@ -141,4 +144,18 @@ private function replaceBackSlashesBySlashes($content)
return str_replace('\\', '/', $content);
}


/**
* @param int $position
* @return bool
*/
private function isUseForNamespaceOrTrait($position)
{
$firstLetter = $this->file->getTokens()[$position + 2]['content'];
if ($firstLetter === '(') { // use ($var)
return FALSE;
}
return TRUE;
}

}
9 changes: 9 additions & 0 deletions tests/Sniffs/Namespaces/UseInAlphabeticalOrder/correct3.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ class Presenter
use B;
use D;


public function getSome()
{
$values = [1, 2];
array_walk($values, function ($var) use ($ovar) {
return '';
});
}

}

0 comments on commit 2e27379

Please sign in to comment.