Skip to content

Commit 7edfeca

Browse files
authored
chore(phpcs): adds more whitespace rules to coding standards (#22)
https://pixelfederation.atlassian.net/browse/TS2-18278
1 parent cae4be4 commit 7edfeca

File tree

9 files changed

+194
-3
lines changed

9 files changed

+194
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/vendor/
33
composer.lock
44
.php-cs-fixer.cache
5+
6+
.phpunit.cache/

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"php-parallel-lint/php-parallel-lint": "^1.4",
2323
"phpmd/phpmd": "^2.15",
2424
"phpro/grumphp": "^2.17",
25-
"phpstan/phpstan": "^2.1"
25+
"phpstan/phpstan": "^2.1",
26+
"phpunit/phpunit": "^12.5"
2627
},
2728
"autoload": {
2829
"psr-4": {
@@ -57,6 +58,7 @@
5758
"phpcbf8.3": "phpcbf --standard=phpcs.ruleset.83.xml tests/Example",
5859
"phpcbf8.4": "phpcbf --standard=phpcs.ruleset.84.xml tests/Example",
5960
"phpcs8.3": "phpcs --standard=phpcs.ruleset.83.xml tests/Example",
60-
"phpcs8.4": "phpcs --standard=phpcs.ruleset.84.xml tests/Example"
61+
"phpcs8.4": "phpcs --standard=phpcs.ruleset.84.xml tests/Example",
62+
"phpunit": "vendor/bin/phpunit tests/Functional --configuration=phpunit.xml --colors=always"
6163
}
6264
}

grumphp.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@ grumphp:
1717
metadata:
1818
priority: 900
1919
jsonlint: ~
20+
phpunit:
21+
always_execute: true
22+
config_file: phpunit.xml
2023
phpcs:
2124
standard: 'phpcs.xml.dist'
2225
tab_width: 4
2326
whitelist_patterns: [ ]
2427
encoding: utf-8
25-
ignore_patterns: [ ]
28+
ignore_patterns: [
29+
'tests/Functional',
30+
]
2631
sniffs: [ ]
2732
triggered_by: [ 'php' ]
2833
show_sniffs_error_path: true

phpcs.ruleset.83.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@
215215
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
216216
<severity>5</severity>
217217
</rule>
218+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.StartFile">
219+
<severity>5</severity>
220+
</rule>
221+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndFile">
222+
<severity>5</severity>
223+
</rule>
224+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.BeforeOpenBrace">
225+
<severity>5</severity>
226+
</rule>
227+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.BeforeCloseBrace">
228+
<severity>5</severity>
229+
</rule>
218230

219231
<rule ref="Zend.Files.ClosingTag"/>
220232

phpunit.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" colors="true" cacheDirectory=".phpunit.cache">
4+
<php>
5+
<ini name="error_reporting" value="-1"/>
6+
<env name="PATH_PHPCS_RULESET" value="phpcs.ruleset.83.xml"/>
7+
<env name="PATH_PHPCS" value="vendor/bin/phpcs"/>
8+
<env name="PATH_PHPCBF" value="vendor/bin/phpcbf"/>
9+
</php>
10+
<testsuites>
11+
<testsuite name="Project Test Suite">
12+
<directory>tests/Functional</directory>
13+
</testsuite>
14+
</testsuites>
15+
</phpunit>

