Skip to content

Commit 83d7468

Browse files
authored
Merge pull request #59 from inpsyde/syde-phpcs
Syde PHP Coding Standards
2 parents c1f999e + 657b70d commit 83d7468

18 files changed

+117
-88
lines changed

.github/workflows/php-qa.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
uses: inpsyde/reusable-workflows/.github/workflows/lint-php.yml@main
3939
strategy:
4040
matrix:
41-
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
41+
php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
4242
with:
4343
PHP_VERSION: ${{ matrix.php }}
4444
LINT_ARGS: '-e php --colors --show-deprecated ./src'
@@ -55,5 +55,11 @@ jobs:
5555
strategy:
5656
matrix:
5757
php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
58+
include:
59+
- php: '7.4'
60+
composer: '--ignore-platform-req=php'
61+
- php: '8.0'
62+
composer: '--ignore-platform-req=php'
5863
with:
5964
PHP_VERSION: ${{ matrix.php }}
65+
COMPOSER_ARGS: ${{ matrix.composer || '' }}

.github/workflows/php-unit-tests.yml

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,41 @@ jobs:
2929

3030
strategy:
3131
matrix:
32-
php-versions: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
32+
php: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
33+
container-version: [ '^1.1.0', '^2' ]
3334
dependency-versions: [ 'lowest', 'highest' ]
34-
container-versions: [ '^1.1.0', '^2' ]
35+
include:
36+
- php: '7.4'
37+
composer: '--ignore-platform-req=php'
38+
- php: '8.0'
39+
composer: '--ignore-platform-req=php'
3540

3641
steps:
3742
- name: Checkout
3843
uses: actions/checkout@v4
3944

4045
- name: Use coverage?
41-
if: ${{ (matrix.php-versions == '8.2') && (matrix.dependency-versions == 'highest') && (matrix.container-versions == '^2') }}
46+
if: ${{ (matrix.php == '8.2') && (matrix.dependency-versions == 'highest') && (matrix.container-version == '^2') }}
4247
run: echo "USE_COVERAGE=yes" >> $GITHUB_ENV
4348

4449
- name: Setup PHP
4550
uses: shivammathur/setup-php@v2
4651
with:
47-
php-version: ${{ matrix.php-versions }}
52+
php-version: ${{ matrix.php }}
4853
ini-values: zend.assertions=1, error_reporting=-1, display_errors=On
4954
coverage: ${{ ((env.USE_COVERAGE == 'yes') && 'xdebug') || 'none' }}
5055

5156
- name: Setup dependencies for PSR-11 target version
5257
run: |
5358
composer remove inpsyde/php-coding-standards --dev --no-update
54-
composer require "psr/container:${{ matrix.container-versions }}" --no-update
59+
composer require "psr/container:${{ matrix.container-version }}" --no-update
5560
composer config --no-plugins allow-plugins.roots/wordpress-core-installer false
5661
5762
- name: Install Composer dependencies
5863
uses: ramsey/composer-install@v3
5964
with:
6065
dependency-versions: ${{ matrix.dependency-versions }}
66+
composer-options: ${{ matrix.composer || '' }}
6167

6268
- name: Run unit tests
6369
run: ./vendor/bin/phpunit ${{ ((env.USE_COVERAGE == 'yes') && '--coverage-clover coverage.xml') || '--no-coverage' }}

composer.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@
2727
},
2828
"require-dev": {
2929
"brain/monkey": "^2.6.1",
30-
"inpsyde/php-coding-standards": "^2.0.0",
31-
"roots/wordpress-no-content": "@dev",
30+
"inpsyde/wp-stubs-versions": "dev-latest",
3231
"mikey179/vfsstream": "^v1.6.11",
33-
"phpunit/phpunit": "^9.6.19",
3432
"phpstan/phpstan": "^2.1.1",
33+
"phpstan/phpstan-deprecation-rules": "^2.0.1",
3534
"phpstan/phpstan-mockery": "^2.0.0",
3635
"phpstan/phpstan-phpunit": "^2.0.4",
36+
"phpunit/phpunit": "^9.6.19",
3737
"swissspidy/phpstan-no-private": "^v1.0.0",
38-
"phpstan/phpstan-deprecation-rules": "^2.0.1",
39-
"inpsyde/wp-stubs-versions": "dev-latest"
38+
"syde/phpcs": "^1.0.0"
4039
},
4140
"extra": {
4241
"branch-alias": {
@@ -72,6 +71,7 @@
7271
"composer/*": true,
7372
"inpsyde/*": true,
7473
"dealerdirect/phpcodesniffer-composer-installer": true
75-
}
74+
},
75+
"sort-packages": true
7676
}
7777
}

