Skip to content

Commit 0ae635f

Browse files
xaloppmmoll
authored andcommitted
improve testing of AlphabeticalUseStatementsSniff
fixes GH-65
1 parent afceadd commit 0ae635f

5 files changed

+48
-23
lines changed

MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

+10-21
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ class AlphabeticalUseStatementsSniff extends UseDeclarationSniff
8686

8787

8888
/**
89-
* Returns an array of tokens this test wants to listen for.
89+
* Processes this test, when one of its tokens is encountered.
90+
*
91+
* @param File $phpcsFile The file being scanned.
92+
* @param int $stackPtr The position of the current token in
93+
* the stack passed in $tokens.
9094
*
91-
* @return array
92-
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
95+
* @return void
9396
*/
94-
public function register()
97+
public function process(File $phpcsFile, $stackPtr)
9598
{
9699
if (in_array($this->order, $this->supportedOrderingMethods, true) === false) {
97100
$error = sprintf(
@@ -101,25 +104,11 @@ public function register()
101104
implode(', ', $this->supportedOrderingMethods)
102105
);
103106

104-
throw new RuntimeException($error);
105-
}
106-
107-
return parent::register();
108-
109-
}//end register()
107+
$phpcsFile->addError($error, $stackPtr, 'InvalidOrder');
110108

109+
return;
110+
}
111111

112-
/**
113-
* Processes this test, when one of its tokens is encountered.
114-
*
115-
* @param File $phpcsFile The file being scanned.
116-
* @param int $stackPtr The position of the current token in
117-
* the stack passed in $tokens.
118-
*
119-
* @return void
120-
*/
121-
public function process(File $phpcsFile, $stackPtr)
122-
{
123112
parent::process($phpcsFile, $stackPtr);
124113

125114
if ($this->currentFile !== $phpcsFile->getFilename()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
// phpcs:set MO4.Formatting.AlphabeticalUseStatements order dictionary-typo
4+
5+
use A1\B;
6+
7+
// reset it
8+
// phpcs:set MO4.Formatting.AlphabeticalUseStatements order dictionary
9+
10+
class Foo
11+
{
12+
13+
}

MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.pass.inc

+21
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,27 @@ use A\B;
1616
use A\B\C;
1717
use A\BD;
1818

19+
// phpcs:set MO4.Formatting.AlphabeticalUseStatements order string
20+
21+
use Doctrine\ORM\Query;
22+
use Doctrine\ORM\QueryBuilder;
23+
use Doctrine\ORM\Query\Expr;
24+
25+
use ExampleSub;
26+
use Examples;
27+
28+
// phpcs:set MO4.Formatting.AlphabeticalUseStatements order string-case-insensitive
29+
30+
use E2xamples;
31+
use E2xampleSub;
32+
33+
// phpcs:set MO4.Formatting.AlphabeticalUseStatements order string-locale
34+
35+
use E3xampleSub;
36+
use E3xamples;
37+
38+
// phpcs:set MO4.Formatting.AlphabeticalUseStatements order dictionary
39+
1940
class Foo {
2041
$bar = 'foo';
2142

MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ protected function getErrorList($testFile='')
8383
];
8484
case 'AlphabeticalUseStatementsUnitTest.fail.5.inc':
8585
return [12 => 1];
86+
case 'AlphabeticalUseStatementsUnitTest.fail.6.inc':
87+
return [5 => 1];
8688
}//end switch
8789

8890
throw new RuntimeException("Testfile {$testFile} in ".__DIR__." is not handled by ".__CLASS__);

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Possible values for order:
136136
use Examples;
137137
```
138138
* `string-locale`: locale based string comparison using [strcoll](http://php.net/strcoll)
139-
* `string-case-insenstive`: binary safe case-insensitive string comparison [strcasecmp](http://php.net/strcasecmp)
139+
* `string-case-insensitive`: binary safe case-insensitive string comparison [strcasecmp](http://php.net/strcasecmp)
140140
```php
141141
use Examples;
142142
use ExampleSub;
@@ -159,7 +159,7 @@ If `phpcs` complains that MO4 is not installed, please check the installed codin
159159

160160
## Dependencies
161161

162-
* [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer)
162+
* [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) version 3.2.0 or later
163163
* [David Joos's Symfony Coding Standard](https://github.com/djoos/Symfony-coding-standard) ruleset for PHP CodeSniffer
164164
* [Composer installer for PHP_CodeSniffer coding standards](https://github.com/DealerDirect/phpcodesniffer-composer-installer)
165165

0 commit comments

Comments
 (0)