tests/Functional/PhpcsTestCase.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PixelFederation\CodingStandards\Tests\Functional;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
abstract class PhpcsTestCase extends TestCase
10+
{
11+
public static function assertPhpcbf(
12+
string $fileBefore,
13+
string $fileAfter,
14+
string $message = '',
15+
string $tmpFilename = 'Bar.php',
16+
): void {
17+
$before = __DIR__ . '/' . $fileBefore;
18+
$expected = __DIR__ . '/' . $fileAfter;
19+
20+
self::assertFileExists($before, 'Before file does not exist: ' . $before);
21+
self::assertFileExists($expected, 'After file does not exist: ' . $expected);
22+
23+
$tmpDir = sys_get_temp_dir() . '/cs_bar_' . bin2hex(random_bytes(4));
24+
self::assertTrue(mkdir($tmpDir, 0777, true), $message);
25+
26+
$tmpFile = $tmpDir . '/' . $tmpFilename;
27+
self::assertNotFalse($tmpFile, $message);
28+
file_put_contents($tmpFile, file_get_contents($before));
29+
30+
$cmdCbf = sprintf(
31+
'%s --standard=%s %s 2>&1',
32+
escapeshellarg(self::getPath('PATH_PHPCBF')),
33+
escapeshellarg(self::getPath('PATH_PHPCS_RULESET')),
34+
escapeshellarg($tmpFile),
35+
);
36+
37+
$beforeContent = file_get_contents($before);
38+
file_put_contents($tmpFile, $beforeContent);
39+
40+
exec($cmdCbf, $outputCbf, $exitCodeCbf);
41+
self::assertSame(0, $exitCodeCbf, $message);
42+
43+
$actual = file_get_contents($tmpFile);
44+
self::assertNotSame(
45+
$beforeContent,
46+
$actual,
47+
'Expected PHPCBF to change the file, but contents are identical.' . $message,
48+
);
49+
50+
self::assertSame(
51+
file_get_contents($expected),
52+
$actual,
53+
"PHPCBF output does not match expected 'after' file. " . $message,
54+
);
55+
56+
$cmdCs = sprintf(
57+
'%s --standard=%s %s 2>&1',
58+
escapeshellarg(self::getPath('PATH_PHPCS')),
59+
escapeshellarg(self::getPath('PATH_PHPCS_RULESET')),
60+
escapeshellarg($tmpFile),
61+
);
62+
63+
exec($cmdCs, $outputCs, $exitCodeCs);
64+
65+
self::assertSame(
66+
0,
67+
$exitCodeCs,
68+
sprintf(
69+
"Expected no PHPCS errors after PHPCBF fix, got exit code %s.\nOutput:\n%s %s",
70+
$exitCodeCs,
71+
implode("\n", $outputCs),
72+
$message,
73+
),
74+
);
75+
76+
@unlink($tmpFile);
77+
}
78+
79+
protected static function getPath(string $env): string
80+
{
81+
return __DIR__ . '/../../' . getenv($env);
82+
}
83+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PixelFederation\CodingStandards\Tests\Functional\SuperfluousWhitespace;
6+
7+
final class Bar
8+
{
9+
public readonly int $superNumber;
10+
11+
/**
12+
* Superfluous docblock parameters
13+
*/
14+
public function __construct(
15+
public readonly int $width,
16+
public readonly int $height,
17+
) {
18+
$total = $this->width + $this->height;
19+
20+
$heightDoubled = $this->height * 2;
21+
$this->superNumber = $total + $heightDoubled;
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
4+
declare(strict_types=1);
5+
6+
namespace PixelFederation\CodingStandards\Tests\Functional\SuperfluousWhitespace;
7+
8+
final class Bar
9+
{
10+
11+
public readonly int $superNumber;
12+
13+
14+
/**
15+
* Superfluous docblock parameters
16+
*
17+
* @param int $width
18+
* @param int $height
19+
*/
20+
public function __construct(
21+
public readonly int $width ,
22+
23+
public readonly int $height,
24+
)
25+
{
26+
$total = $this->width + $this->height;
27+
28+
29+
$heightDoubled = $this->height * 2;
30+
$this->superNumber = $total + $heightDoubled;
31+
}
32+
33+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PixelFederation\CodingStandards\Tests\Functional;
6+
7+
final class SuperfluousWhitespaceTest extends PhpcsTestCase
8+
{
9+
public function testPhpcbfFixesWhitespaceAsExpected(): void
10+
{
11+
self::assertPhpcbf(
12+
'SuperfluousWhitespace/Bar.SuperfluousWhitespace.before.php',
13+
'SuperfluousWhitespace/Bar.SuperfluousWhitespace.after.php',
14+
);
15+
}
16+
}

0 commit comments

Comments
 (0)