Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ script:
- |
if [ `php -r "echo (int) version_compare(PHP_VERSION, '7.0', '>=');"` = "1" ]; then
composer require phpstan/phpstan --dev
vendor/bin/phpstan analyse --level=5 --no-progress -c .phpstan.neon MO4
vendor/bin/phpstan analyse --level=max --no-progress -c .phpstan.neon MO4 tests
fi
- php -d zend_extension=xdebug.so vendor/bin/phpunit --coverage-clover=coverage.xml

Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ To run the unit tests, execute in the repository root:

## Static analysis

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

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

## Code Coverage

Expand Down
2 changes: 2 additions & 0 deletions MO4/Sniffs/Commenting/PropertyCommentSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ protected function processTokenWithinScope(
return;
}

$commentEnd = (int) $commentEnd;

$conditions = $tokens[$commentEnd]['conditions'];
$lastCondition = array_pop($conditions);
if ($lastCondition !== T_CLASS) {
Expand Down
20 changes: 15 additions & 5 deletions MO4/Sniffs/Formatting/AlphabeticalUseStatementsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ public function process(File $phpcsFile, $stackPtr)
}

$currentImportArr = $this->getUseImport($phpcsFile, $stackPtr);
$currentPtr = $currentImportArr['startPtr'];
$currentImport = $currentImportArr['content'];
if ($currentImportArr === false) {
return;
}

$currentPtr = $currentImportArr['startPtr'];
$currentImport = $currentImportArr['content'];

if (($this->lastLine + 1) < $line) {
$this->lastLine = $line;
Expand Down Expand Up @@ -183,7 +187,7 @@ public function process(File $phpcsFile, $stackPtr)
* @param File $phpcsFile PHP CS File
* @param int $stackPtr pointer
*
* @return array
* @return array|false
*/
private function getUseImport(File $phpcsFile, $stackPtr)
{
Expand All @@ -192,12 +196,18 @@ private function getUseImport(File $phpcsFile, $stackPtr)
T_STRING,
);

$start = $phpcsFile->findNext(
$start = $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
($stackPtr + 1),
null,
true
);
// $start is false when "use" is the last token in file...
if ($start === false) {
return false;
}

$start = (int) $start;
$end = $phpcsFile->findNext($importTokens, $start, null, true);
$import = $phpcsFile->getTokensAsString($start, ($end - $start));

Expand Down Expand Up @@ -330,7 +340,7 @@ private function findNewDestination(
}

$prevLine = $tokens[$prevPtr]['line'];
$prevImportArr = $this->getUseImport($phpcsFile, $prevPtr);
$prevImportArr = $this->getUseImport($phpcsFile, (int) $prevPtr);
} while ($prevLine === ($line - 1)
&& ($this->compareString($prevImportArr['content'], $import) > 0)
);
Expand Down
22 changes: 17 additions & 5 deletions MO4/Sniffs/Formatting/UnnecessaryNamespaceUsageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public function process(File $phpcsFile, $stackPtr)
$nsSep = $phpcsFile->findNext($scanTokens, ($stackPtr + 1));

while ($nsSep !== false) {
$nsSep = (int) $nsSep;

$classNameEnd = $phpcsFile->findNext(
$this->classNameTokens,
$nsSep,
Expand Down Expand Up @@ -126,7 +128,8 @@ public function process(File $phpcsFile, $stackPtr)
continue;
}

$next = ($tag + 1);
$next = ($tag + 1);
// PHP Code Sniffer will magically add T_DOC_COMMENT_CLOSE_TAG with empty string content.
$lineEnd = $phpcsFile->findNext(
array(
T_DOC_COMMENT_CLOSE_TAG,
Expand All @@ -138,13 +141,15 @@ public function process(File $phpcsFile, $stackPtr)
$docCommentStringPtr = $phpcsFile->findNext(
array(T_DOC_COMMENT_STRING),
$next,
$lineEnd
(int) $lineEnd
);

if ($docCommentStringPtr === false) {
continue;
}

$docCommentStringPtr = (int) $docCommentStringPtr;

$docLine = $tokens[$docCommentStringPtr]['content'];

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

while ($useTokenPtr !== false) {
$classNameStart = $phpcsFile->findNext(
$classNameStart = (int) $phpcsFile->findNext(
PHP_CodeSniffer_Tokens::$emptyTokens,
($useTokenPtr + 1),
$end,
true
);
$classNameEnd = $phpcsFile->findNext(
$classNameEnd = (int) $phpcsFile->findNext(
$this->classNameTokens,
($classNameStart + 1),
$end,
Expand All @@ -230,7 +235,12 @@ protected function getUseStatements(
$classNameEnd,
$end
);
$aliasNamePtr = $phpcsFile->findPrevious(
// Prevent endless loop when 'use ;' is the last use statement.
if ($useEnd === false) {
break;
}

$aliasNamePtr = $phpcsFile->findPrevious(
PHP_CodeSniffer_Tokens::$emptyTokens,
($useEnd - 1),
null,
Expand Down Expand Up @@ -279,6 +289,8 @@ protected function getNamespace(File $phpcsFile, $start, $end)
return '';
}

$namespaceStart = (int) $namespaceStart;

$namespaceEnd = $phpcsFile->findNext(
$this->classNameTokens,
($namespaceStart + 1),
Expand Down
12 changes: 12 additions & 0 deletions MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.fail.5.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use A1\B;
use A1\B\C;
use A1\BD;

class Foo
{

}

use
15 changes: 8 additions & 7 deletions MO4/Tests/Formatting/AlphabeticalUseStatementsUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
* @link https://github.com/Mayflower/mo4-coding-standard
*/

namespace MO4\Tests\Formatting;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the AlphabeticalUseStatements sniff.
*
Expand All @@ -22,16 +27,10 @@
* @category PHP
* @package PHP_CodeSniffer-MO4
* @author Xaver Loppenstedt <xaver@loppenstedt.de>
* @copyright 2013 Xaver Loppenstedt, some rights reserved.
* @copyright 2013-2017 Xaver Loppenstedt, some rights reserved.
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/Mayflower/mo4-coding-standard
*/

namespace MO4\Tests\Formatting;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class AlphabeticalUseStatementsUnitTest extends AbstractSniffUnitTest
{

Expand Down Expand Up @@ -82,6 +81,8 @@ protected function getErrorList($testFile='')
20 => 1,
21 => 1,
);
case 'AlphabeticalUseStatementsUnitTest.fail.5.inc':
return array(12 => 1);
}//end switch

throw new RuntimeException("Testfile {$testFile} in ".__DIR__." is not handled by ".__CLASS__);
Expand Down
10 changes: 10 additions & 0 deletions MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.fail.4.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use A;
use ;

class MyFixtures
{
}

use
12 changes: 7 additions & 5 deletions MO4/Tests/Formatting/UnnecessaryNamespaceUsageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
* @link https://github.com/Mayflower/mo4-coding-standard
*/

namespace MO4\Tests\Formatting;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the UnnecessaryNamespaceUsageUnitTest sniff.
*
Expand All @@ -28,11 +33,6 @@
* @license http://spdx.org/licenses/MIT MIT License
* @link https://github.com/Mayflower/mo4-coding-standard
*/
namespace MO4\Tests\Formatting;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class UnnecessaryNamespaceUsageUnitTest extends AbstractSniffUnitTest
{

Expand Down Expand Up @@ -106,6 +106,8 @@ protected function getWarningList($testFile='')
23 => 1,
25 => 3,
);
case 'UnnecessaryNamespaceUsageUnitTest.fail.4.inc':
return array();
}//end switch

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