phpcs.xml.dist

+30-21
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,47 @@
11
<?xml version="1.0"?>
2-
<ruleset>
2+
<ruleset
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd"
5+
name="Modularity"
6+
>
7+
<!-- Report warnings, but return zero exit code. -->
8+
<config name="ignore_warnings_on_exit" value="1" />
9+
10+
<!-- Check for cross-version support for PHP 7.4 and higher. -->
11+
<config name="testVersion" value="7.4-" />
12+
13+
<config name="php_version" value="70400" />
14+
315
<file>./src</file>
416
<file>./tests</file>
517
<exclude-pattern>./tests/unit/stubs.php</exclude-pattern>
618

7-
<arg value="sp"/>
8-
<arg name="colors"/>
9-
<config name="testVersion" value="7.4-"/>
10-
<config name="ignore_warnings_on_exit" value="1"/>
19+
<!-- Use colors, and show sniff error codes and progress. -->
20+
<arg name="colors" />
21+
<arg value="sp" />
22+
23+
<rule ref="Syde-Extra">
24+
<exclude name="SlevomatCodingStandard.Functions.RequireTrailingCommaInCall.MissingTrailingComma" />
25+
</rule>
1126

12-
<rule ref="Inpsyde">
13-
<exclude name="WordPress.PHP.DevelopmentFunctions.error_log_trigger_error" />
27+
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
28+
<exclude-pattern>/tests/boot.php</exclude-pattern>
1429
</rule>
1530

16-
<rule ref="Inpsyde.CodeQuality.Psr4">
31+
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
1732
<properties>
18-
<property
19-
name="psr4"
20-
type="array"
21-
value="
22-
Inpsyde\Modularity=>src,
23-
Inpsyde\Modularity\Tests=>tests/src,
24-
Inpsyde\Modularity\Tests\Unit=>tests/unit"
25-
/>
33+
<property name="rootNamespaces" type="array">
34+
<element key="src" value="Inpsyde\Modularity" />
35+
<element key="tests/src" value="Inpsyde\Modularity\Tests" />
36+
<element key="tests/unit" value="Inpsyde\Modularity\Tests\Unit" />
37+
</property>
2638
</properties>
2739
</rule>
2840

