Skip to content

Commit f60bda1

Browse files
xaloppmmoll
authored andcommitted
phpstan: set max inspection level, fixes #56
1 parent e3ec10a commit f60bda1

9 files changed

+74
-25
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ script:
1818
- |
1919
if [ `php -r "echo (int) version_compare(PHP_VERSION, '7.0', '>=');"` = "1" ]; then
2020
composer require phpstan/phpstan --dev
21-
vendor/bin/phpstan analyse --level=5 --no-progress -c .phpstan.neon MO4
21+
vendor/bin/phpstan analyse --level=max --no-progress -c .phpstan.neon MO4 tests
2222
fi
2323
- php -d zend_extension=xdebug.so vendor/bin/phpunit --coverage-clover=coverage.xml
2424

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ To run the unit tests, execute in the repository root:
3939

4040
## Static analysis
4141

42-
We do recommend to use [PHPStan](https://github.com/phpstan/phpstan) for static analysis, with level 5.
42+
We do recommend to use [PHPStan](https://github.com/phpstan/phpstan) for static analysis, with maximum inspection level.
4343
Please refer to the [PHPStan](https://github.com/phpstan/phpstan#installation) documentation for
4444
installation instructions.
4545

46-
phpstan analyse --level=5 -c .phpstan.neon MO4/
46+
phpstan analyse --level=max -c .phpstan.neon MO4/ tests/
4747

4848
## Code Coverage
4949

MO4/Sniffs/Commenting/PropertyCommentSniff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ protected function processTokenWithinScope(
132132
return;
133133
}
134134

135+
$commentEnd = (int) $commentEnd;
136+
135137
$conditions = $tokens[$commentEnd]['conditions'];
136138
$lastCondition = array_pop($conditions);
137139
if ($lastCondition !== T_CLASS) {

MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ public function process(File $phpcsFile, $stackPtr)
139139
}
140140

141141
$currentImportArr = $this->getUseImport($phpcsFile, $stackPtr);
142-
$currentPtr = $currentImportArr['startPtr'];
143-
$currentImport = $currentImportArr['content'];
142+
if ($currentImportArr === false) {
143+
return;
144+
}
145+
146+
$currentPtr = $currentImportArr['startPtr'];
147+
$currentImport = $currentImportArr['content'];
144148

145149
if (($this->lastLine + 1) < $line) {
146150
$this->lastLine = $line;
@@ -183,7 +187,7 @@ public function process(File $phpcsFile, $stackPtr)
183187
* @param File $phpcsFile PHP CS File
184188
* @param int $stackPtr pointer
185189
*
186-
* @return array
190+
* @return array|false
187191
*/
188192
private function getUseImport(File $phpcsFile, $stackPtr)
189193
{
@@ -192,12 +196,18 @@ private function getUseImport(File $phpcsFile, $stackPtr)
192196
T_STRING,
193197
);
194198

195-
$start = $phpcsFile->findNext(
199+
$start = $phpcsFile->findNext(
196200
PHP_CodeSniffer_Tokens::$emptyTokens,
197201
($stackPtr + 1),
198202
null,
199203
true
200204
);
205+
// $start is false when "use" is the last token in file...
206+
if ($start === false) {
207+
return false;
208+
}
209+
210+
$start = (int) $start;
201211
$end = $phpcsFile->findNext($importTokens, $start, null, true);
202212
$import = $phpcsFile->getTokensAsString($start, ($end - $start));
203213

@@ -330,7 +340,7 @@ private function findNewDestination(
330340
}
331341

332342
$prevLine = $tokens[$prevPtr]['line'];
333-
$prevImportArr = $this->getUseImport($phpcsFile, $prevPtr);
343+
$prevImportArr = $this->getUseImport($phpcsFile, (int) $prevPtr);
334344
} while ($prevLine === ($line - 1)
335345
&& ($this->compareString($prevImportArr['content'], $import) > 0)
336346
);

MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ public function process(File $phpcsFile, $stackPtr)
9292
$nsSep = $phpcsFile->findNext($scanTokens, ($stackPtr + 1));
9393

9494
while ($nsSep !== false) {
95+
$nsSep = (int) $nsSep;
96+
9597
$classNameEnd = $phpcsFile->findNext(
9698
$this->classNameTokens,
9799
$nsSep,
@@ -126,7 +128,8 @@ public function process(File $phpcsFile, $stackPtr)
126128
continue;
127129
}
128130

129-
$next = ($tag + 1);
131+
$next = ($tag + 1);
132+
// PHP Code Sniffer will magically add T_DOC_COMMENT_CLOSE_TAG with empty string content.
130133
$lineEnd = $phpcsFile->findNext(
131134
array(
132135
T_DOC_COMMENT_CLOSE_TAG,
@@ -138,13 +141,15 @@ public function process(File $phpcsFile, $stackPtr)
138141
$docCommentStringPtr = $phpcsFile->findNext(
139142
array(T_DOC_COMMENT_STRING),
140143
$next,
141-
$lineEnd
144+
(int) $lineEnd
142145
);
143146

144147
if ($docCommentStringPtr === false) {
145148
continue;
146149
}
147150

151+
$docCommentStringPtr = (int) $docCommentStringPtr;
152+
148153
$docLine = $tokens[$docCommentStringPtr]['content'];
149154

150155
$docLineTokens = preg_split(
@@ -210,13 +215,13 @@ protected function getUseStatements(
210215
$useTokenPtr = $phpcsFile->findNext(T_USE, $i, $end);
211216

212217
while ($useTokenPtr !== false) {
213-
$classNameStart = $phpcsFile->findNext(
218+
$classNameStart = (int) $phpcsFile->findNext(
214219
PHP_CodeSniffer_Tokens::$emptyTokens,
215220
($useTokenPtr + 1),
216221
$end,
217222
true
218223
);
219-
$classNameEnd = $phpcsFile->findNext(
224+
$classNameEnd = (int) $phpcsFile->findNext(
220225
$this->classNameTokens,
221226
($classNameStart + 1),
222227
$end,
@@ -230,7 +235,12 @@ protected function getUseStatements(
230235
$classNameEnd,
231236
$end
232237
);
233-
$aliasNamePtr = $phpcsFile->findPrevious(
238+
// Prevent endless loop when 'use ;' is the last use statement.
239+
if ($useEnd === false) {
240+
break;
241+
}
242+
243+
$aliasNamePtr = $phpcsFile->findPrevious(
234244
PHP_CodeSniffer_Tokens::$emptyTokens,
235245
($useEnd - 1),
236246
null,
@@ -279,6 +289,8 @@ protected function getNamespace(File $phpcsFile, $start, $end)
279289
return '';
280290
}
281291

292+
$namespaceStart = (int) $namespaceStart;
293+
282294
$namespaceEnd = $phpcsFile->findNext(
283295
$this->classNameTokens,
284296
($namespaceStart + 1),
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use A1\B;
4+
use A1\B\C;
5+
use A1\BD;
6+
7+
class Foo
8+
{
9+
10+
}
11+
12+
use

MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
* @link https://github.com/Mayflower/mo4-coding-standard
1414
*/
1515

16+
namespace MO4\Tests\Formatting;
17+
18+
use PHP_CodeSniffer\Exceptions\RuntimeException;
19+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
20+
1621
/**
1722
* Unit test class for the AlphabeticalUseStatements sniff.
1823
*
@@ -22,16 +27,10 @@
2227
* @category PHP
2328
* @package PHP_CodeSniffer-MO4
2429
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
25-
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
30+
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
2631
* @license http://spdx.org/licenses/MIT MIT License
2732
* @link https://github.com/Mayflower/mo4-coding-standard
2833
*/
29-
30-
namespace MO4\Tests\Formatting;
31-
32-
use PHP_CodeSniffer\Exceptions\RuntimeException;
33-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
34-
3534
class AlphabeticalUseStatementsUnitTest extends AbstractSniffUnitTest
3635
{
3736

@@ -82,6 +81,8 @@ protected function getErrorList($testFile='')
8281
20 => 1,
8382
21 => 1,
8483
);
84+
case 'AlphabeticalUseStatementsUnitTest.fail.5.inc':
85+
return array(12 => 1);
8586
}//end switch
8687

8788
throw new RuntimeException("Testfile {$testFile} in ".__DIR__." is not handled by ".__CLASS__);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use A;
4+
use ;
5+
6+
class MyFixtures
7+
{
8+
}
9+
10+
use

MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
* @link https://github.com/Mayflower/mo4-coding-standard
1414
*/
1515

16+
namespace MO4\Tests\Formatting;
17+
18+
use PHP_CodeSniffer\Exceptions\RuntimeException;
19+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
20+
1621
/**
1722
* Unit test class for the UnnecessaryNamespaceUsageUnitTest sniff.
1823
*
@@ -28,11 +33,6 @@
2833
* @license http://spdx.org/licenses/MIT MIT License
2934
* @link https://github.com/Mayflower/mo4-coding-standard
3035
*/
31-
namespace MO4\Tests\Formatting;
32-
33-
use PHP_CodeSniffer\Exceptions\RuntimeException;
34-
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
35-
3636
class UnnecessaryNamespaceUsageUnitTest extends AbstractSniffUnitTest
3737
{
3838

@@ -106,6 +106,8 @@ protected function getWarningList($testFile='')
106106
23 => 1,
107107
25 => 3,
108108
);
109+
case 'UnnecessaryNamespaceUsageUnitTest.fail.4.inc':
110+
return array();
109111
}//end switch
110112

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

0 commit comments

Comments
 (0)