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
3 changes: 3 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ jobs:
- name: phpcs version
run: vendor/bin/phpcs --version

- name: Validate code style
run: vendor/bin/phpcs -w -p -s --standard=ruleset.xml --ignore="tests/*not-allowed*" custom-standards/ tests/

- name: Run test suite
run: php tests/runner.php
19 changes: 9 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ COMPOSER_BIN=$(PHP_BIN) composer.phar
# ---------------------------------------------

# make
.DEFAULT_GOAL := install-dev
.DEFAULT_GOAL := install

# make test
test:
$(PHP_BIN) tests/runner.php

# make install
install:
$(COMPOSER_BIN) install

# ---------------------------------------------
# functions
sniff:
$(PHP_BIN) vendor/bin/phpcs -w -p -s --standard=ruleset.xml --ignore="tests/*not-allowed*" custom-standards/ tests/

install-dev:
$(COMPOSER_BIN) install
sniff-fix:
$(PHP_BIN) vendor/bin/phpcbf -w -p -s --standard=ruleset.xml --ignore="tests/*not-allowed*" custom-standards/ tests/

test:
$(PHP_BIN) tests/runner.php
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ interpreted as described in [RFC 2119](http://www.ietf.org/rfc/rfc2119.txt).

To prepare run command:
```bash
make
make install
```

To check code style compliance or to fix what can be autofixed run commands:
```bash
make sniff
make sniff-fix
```

To test ruleset run command:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class FullyQualifiedSniff implements Sniff
*/
public function register()
{
return array(T_DOUBLE_COLON, T_NEW, T_EXTENDS, T_IMPLEMENTS);
return [T_DOUBLE_COLON, T_NEW, T_EXTENDS, T_IMPLEMENTS];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class YodaSniff implements Sniff
*/
public function register()
{
return array(T_IF, T_ELSEIF, T_WHILE);
return [T_IF, T_ELSEIF, T_WHILE];
}

/**
Expand Down Expand Up @@ -87,21 +87,25 @@ private function checkConditionalOrderForArithmeticExpression(

// e.g. if ($foo = true)
// e.g. if ($foo = 'bar')
if (in_array($leftOperandTokenId, $functionAndVariableTokenIds)
if (
in_array($leftOperandTokenId, $functionAndVariableTokenIds)
&& in_array($rightOperandTokenId, $languageTypeTokenIds)
) {
return;
}
// e.g. if (count(..) > $test)
// e.g. if ($foo == $bar)
if (in_array($leftOperandTokenId, $functionAndVariableTokenIds)
if (
in_array($leftOperandTokenId, $functionAndVariableTokenIds)
&& in_array($rightOperandTokenId, $functionAndVariableTokenIds)
) {
return;
}
// e.g. if ('foo' == 'bar')
if (in_array($leftOperandTokenId, $languageTypeTokenIds)
&& in_array($rightOperandTokenId, $languageTypeTokenIds)) {
if (
in_array($leftOperandTokenId, $languageTypeTokenIds)
&& in_array($rightOperandTokenId, $languageTypeTokenIds)
) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ExpectedExceptionMessageSniff implements Sniff
*/
public function register()
{
return array(T_DOC_COMMENT_OPEN_TAG);
return [T_DOC_COMMENT_OPEN_TAG];
}

/**
Expand All @@ -25,8 +25,10 @@ public function process(File $phpcsFile, $stackPtr)
if (!$this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedException')) {
return;
}
if ($this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessage')
|| $this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessageRegExp')) {
if (
$this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessage')
|| $this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessageRegExp')
) {
return;
}

Expand Down
20 changes: 15 additions & 5 deletions custom-standards/Flyeralarm/Sniffs/File/ExceptionMessageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\File;

use PHP_CodeSniffer\Exceptions\RuntimeException;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

Expand All @@ -12,26 +13,26 @@ class ExceptionMessageSniff implements Sniff
*/
public function register()
{
return array(T_CLASS);
return [T_CLASS];
}

/**
* @param File $phpcsFile
* @param int $stackPtr
* @return int|void
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
* @return void
* @throws RuntimeException
*/
public function process(File $phpcsFile, $stackPtr)
{
$className = $phpcsFile->getDeclarationName($stackPtr);

if (strpos($className, 'Exception') === false) {
if (! $this->doesStringEndsWith($className, 'Exception')) {
return;
}

$tokens = $phpcsFile->getTokens();
$ptr = -1;
while($ptr = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $ptr + 1)) {
while ($ptr = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $ptr + 1)) {
if (strpos($tokens[$ptr]['content'], '!') !== false) {
$phpcsFile->addError(
'Exclamationmarks are not allowed in Exceptionmessages',
Expand All @@ -49,4 +50,13 @@ public function process(File $phpcsFile, $stackPtr)
}
}
}

/**
* @alias str_ends_with in PHP8+
*/
public function doesStringEndsWith(string $className, string $suffix): bool
{
$suffixLength = strlen($suffix);
return ($suffixLength === 0 || 0 === substr_compare($className, $suffix, - $suffixLength));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ForbiddenKeywordsSniff implements Sniff
*/
public function register()
{
return array(T_CLASS, T_ABSTRACT, T_TRAIT);
return [T_CLASS, T_ABSTRACT, T_TRAIT];
}

/**
Expand Down
4 changes: 2 additions & 2 deletions custom-standards/Flyeralarm/Sniffs/File/NamespacesSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NamespacesSniff implements Sniff
*/
public function register()
{
return array(T_CLASS, T_ABSTRACT, T_TRAIT, T_INTERFACE);
return [T_CLASS, T_ABSTRACT, T_TRAIT, T_INTERFACE];
}

/**
Expand All @@ -24,7 +24,7 @@ public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$ptr = -1;
while($ptr = $phpcsFile->findNext(T_NS_SEPARATOR, $ptr + 1)) {
while ($ptr = $phpcsFile->findNext(T_NS_SEPARATOR, $ptr + 1)) {
if (strpos($tokens[$ptr + 1]['content'], '_') !== false) {
$phpcsFile->addError(
'Using underscore within namespaces is discouraged',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class NoClassKindSuffixSniff implements Sniff
*/
public function register()
{
return array(T_INTERFACE, T_CLASS, T_TRAIT);
return [T_INTERFACE, T_CLASS, T_TRAIT];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class LowerCamelCaseSniff implements Sniff
*/
public function register()
{
return array(T_OPEN_TAG);
return [T_OPEN_TAG];
}

/**
Expand Down
76 changes: 76 additions & 0 deletions tests/TestRunner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Flyeralarm\Sniffer\Tests;

class TestRunner
{
/**
* @param string $dirPath
*/
public function processDir(string $dirPath)
{
$hasError = $this->recursiveDirProcess($dirPath);
if ($hasError) {
exit(1);
}
exit(0);
}

public function recursiveDirProcess(string $dirPath, bool $hasError = false): bool
{
$dir = opendir($dirPath);
while (($file = readdir($dir)) !== false) {
if (strpos($file, '.') === 0) {
continue;
}
if (is_dir($dirPath . $file)) {
$hasError = $this->recursiveDirProcess($dirPath . $file . '/', $hasError);
continue;
}

$fileContent = file_get_contents($dirPath . $file);
$snifferOutput = shell_exec(
sprintf(
"%s -w -p -s --report-width=120 --standard=%s %s",
escapeshellcmd(__DIR__ . '/../vendor/bin/phpcs'),
escapeshellarg(__DIR__ . '/ruleset.xml'),
escapeshellarg($dirPath . $file)
)
);

// expectedPass
if (preg_match('|//\s@expectedPass$|m', $fileContent)) {
if (preg_match('|^FOUND.*AFFECTING.*LINE|m', $snifferOutput) === 0) {
echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL;
continue;
}

$hasError = true;
echo "ERROR - [" . $dirPath . $file . "]:'
. ' Test was expected to fully pass. Result: " . PHP_EOL . $snifferOutput . PHP_EOL;
continue;
}

// expectedError
preg_match('|//\s@expectedError\s(.*)$|m', $fileContent, $expectedMatch);
if (count($expectedMatch) !== 2) {
echo 'WARNING - [' . $dirPath . $file . ']:'
. ' File must contain exactly one "@expectedError <EXPECTATION MESSAGE>"'
. ' or "@expectedPass" comment' . PHP_EOL;
continue;
}
$expected = $expectedMatch[1];
if (preg_match('/ERROR\s\|\s?[\[\]x\s]*\s' . preg_quote($expected, '/') . '/', $snifferOutput) === 0) {
$hasError = true;
echo 'ERROR - [' . $dirPath . $file . ']:'
. ' Expectation <<' . $expected . '>>'
. ' not found in result: ' . PHP_EOL . $snifferOutput . PHP_EOL;
} else {
echo 'OK - [' . $dirPath . $file . ']' . PHP_EOL;
}
}
return $hasError;
}
}
1 change: 1 addition & 0 deletions tests/rules/array/allowed/ShortArraySyntax.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// @expectedPass

$array = [];
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class LowercaseNamespaceCapitalization
{

}
1 change: 0 additions & 1 deletion tests/rules/doc/allowed/ReturnTypeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class FooTest
{

/**
* @return resource
*/
Expand Down
18 changes: 9 additions & 9 deletions tests/rules/variables/allowed/SuperGlobals.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

// @expectedPass

$GLOBALS=true;
$_SERVER=true;
$_GET=true;
$_POST=true;
$_FILES=true;
$_COOKIE=true;
$_SESSION=true;
$_REQUEST=true;
$_ENV=true;
$GLOBALS = true;
$_SERVER = true;
$_GET = true;
$_POST = true;
$_FILES = true;
$_COOKIE = true;
$_SESSION = true;
$_REQUEST = true;
$_ENV = true;
Loading