Skip to content

Commit d98629e

Browse files
committed
Check coding-style in the repo
1 parent 74617a5 commit d98629e

File tree

13 files changed

+172
-35
lines changed

13 files changed

+172
-35
lines changed

.editorconfig

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
max_line_length = 120
11+
tab_width = 4
12+
13+
[*.md]
14+
trim_trailing_whitespace = false
15+
16+
[*.{yml,yaml}]
17+
indent_size = 2

.github/workflows/format_php.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Format (PHP)
2+
3+
on:
4+
push:
5+
paths:
6+
- '**.php'
7+
- 'composer.*'
8+
- 'phpcs.xml'
9+
- 'IxDFCodingStandard/ruleset.xml'
10+
11+
jobs:
12+
format_php:
13+
name: Format PHP
14+
runs-on: ubuntu-latest
15+
timeout-minutes: 7
16+
steps:
17+
- uses: actions/checkout@v3
18+
with:
19+
ref: ${{ github.head_ref }}
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: 8.2
25+
coverage: none
26+
tools: cs2pr
27+
- name: Get Composer cache directory
28+
id: composer-cache
29+
run: |
30+
echo "composer_dir={$(composer config cache-files-dir)}" >> $GITHUB_OUTPUT
31+
32+
- name: Retrieve Composer‘s cache
33+
uses: actions/cache@v3
34+
with:
35+
path: ${{ steps.composer-cache.outputs.composer_dir }}
36+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
37+
restore-keys: |
38+
${{ runner.os }}-composer-
39+
40+
- name: Install composer dependencies
41+
run: "composer install --no-interaction --no-progress --no-scripts"
42+
43+
- name: Retrieve PHPCS‘s cache
44+
uses: actions/cache@v3
45+
with:
46+
path: .phpcs.cache
47+
key: ${{ runner.os }}-phpcs-${{ hashFiles('phpcs.xml') }}
48+
restore-keys: |
49+
${{ runner.os }}-phpcs-
50+
51+
- name: Detect PHP coding style issues
52+
id: lint_php
53+
run: composer cs:lint
54+
continue-on-error: true
55+
56+
- name: Fix detected PHP coding style issues (if any)
57+
if: ${{ steps.lint_php.outcome == 'failure' }}
58+
id: fix_php
59+
run: composer cs:fix
60+
continue-on-error: true
61+
62+
- name: Commit PHP code-style fixes (if any)
63+
if: ${{ steps.lint_php.outcome == 'failure' }}
64+
uses: EndBug/add-and-commit@v9
65+
with:
66+
message: "#15000 🪄️ Apply coding style fixes to PHP"
67+
author_name: GitHub Actions
68+
author_email: [email protected]
69+
add: '*.php'
70+
pull: '--rebase --autostash'
71+
72+
- name: Lint PHP coding style issues (if previously detected)
73+
if: ${{ steps.lint_php.outcome == 'failure' }}
74+
run: composer cs:check -- --report-full --report-checkstyle=./phpcs-report.xml
75+
76+
- name: Show PHPCS results in on GitHub UI
77+
if: ${{ steps.lint_php.outcome == 'failure' }}
78+
run: cs2pr ./phpcs-report.xml

IxDFCodingStandard/Sniffs/Classes/ForbidMethodDeclarationSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ public function process(File $phpcsFile, $classPointer): void
4343
continue;
4444
}
4545

46-
$phpcsFile->addError(sprintf('Method “%s” is forbidden, use “%s” instead.', $typeAndMethod, $replacement), $classPointer, self::FORBIDDEN_METHOD_DECLARATION);
46+
$phpcsFile->addError(
47+
sprintf('Method “%s” is forbidden, use “%s” instead.', $typeAndMethod, $replacement),
48+
$classPointer,
49+
self::FORBIDDEN_METHOD_DECLARATION
50+
);
4751
}
4852
}
4953
}

IxDFCodingStandard/Sniffs/Files/BemCasedFilenameSniff.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88
/** Checks that all file names are BEM-cased. */
99
final class BemCasedFilenameSniff implements Sniff
1010
{
11+
private const ERROR_TOO_MANY_DELIMITERS = 'TooManyElementModifiers';
12+
private const ERROR_TOO_MANY_MODIFIERS = 'TooManyElementModifiers';
13+
private const ERROR_INVALID_CHARACTERS = 'InvalidCharacters';
14+
1115
private const MODIFIER_DELIMITER = '--';
1216
private const ELEMENT_DELIMITER = '__';
13-
private const BEM_FILE_NAME_PATTERN = '/^(?:(?:(?:__)?[a-z][a-zA-Z0-9]*)(?:(?:--)[\w][a-zA-Z0-9]*)?)+?\.blade\.php$/';
14-
private const BLADE_COMPONENT_FILE_NAME_PATTERN = '/^([a-z]+(\-[a-z]+)+)\.blade\.php$/';
17+
private const BEM_FILE_NAME_PATTERN = '/^(?:(?:__)?[a-z][a-zA-Z0-9]*(?:--[\w][a-zA-Z0-9]*)?)+?\.blade\.php$/';
18+
private const BLADE_COMPONENT_FILE_NAME_PATTERN = '/^([a-z]+(-[a-z]+)+)\.blade\.php$/';
1519

1620
/** @var array<string, bool> */
1721
private array $checkedFiles = [];
@@ -23,9 +27,12 @@ public function register(): array
2327
}
2428

