Skip to content

Commit 448f1a3

Browse files
authored
Merge pull request #204 from antecedent/feature/enforce-code-style-psr12
Introduce PHP_CodeSniffer ruleset based on PSR12
2 parents e291173 + c456ebc commit 448f1a3

8 files changed

Lines changed: 167 additions & 2 deletions

File tree

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/.gitattributes export-ignore
22
/.gitignore export-ignore
3+
/.phpcs.xml.dist export-ignore
34
/box.json export-ignore
45
/.github export-ignore
56
/tests export-ignore

.github/workflows/qa.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,33 @@ jobs:
3535
uses: docker://rhysd/actionlint:latest
3636
with:
3737
args: -color
38+
39+
phpcs:
40+
name: 'PHPCS'
41+
runs-on: ubuntu-latest
42+
43+
steps:
44+
- name: Checkout code
45+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
46+
with:
47+
persist-credentials: false
48+
49+
- name: Install PHP
50+
uses: shivammathur/setup-php@accd6127cb78bee3e8082180cb391013d204ef9f # 2.37.0
51+
with:
52+
php-version: 'latest'
53+
coverage: none
54+
tools: cs2pr
55+
56+
# Install dependencies and handle caching in one go.
57+
# @link https://github.com/marketplace/actions/install-composer-dependencies
58+
- name: Install Composer dependencies
59+
uses: "ramsey/composer-install@65e4f84970763564f46a70b8a54b90d033b3bdda" # 4.0.0
60+
61+
# Check the code-style consistency of the PHP files.
62+
- name: Check PHP code style
63+
continue-on-error: true
64+
run: composer checkcs -- --report-full --report-checkstyle=./phpcs-report.xml
65+
66+
- name: Show PHPCS results in PR
67+
run: cs2pr ./phpcs-report.xml

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ tests/cache
33
vendor/
44
composer.phar
55
.DS_Store
6+
.phpcs.xml
7+
phpcs.xml
68

79
# Ignore PhpStorm project related files.
810
.idea

