Skip to content

Commit e2bdd7e

Browse files
authored
Merge pull request #29 from flyeralarm/IN-1729-sniff
IN-1729 - Add sniff to make and CI to apply code style check on self
2 parents abe042b + 666ddf0 commit e2bdd7e

File tree

17 files changed

+142
-97
lines changed

17 files changed

+142
-97
lines changed

.github/workflows/php.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,8 @@ jobs:
3838
- name: phpcs version
3939
run: vendor/bin/phpcs --version
4040

41+
- name: Validate code style
42+
run: vendor/bin/phpcs -w -p -s --standard=ruleset.xml --ignore="tests/*not-allowed*" custom-standards/ tests/
43+
4144
- name: Run test suite
4245
run: php tests/runner.php

Makefile

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ COMPOSER_BIN=$(PHP_BIN) composer.phar
44
# ---------------------------------------------
55

66
# make
7-
.DEFAULT_GOAL := install-dev
7+
.DEFAULT_GOAL := install
88

9-
# make test
10-
test:
11-
$(PHP_BIN) tests/runner.php
12-
13-
# make install
149
install:
10+
$(COMPOSER_BIN) install
1511

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

19-
install-dev:
20-
$(COMPOSER_BIN) install
15+
sniff-fix:
16+
$(PHP_BIN) vendor/bin/phpcbf -w -p -s --standard=ruleset.xml --ignore="tests/*not-allowed*" custom-standards/ tests/
17+
18+
test:
19+
$(PHP_BIN) tests/runner.php

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ interpreted as described in [RFC 2119](http://www.ietf.org/rfc/rfc2119.txt).
2323

2424
To prepare run command:
2525
```bash
26-
make
26+
make install
27+
```
28+
29+
To check code style compliance or to fix what can be autofixed run commands:
30+
```bash
31+
make sniff
32+
make sniff-fix
2733
```
2834

2935
To test ruleset run command:

custom-standards/Flyeralarm/Sniffs/Classes/FullyQualifiedSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class FullyQualifiedSniff implements Sniff
1313
*/
1414
public function register()
1515
{
16-
return array(T_DOUBLE_COLON, T_NEW, T_EXTENDS, T_IMPLEMENTS);
16+
return [T_DOUBLE_COLON, T_NEW, T_EXTENDS, T_IMPLEMENTS];
1717
}
1818

1919
/**

custom-standards/Flyeralarm/Sniffs/ControlStructures/YodaSniff.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class YodaSniff implements Sniff
1212
*/
1313
public function register()
1414
{
15-
return array(T_IF, T_ELSEIF, T_WHILE);
15+
return [T_IF, T_ELSEIF, T_WHILE];
1616
}
1717

1818
/**
@@ -87,21 +87,25 @@ private function checkConditionalOrderForArithmeticExpression(
8787

8888
// e.g. if ($foo = true)
8989
// e.g. if ($foo = 'bar')
90-
if (in_array($leftOperandTokenId, $functionAndVariableTokenIds)
90+
if (
91+
in_array($leftOperandTokenId, $functionAndVariableTokenIds)
9192
&& in_array($rightOperandTokenId, $languageTypeTokenIds)
9293
) {
9394
return;
9495
}
9596
// e.g. if (count(..) > $test)
9697
// e.g. if ($foo == $bar)
97-
if (in_array($leftOperandTokenId, $functionAndVariableTokenIds)
98+
if (
99+
in_array($leftOperandTokenId, $functionAndVariableTokenIds)
98100
&& in_array($rightOperandTokenId, $functionAndVariableTokenIds)
99101
) {
100102
return;
101103
}
102104
// e.g. if ('foo' == 'bar')
103-
if (in_array($leftOperandTokenId, $languageTypeTokenIds)
104-
&& in_array($rightOperandTokenId, $languageTypeTokenIds)) {
105+
if (
106+
in_array($leftOperandTokenId, $languageTypeTokenIds)
107+
&& in_array($rightOperandTokenId, $languageTypeTokenIds)
108+
) {
105109
return;
106110
}
107111

custom-standards/Flyeralarm/Sniffs/Docblock/ExpectedExceptionMessageSniff.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ExpectedExceptionMessageSniff implements Sniff
1212
*/
1313
public function register()
1414
{
15-
return array(T_DOC_COMMENT_OPEN_TAG);
15+
return [T_DOC_COMMENT_OPEN_TAG];
1616
}
1717

1818
/**
@@ -25,8 +25,10 @@ public function process(File $phpcsFile, $stackPtr)
2525
if (!$this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedException')) {
2626
return;
2727
}
28-
if ($this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessage')
29-
|| $this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessageRegExp')) {
28+
if (
29+
$this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessage')
30+
|| $this->hasAnnotationInDoc($phpcsFile, $stackPtr, '@expectedExceptionMessageRegExp')
31+
) {
3032
return;
3133
}
3234

custom-standards/Flyeralarm/Sniffs/File/ExceptionMessageSniff.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Flyeralarm\CodingGuidelines\Flyeralarm\Sniffs\File;
44

5+
use PHP_CodeSniffer\Exceptions\RuntimeException;
56
use PHP_CodeSniffer\Sniffs\Sniff;
67
use PHP_CodeSniffer\Files\File;
78

@@ -12,26 +13,26 @@ class ExceptionMessageSniff implements Sniff
1213
*/
1314
public function register()
1415
{
15-
return array(T_CLASS);
16+
return [T_CLASS];
1617
}
1718

1819
/**
1920
* @param File $phpcsFile
2021
* @param int $stackPtr
21-
* @return int|void
22-
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException
22+
* @return void
23+
* @throws RuntimeException
2324
*/
2425
public function process(File $phpcsFile, $stackPtr)
2526
{
2627
$className = $phpcsFile->getDeclarationName($stackPtr);
2728

28-
if (strpos($className, 'Exception') === false) {
29+
if (! $this->doesStringEndsWith($className, 'Exception')) {
2930
return;
3031
}
3132

3233
$tokens = $phpcsFile->getTokens();
3334
$ptr = -1;
34-
while($ptr = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $ptr + 1)) {
35+
while ($ptr = $phpcsFile->findNext(T_CONSTANT_ENCAPSED_STRING, $ptr + 1)) {
3536
if (strpos($tokens[$ptr]['content'], '!') !== false) {
3637
$phpcsFile->addError(
3738
'Exclamationmarks are not allowed in Exceptionmessages',
@@ -49,4 +50,13 @@ public function process(File $phpcsFile, $stackPtr)
4950
}
5051
}
5152
}
53+
54+
/**
55+
* @alias str_ends_with in PHP8+
56+
*/
57+
public function doesStringEndsWith(string $className, string $suffix): bool
58+
{
59+
$suffixLength = strlen($suffix);
60+
return ($suffixLength === 0 || 0 === substr_compare($className, $suffix, - $suffixLength));
61+
}
5262
}

custom-standards/Flyeralarm/Sniffs/File/ForbiddenKeywordsSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ForbiddenKeywordsSniff implements Sniff
1212
*/
1313
public function register()
1414
{
15-
return array(T_CLASS, T_ABSTRACT, T_TRAIT);
15+
return [T_CLASS, T_ABSTRACT, T_TRAIT];
1616
}
1717

1818
/**

custom-standards/Flyeralarm/Sniffs/File/NamespacesSniff.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class NamespacesSniff implements Sniff
1212
*/
1313
public function register()
1414
{
15-
return array(T_CLASS, T_ABSTRACT, T_TRAIT, T_INTERFACE);
15+
return [T_CLASS, T_ABSTRACT, T_TRAIT, T_INTERFACE];
1616
}
1717

1818
/**
@@ -24,7 +24,7 @@ public function process(File $phpcsFile, $stackPtr)
2424
{
2525
$tokens = $phpcsFile->getTokens();
2626
$ptr = -1;
27-
while($ptr = $phpcsFile->findNext(T_NS_SEPARATOR, $ptr + 1)) {
27+
while ($ptr = $phpcsFile->findNext(T_NS_SEPARATOR, $ptr + 1)) {
2828
if (strpos($tokens[$ptr + 1]['content'], '_') !== false) {
2929
$phpcsFile->addError(
3030
'Using underscore within namespaces is discouraged',

custom-standards/Flyeralarm/Sniffs/File/NoClassKindSuffixSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class NoClassKindSuffixSniff implements Sniff
1212
*/
1313
public function register()
1414
{
15-
return array(T_INTERFACE, T_CLASS, T_TRAIT);
15+
return [T_INTERFACE, T_CLASS, T_TRAIT];
1616
}
1717

1818
/**

0 commit comments

Comments
 (0)