2529
/** @inheritDoc */
26-
public function process(File $phpcsFile, $stackPtr): int
30+
public function process(File $phpcsFile, $stackPtr): int // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
2731
{
2832
$filename = $phpcsFile->getFilename();
33+
if (! str_ends_with($filename, '.blade.php')) {
34+
return 0;
35+
}
2936

3037
$hash = md5($filename);
3138
if (isset($this->checkedFiles[$hash]) || $filename === 'STDIN') {
@@ -40,7 +47,7 @@ public function process(File $phpcsFile, $stackPtr): int
4047
$phpcsFile->addError(
4148
'Filename “%s” has too many element delimiters',
4249
$stackPtr,
43-
'TooManyElementDelimiters',
50+
self::ERROR_TOO_MANY_DELIMITERS,
4451
[$filename]
4552
);
4653
$phpcsFile->recordMetric($stackPtr, 'BEM element delimiters', 'no');
@@ -52,7 +59,7 @@ public function process(File $phpcsFile, $stackPtr): int
5259
$phpcsFile->addError(
5360
'Filename “%s” has to many element modifiers',
5461
$stackPtr,
55-
'TooManyElementModifiers',
62+
self::ERROR_TOO_MANY_MODIFIERS,
5663
[$filename]
5764
);
5865
$phpcsFile->recordMetric($stackPtr, 'BEM element modifiers', 'no');
@@ -64,7 +71,7 @@ public function process(File $phpcsFile, $stackPtr): int
6471
$phpcsFile->recordMetric($stackPtr, 'BEM case filename', 'yes');
6572
} else {
6673
$error = 'Filename “%s” does not match the expected BEM filename convention';
67-
$phpcsFile->addError($error, $stackPtr, 'InvalidCharacters', [$filename]);
74+
$phpcsFile->addError($error, $stackPtr, self::ERROR_INVALID_CHARACTERS, [$filename]);
6875
$phpcsFile->recordMetric($stackPtr, 'BEM case filename', 'no');
6976
}
7077

@@ -79,7 +86,9 @@ private function hasInvalidNumberOfElements(string $filename): bool
7986

8087
private function hasCorrectFileName(string $filename): bool
8188
{
82-
return $this->isBemFilename($filename) || $this->isComponentFileName($filename) || $this->isErrorPage($filename);
89+
return $this->isBemFilename($filename)
90+
|| $this->isComponentFileName($filename)
91+
|| $this->isErrorPage($filename);
8392
}
8493

8594
private function isBemFilename(string $filename): bool

IxDFCodingStandard/Sniffs/Laravel/NonExistingBladeTemplateSniff.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ final class NonExistingBladeTemplateSniff implements Sniff
1010
{
1111
private const INVALID_METHOD_CALL = 'Invalid method call';
1212

13-
private const INCLUDE_BLADE_DIRECTIVE = '/@(include|component|extends)\(\'([^\']++)\'/'; // @include
13+
// @include
14+
private const INCLUDE_BLADE_DIRECTIVE = '/@(include|component|extends)\(\'([^\']++)\'/';
1415

15-
private const CONDITIONAL_INCLUDE_BLADE_DIRECTIVE = '/@(includeIf|includeWhen)\([^,]++,\s*+\'([^\']++)\'/'; // @includeIf
16+
// @includeIf
17+
private const CONDITIONAL_INCLUDE_BLADE_DIRECTIVE = '/@(includeIf|includeWhen)\([^,]++,\s*+\'([^\']++)\'/';
1618

1719
/** @var array<string, bool> */
1820
private array $checkedFiles = [];
@@ -27,7 +29,7 @@ public function register(): array
2729
}
2830

2931
/** @inheritDoc */
30-
public function process(File $phpcsFile, $stackPtr): int // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded
32+
public function process(File $phpcsFile, $stackPtr): int // phpcs:ignore Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
3133
{
3234
$tokens = $phpcsFile->getTokens();
3335

@@ -162,7 +164,7 @@ private function isViewFacade(array $tokens, int $position): bool
162164
}
163165

164166
/** @param array<array<string>> $tokens */
165-
private function getViewFacadeTemplateName(array $tokens, int $position): string
167+
private function getViewFacadeTemplateName(array $tokens, int $position): string // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
166168
{
167169
if (! $this->isViewFacade($tokens, $position)) {
168170
throw new BadMethodCallException(self::INVALID_METHOD_CALL);
@@ -193,7 +195,7 @@ private function isViewFunctionFactory(array $tokens, int $position): bool
193195
}
194196

195197
/** @param array<array<string>> $tokens */
196-
private function getViewFunctionFactoryTemplateName(array $tokens, int $position): string
198+
private function getViewFunctionFactoryTemplateName(array $tokens, int $position): string // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
197199
{
198200
if (! $this->isViewFunctionFactory($tokens, $position)) {
199201
throw new BadMethodCallException(self::INVALID_METHOD_CALL);

IxDFCodingStandard/Sniffs/NamingConventions/CamelCaseRouteNameSniff.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ public function processRouteName(File $phpcsFile, int $stackPtr, array $tokens):
4141
$routeName = $tokens[$routeNameLocation]['content'];
4242

4343
if (!$this->isCamelCase($routeName)) {
44-
$phpcsFile->addError('Route names must be in camel case', $routeNameLocation, self::CODE_NOT_CAMEL_CASE_ROUTE_NAME);
44+
$phpcsFile->addError(
45+
'Route name is not camel cased',
46+
$routeNameLocation,
47+
self::CODE_NOT_CAMEL_CASE_ROUTE_NAME
48+
);
4549
}
4650
}
4751

IxDFCodingStandard/Sniffs/NamingConventions/MeaningfulVariableNameSniff.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ protected function processMemberVar(File $phpcsFile, $stackPtr): void
5656
* @param int|string $stackPtr The position of the double-quoted string.
5757
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
5858
*/
59-
protected function processVariableInString(File $phpcsFile, $stackPtr): void
59+
protected function processVariableInString(File $phpcsFile, $stackPtr): void // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
6060
{
6161
$tokens = $phpcsFile->getTokens();
6262

63-
if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $tokens[$stackPtr]['content'], $matches) !== 0) {
63+
$subject = $tokens[$stackPtr]['content'];
64+
65+
if (preg_match_all('|[^\\\]\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)|', $subject, $matches) !== 0) {
6466
foreach ($matches[1] as $varName) {
6567
// If it’s a php reserved var, then it's ok.
6668
if (isset($this->phpReservedVars[$varName]) === true) {

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ composer require --dev interaction-design-foundation/coding-standard
1717
2. Add composer scripts into your `composer.json`:
1818
```json
1919
"scripts": {
20-
"cs:check": "phpcs",
21-
"cs:fix": "phpcbf"
20+
"cs:check": "phpcs -p -s --colors --report-full --report-summary",
21+
"cs:fix": "phpcbf -p --colors"
2222
}
2323
```
2424

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
}
3232
},
3333
"scripts": {
34-
"cs-check": "phpcs",
35-
"cs-fix": "phpcbf",
34+
"cs:check": "phpcs -p -s --colors --report-full --report-summary --cache=./.cache/phpcs",
35+
"cs:fix": "phpcbf -p --colors --cache=./.cache/phpcs",
3636
"test": "phpunit",
3737
"psalm": "psalm"
3838
}

docs/README.md

Whitespace-only changes.

IxDFCodingStandard/Sniffs/Laravel/ideas.md renamed to docs/ideas.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Ideas for new sniffs
1+
# Ideas for new Laravel sniffs
22

33
1. Validation rule syntax: don’t use user’s input in unique rule (security)
44
1. Validation rule syntax: prefer an array instead `|`

phpcs.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="IxDFCodingStandard (package version)">
3+
<!-- Include all rules from the IxDF Coding Standard -->
4+
<rule ref="./IxDFCodingStandard/ruleset.xml"/>
5+
6+
<rule ref="Generic.Files.LineLength.TooLong">
7+
<severity>1</severity>
8+
<properties>
9+
<property name="lineLimit" value="150"/>
10+
<property name="absoluteLineLimit" value="150"/>
11+
<property name="ignoreComments" value="true"/>
12+
</properties>
13+
</rule>
14+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
15+
<exclude-pattern>./tests*</exclude-pattern>
16+
</rule>
17+
18+
<!-- Paths to check -->
19+
<file>IxDFCodingStandard</file>
20+
<file>tests</file>
21+
</ruleset>

phpunit.xml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@
22
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
44
bootstrap="tests/bootstrap.php"
5-
cacheDirectory=".phpunit.cache"
5+
cacheDirectory=".cache/phpunit/phpunit.cache"
66
executionOrder="depends,defects"
77
requireCoverageMetadata="true"
88
beStrictAboutCoverageMetadata="true"
99
beStrictAboutOutputDuringTests="true"
10-
convertDeprecationsToExceptions="true"
1110
failOnRisky="true"
12-
failOnWarning="true">
13-
<testsuites>
14-
<testsuite name="default">
15-
<directory suffix="Test.php">./tests/</directory>
16-
</testsuite>
17-
</testsuites>
18-
19-
<coverage>
20-
<include>
21-
<directory suffix=".php">IxDFCodingStandard</directory>
22-
</include>
23-
</coverage>
11+
failOnWarning="true"
12+
>
13+
<testsuites>
14+
<testsuite name="default">
15+
<directory suffix="Test.php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
<coverage/>
19+
<source>
20+
<include>
21+
<directory suffix=".php">IxDFCodingStandard</directory>
22+
</include>
23+
</source>
2424
</phpunit>

0 commit comments

Comments
 (0)