.phpcs.xml.dist

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
name="Patchwork"
4+
xsi:noNamespaceSchemaLocation="./vendor/squizlabs/php_codesniffer/phpcs.xsd">
5+
6+
<description>Patchwork rules for PHP_CodeSniffer</description>
7+
8+
<!--
9+
#############################################################################
10+
COMMAND LINE ARGUMENTS
11+
https://github.com/PHPCSStandards/PHP_CodeSniffer/wiki/Annotated-Ruleset
12+
#############################################################################
13+
-->
14+
15+
<!-- Scan all files. -->
16+
<file>.</file>
17+
18+
<!-- Third party files and build files don't need to comply with these coding standards. -->
19+
<exclude-pattern>*/vendor/*</exclude-pattern>
20+
21+
<!-- Test fixtures don't need to comply with these coding standards
22+
as we may be specifically testing a variation in how syntax can be written.
23+
-->
24+
<exclude-pattern>*/tests/includes/*</exclude-pattern>
25+
26+
<!-- Only check PHP files.
27+
Note: at this time PHPCS can not scan .phpt files.
28+
There is a feature request open to change this.
29+
https://github.com/PHPCSStandards/PHP_CodeSniffer/issues/1363
30+
-->
31+
<arg name="extensions" value="php"/>
32+
33+
<!-- Show progress, show the error codes for each message (source). -->
34+
<arg value="ps"/>
35+
36+
<!-- Strip the filepaths down to the relevant bit. -->
37+
<arg name="basepath" value="./"/>
38+
39+
<!-- Check up to 8 files simultaneously. -->
40+
<arg name="parallel" value="8"/>
41+
42+
43+
<!--
44+
#############################################################################
45+
CHECK FOR PHP CROSS-VERSION COMPATIBILITY
46+
#############################################################################
47+
-->
48+
49+
<config name="testVersion" value="7.1-"/>
50+
<rule ref="PHPCompatibility"/>
51+
52+
53+
<!--
54+
#############################################################################
55+
FOLLOW PSR12 for CODING STYLE
56+
#############################################################################
57+
-->
58+
59+
<rule ref="PSR12">
60+
<!-- Re-organising the files should be a separate consideration and needs discussion
61+
about whether or not to adopt PSR-4 or another convention.
62+
-->
63+
<exclude name="PSR1.Files.SideEffects"/>
64+
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
65+
</rule>
66+
67+
68+
<!--
69+
#############################################################################
70+
SNIFF SPECIFIC CONFIGURATION
71+
#############################################################################
72+
-->
73+
74+
<!-- Allow for slightly longer line length than PSR12 (120) allows for. -->
75+
<rule ref="Generic.Files.LineLength">
76+
<properties>
77+
<property name="lineLimit" value="140" />
78+
</properties>
79+
</rule>
80+
81+
82+
<!--
83+
#############################################################################
84+
SELECTIVE EXCLUSIONS
85+
Exclude specific files for specific sniffs.
86+
#############################################################################
87+
-->
88+
89+
<!-- Renaming these methods from snake_case to camelCaps would be a BC-break. -->
90+
<rule ref="PSR1.Methods.CamelCapsMethodName.NotCamelCaps">
91+
<exclude-pattern>/src/CodeManipulation/Stream\.php$</exclude-pattern>
92+
</rule>
93+
94+
<!-- Renaming these constants from camelCaps to ALL_CAPPS would be a BC-break. -->
95+
<rule ref="Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase">
96+
<exclude-pattern>/src/CodeManipulation/Actions/RedefinitionOfNew\.php$</exclude-pattern>
97+
</rule>
98+
99+
</ruleset>

composer.json

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,40 @@
1010
"email": "ignas.rudaitis@gmail.com"
1111
}
1212
],
13-
"minimum-stability": "stable",
1413
"require": {
1514
"php": ">=7.1.0"
1615
},
1716
"require-dev": {
18-
"phpunit/phpunit": ">=4"
17+
"phpcompatibility/php-compatibility": "^10.0.0@alpha",
18+
"phpunit/phpunit": ">=4",
19+
"squizlabs/php_codesniffer": "^3.13.5 || ^4.0"
1920
},
21+
"minimum-stability": "alpha",
22+
"prefer-stable": true,
2023
"config": {
24+
"allow-plugins": {
25+
"dealerdirect/phpcodesniffer-composer-installer": true
26+
},
2127
"lock": false
28+
},
29+
"scripts": {
30+
"checkcs": [
31+
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcs"
32+
],
33+
"fixcs": [
34+
"@php ./vendor/squizlabs/php_codesniffer/bin/phpcbf"
35+
],
36+
"test": [
37+
"@php ./vendor/phpunit/phpunit/phpunit --no-coverage"
38+
],
39+
"coverage": [
40+
"@php ./vendor/phpunit/phpunit/phpunit"
41+
]
42+
},
43+
"scripts-descriptions": {
44+
"checkcs": "Check the PHP files for code style violations and best practices.",
45+
"fixcs": "Auto-fix code style violations in the PHP files.",
46+
"test": "Run the unit tests without code coverage.",
47+
"coverage": "Run the unit tests with code coverage."
2248
}
2349
}

src/CallRerouting.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ function connectOnHHVM($function, Handle $handle)
360360
} elseif (is_object($obj)) {
361361
$calledClass = get_class($obj);
362362
}
363+
// phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection -- Verified okay.
363364
$frame = count(debug_backtrace(0)) - 1;
364365
$result = null;
365366
$done = dispatch($class, $calledClass, $method, $frame, $result, $args);

src/CodeManipulation/Actions/Namespaces.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function getNamespaceAt(Source $s, $pos)
5252

5353
function collectNamespaceBoundaries(Source $s)
5454
{
55+
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass -- Using $this is okay. Source::cache() binds the closure.
5556
return $s->cache([], function () {
5657
if (!$this->has(T_NAMESPACE)) {
5758
return ['' => [[0, INF]]];
@@ -71,6 +72,7 @@ function collectNamespaceBoundaries(Source $s)
7172
}
7273
return $result;
7374
});
75+
// phpcs:enable
7476
}
7577

7678
function collectUseDeclarations(Source $s, $begin)
@@ -84,6 +86,8 @@ function collectUseDeclarations(Source $s, $begin)
8486
}
8587
}
8688
}
89+
90+
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewClosure.ThisFoundOutsideClass -- Using $this is okay. Source::cache() binds the closure.
8791
return $s->cache([$begin], function ($begin) {
8892
$result = ['class' => [], 'function' => [], 'const' => []];
8993
# only tokens that are siblings bracket-wise are considered,
@@ -98,6 +102,7 @@ function collectUseDeclarations(Source $s, $begin)
98102
}
99103
return $result;
100104
});
105+
// phpcs:enable
101106
}
102107

103108
function parseUseDeclaration(Source $s, $pos, array &$aliases, $prefix = '', $type = 'class')

src/CodeManipulation/Actions/RedefinitionOfNew.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
const STATIC_INSTANTIATION_REPLACEMENT = '\Patchwork\CallRerouting\getInstantiator(\'%s\', %s)->instantiate(%s)';
1818
const DYNAMIC_INSTANTIATION_REPLACEMENT = '\Patchwork\CallRerouting\getInstantiator(%s, %s)->instantiate(%s)';
19+
// phpcs:ignore Generic.Files.LineLength.TooLong
1920
const CALLED_CLASS = '((__CLASS__ && __FUNCTION__ !== (__NAMESPACE__ ? __NAMESPACE__ . "\\\\{closure}" : "\\\\{closure}")) ? \get_called_class() : null)';
2021

2122
const spliceAllInstantiations = 'Patchwork\CodeManipulation\Actions\RedefinitionOfNew\spliceAllInstantiations';

0 commit comments

Comments
 (0)