29-
<rule ref="Inpsyde.CodeQuality.FunctionLength">
41+
<rule ref="Squiz.PHP.Eval">
3042
<exclude-pattern>*/tests/*</exclude-pattern>
3143
</rule>
32-
<rule ref="Inpsyde.CodeQuality.ForbiddenPublicProperty">
44+
<rule ref="Syde.Functions.FunctionLength.TooLong">
3345
<exclude-pattern>*/tests/*</exclude-pattern>
3446
</rule>
3547
<rule ref="WordPress.PHP.DevelopmentFunctions">
@@ -41,7 +53,4 @@
4153
<rule ref="WordPress.Security.EscapeOutput">
4254
<exclude-pattern>*/tests/*</exclude-pattern>
4355
</rule>
44-
<rule ref="Squiz.PHP.Eval">
45-
<exclude-pattern>*/tests/*</exclude-pattern>
46-
</rule>
4756
</ruleset>

phpunit.xml.dist

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
backupGlobals="false"
1212
stopOnFailure="false">
1313

14+
<php>
15+
<ini name="error_reporting" value="E_ALL"/>
16+
</php>
17+
1418
<coverage processUncoveredFiles="true">
1519
<include>
1620
<directory suffix=".php">./src</directory>

src/Container/ServiceExtensions.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,17 @@ protected function resolveById(string $id, $service, Container $container)
9494
*
9595
* @return mixed
9696
*
97-
* phpcs:disable Generic.Metrics.CyclomaticComplexity
98-
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration
97+
* phpcs:disable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
98+
* phpcs:disable Syde.Functions.ReturnTypeDeclaration.NoReturnType
9999
*/
100100
protected function resolveByType(
101101
string $className,
102102
object $service,
103103
Container $container,
104104
array $extendedClasses = []
105105
) {
106-
// phpcs:enable Generic.Metrics.CyclomaticComplexity
107-
// phpcs:enable Inpsyde.CodeQuality.ReturnTypeDeclaration
106+
// phpcs:enable SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
107+
// phpcs:enable Syde.Functions.ReturnTypeDeclaration.NoReturnType
108108

109109
$extendedClasses[] = $className;
110110

src/Package.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -270,12 +270,9 @@ public function addModule(Module $module): Package
270270
/**
271271
* @param Package $package
272272
* @return bool
273-
*
274-
* phpcs:disable Inpsyde.CodeQuality.FunctionLength
275273
*/
276274
public function connect(Package $package): bool
277275
{
278-
// phpcs:enable Inpsyde.CodeQuality.FunctionLength
279276
try {
280277
if ($package === $this) {
281278
return false;
@@ -815,6 +812,7 @@ private function deprecatedArgument(string $message, string $function, string $v
815812

816813
if (apply_filters('deprecated_argument_trigger_error', true)) {
817814
do_action('wp_trigger_error_run', $function, $message, \E_USER_DEPRECATED);
815+
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
818816
trigger_error(esc_html($message), \E_USER_DEPRECATED);
819817
}
820818
}

src/Properties/LibraryProperties.php

+32-12
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,13 @@ class LibraryProperties extends BaseProperties
4040
*
4141
* @return LibraryProperties
4242
*
43-
* phpcs:disable Generic.Metrics.CyclomaticComplexity
43+
* phpcs:disable SlevomatCodingStandard.Complexity
4444
*/
4545
public static function new(string $composerJsonFile, ?string $baseUrl = null): LibraryProperties
4646
{
47-
if (!\is_file($composerJsonFile) || !\is_readable($composerJsonFile)) {
48-
throw new \Exception(
49-
esc_html("File {$composerJsonFile} does not exist or is not readable."),
50-
);
51-
}
47+
// phpcs:enable SlevomatCodingStandard.Complexity
5248

53-
$content = (string) file_get_contents($composerJsonFile);
54-
/** @var ComposerData $composerJsonData */
55-
$composerJsonData = json_decode($content, true);
49+
$composerJsonData = self::readComposerJsonData($composerJsonFile);
5650

5751
$properties = Properties::DEFAULT_PROPERTIES;
5852
$properties[self::PROP_DESCRIPTION] = $composerJsonData['description'] ?? '';
@@ -156,14 +150,12 @@ protected static function buildNames(array $composerJsonData): array
156150
* @param string $key
157151
*
158152
* @return string
159-
*
160-
* phpcs:disable Generic.Metrics.CyclomaticComplexity
161153
*/
162154
protected static function extractPhpVersion(
163155
array $composerData,
164156
string $key = 'require'
165157
): string {
166-
// phpcs:enable Generic.Metrics.CyclomaticComplexity
158+
167159
$nextKey = ($key === 'require')
168160
? 'require-dev'
169161
: null;
@@ -233,4 +225,32 @@ protected static function parseVersion(string $version): string
233225

234226
return '';
235227
}
228+
229+
/**
230+
* @param string $composerJsonFile
231+
*
232+
* @return ComposerData
233+
* @throws \Exception
234+
*/
235+
private static function readComposerJsonData(string $composerJsonFile): array
236+
{
237+
if (!\is_file($composerJsonFile) || !\is_readable($composerJsonFile)) {
238+
throw new \Exception(
239+
esc_html("File {$composerJsonFile} does not exist or is not readable."),
240+
);
241+
}
242+
243+
$content = (string) file_get_contents($composerJsonFile);
244+
245+
/** @var ComposerData $composerJsonData */
246+
$composerJsonData = json_decode($content, true);
247+
248+
if (json_last_error() !== JSON_ERROR_NONE) {
249+
throw new \Exception(
250+
esc_html("Error reading file {$composerJsonFile}: " . json_last_error_msg()),
251+
);
252+
}
253+
254+
return $composerJsonData;
255+
}
236256
}

tests/boot.php

-10
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
declare(strict_types=1);
44

5-
// phpcs:disable PSR1
6-
75
$testsDir = str_replace('\\', '/', __DIR__);
86
$libDir = dirname($testsDir);
97
$vendorDir = "{$libDir}/vendor";
@@ -17,19 +15,11 @@
1715
putenv('LIB_DIR=' . $libDir);
1816
putenv('VENDOR_DIR=' . $vendorDir);
1917

20-
error_reporting(E_ALL); // phpcs:ignore
21-
2218
require_once "{$libDir}/vendor/antecedent/patchwork/Patchwork.php";
2319

2420
if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
2521
define('PHPUNIT_COMPOSER_INSTALL', $autoload);
2622
require_once $autoload;
2723
}
2824

29-
if (!defined('ABSPATH')) {
30-
define('ABSPATH', "{$vendorDir}/roots/wordpress-no-content/");
31-
}
32-
33-
require_once "{$vendorDir}/roots/wordpress-no-content/wp-includes/class-wp-error.php";
34-
3525
unset($testsDir, $libDir, $vendorDir, $autoload);

tests/src/TestCase.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,12 @@ protected function stubServices(string ...$ids): array
130130
* @param string ...$ids
131131
*
132132
* @return ContainerInterface
133-
*
134-
* phpcs:disable Inpsyde.CodeQuality.NestingLevel
135133
*/
136134
protected function stubContainer(string ...$ids): ContainerInterface
137135
{
138-
// phpcs:enable Inpsyde.CodeQuality.NestingLevel
139136
return new class ($this->stubServices(...$ids)) implements ContainerInterface {
140137
/** @var array<string, callable> */
141-
private array $services; // phpcs:ignore
138+
private array $services;
142139

143140
/** @param array<string, callable> $services */
144141
public function __construct(array $services)

tests/unit/Container/ContainerConfiguratorTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,9 @@ static function () use (&$called): \B {
416416
/**
417417
* @test
418418
* @runInSeparateProcess
419-
*
420-
* phpcs:disable Inpsyde.CodeQuality.NestingLevel
421419
*/
422420
public function testExtensionByTypeNested(): void
423421
{
424-
// phpcs:enable Inpsyde.CodeQuality.NestingLevel
425-
426422
$logs = [];
427423
/**
428424
* @template T

tests/unit/Container/ReadOnlyContainerTest.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Inpsyde\Modularity\Container\ServiceExtensions;
99
use Inpsyde\Modularity\Tests\TestCase;
1010
use Psr\Container\ContainerInterface;
11+
use Psr\Container\NotFoundExceptionInterface;
1112

1213
/**
1314
* @phpstan-import-type Service from \Inpsyde\Modularity\Module\ServiceModule
@@ -31,7 +32,7 @@ public function testBasic(): void
3132
*/
3233
public function testGetUnknown(): void
3334
{
34-
static::expectException(\Exception::class);
35+
static::expectException(NotFoundExceptionInterface::class);
3536

3637
$testee = $this->factoryContainer();
3738
$testee->get('unknown');
@@ -128,12 +129,9 @@ public function has(string $id): bool
128129

129130
/**
130131
* @test
131-
*
132-
* phpcs:disable Inpsyde.CodeQuality.NestingLevel
133132
*/
134133
public function testFactoriesAndServices(): void
135134
{
136-
// phpcs:enable Inpsyde.CodeQuality.NestingLevel
137135
$expectedServiceKey = 'service';
138136
$expectedFactoryKey = 'factory';
139137
$services = [
@@ -185,7 +183,7 @@ public function testServiceExtensionsBackwardCompatibility(): void
185183
};
186184

187185
$extension = static function (object $thing): object {
188-
/** @var object{count:integer}&\stdClass $thing */
186+
/** @var object{count:int}&\stdClass $thing */
189187
$thing->count++;
190188

191189
return $thing;

0 commit comments

Comments
 (0)