Skip to content

Commit 86c7508

Browse files
xellioRichard Prillwitz
andauthored
Cleanup (add more rules, clean codebase, add tests) (#14)
* - added sniff for abstract class naming convention; - added sniff for interface naming convention; - added basic tests (consistent to existing ones); * - code styling; * - code styling; * - enable worflow; * - added some debugging output for fixing/checking the github workflow; * - fix expected result; * - revert workflow changes; * - added return type hint; * - check only class on AbstractClassNameSniff; - optimized reading of the class name; * cleanup; * debugging testing output; * removed test debugging output; * - activate DisallowMultipleAssignment (only one assignment per line); - activate AssignmentInCondition rule; - replace custom AbstractClassNameSniff by existing AbstractClassNamePrefix; - replace custom InterfaceNameSniff by existing InterfaceNameSuffix; - cleanup; * - removed testing debug; - update expected test output; * - check for trait naming; * - removed testing output; * added more cs rules; * - removed testing output; * - error instead of warning; * - removed debug output; Co-authored-by: Richard Prillwitz <[email protected]>
1 parent bd35bfb commit 86c7508

16 files changed

+216
-113
lines changed

Proton/Sniffs/Architecture/ForbiddenNamespacedFunctionsSniff.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
class ForbiddenNamespacedFunctionsSniff extends ForbiddenFunctionsSniff
1111
{
12+
/**
13+
* @inheritDoc
14+
*/
1215
protected function addError($phpcsFile, $stackPtr, $function, $pattern = null): void
1316
{
1417
$fqfn = NamespaceHelper::resolveName($phpcsFile, $function, 'function', $stackPtr);

Proton/Sniffs/Naming/AbstractClassNameSniff.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

Proton/Sniffs/Naming/InterfaceNameSniff.php

Lines changed: 0 additions & 44 deletions
This file was deleted.

Proton/Sniffs/Spacing/ArrowFunctionSpacingSniff.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,17 @@
99

1010
class ArrowFunctionSpacingSniff implements Sniff
1111
{
12+
/**
13+
* @inheritDoc
14+
*/
1215
public function register(): array
1316
{
1417
return [T_FN];
1518
}
1619

20+
/**
21+
* @inheritDoc
22+
*/
1723
public function process(File $phpcsFile, $stackPtr)
1824
{
1925
$tokens = $phpcsFile->getTokens();
@@ -22,7 +28,7 @@ public function process(File $phpcsFile, $stackPtr)
2228
$phpcsFile->addError(
2329
'The fn arrow function token should always be followed by a space.',
2430
$stackPtr,
25-
'Found'
31+
'Found',
2632
);
2733
}
2834
}

Proton/ruleset.xml

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@
2626
<rule ref="Generic.CodeAnalysis.UnconditionalIfStatement"/>
2727
<rule ref="Generic.CodeAnalysis.EmptyPHPStatement"/>
2828
<rule ref="Generic.CodeAnalysis.UnnecessaryFinalModifier"/>
29+
<rule ref="Generic.CodeAnalysis.AssignmentInCondition"/>
2930
<rule ref="Generic.Classes.DuplicateClassName"/>
3031
<rule ref="Generic.WhiteSpace.IncrementDecrementSpacing"/>
3132
<rule ref="Generic.WhiteSpace.SpreadOperatorSpacingAfter"/>
3233
<rule ref="Generic.VersionControl.GitMergeConflict"/>
3334
<rule ref="Generic.Files.OneClassPerFile"/>
3435
<rule ref="Generic.Files.OneInterfacePerFile"/>
36+
<rule ref="Generic.NamingConventions.InterfaceNameSuffix"/>
37+
<rule ref="Generic.NamingConventions.AbstractClassNamePrefix"/>
38+
<rule ref="Generic.NamingConventions.TraitNameSuffix"/>
3539

3640
<!-- Naming conventions -->
3741
<rule ref="Squiz.NamingConventions.ValidVariableName.NotCamelCaps"/>
@@ -94,11 +98,12 @@
9498

9599
<rule ref="Generic.Metrics.CyclomaticComplexity">
96100
<properties>
97-
<property name="complexity" value="15"/>
101+
<property name="complexity" value="10"/>
98102
<property name="absoluteComplexity" value="20"/>
99103
</properties>
100104
</rule>
101105

106+
<rule ref="Squiz.PHP.DisallowMultipleAssignments"/>
102107
<rule ref="Squiz.PHP.NonExecutableCode"/>
103108
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
104109
<rule ref="Squiz.Operators.ValidLogicalOperators"/> <!-- require `&&` and `||`, not `AND` and `OR` -->
@@ -158,7 +163,6 @@
158163
<rule ref="SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration"/>
159164
<rule ref="SlevomatCodingStandard.Exceptions.DeadCatch"/>
160165
<rule ref="SlevomatCodingStandard.Namespaces.DisallowGroupUse"/>
161-
<!-- <rule ref="SlevomatCodingStandard.Namespaces.UnusedUses"/>-->
162166
<rule ref="SlevomatCodingStandard.Namespaces.UselessAlias"/>
163167
<rule ref="SlevomatCodingStandard.Namespaces.NamespaceSpacing"/>
164168
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace"/>
@@ -194,7 +198,17 @@
194198
<property name="nullPosition" value="last" />
195199
</properties>
196200
</rule>
197-
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenComments"/>
201+
<rule ref="SlevomatCodingStandard.Commenting.ForbiddenComments">
202+
<properties>
203+
<property name="forbiddenCommentPatterns" type="array">
204+
<element value="~^(?:(?!private|protected|static)\S+ )?(?:con|de)structor\.\z~i"/>
205+
<element value="~^Created by .+\.\z~i"/>
206+
<element value="~^(User|Date|Time): \S+\z~i"/>
207+
<element value="~^\S+ [gs]etter\.\z~i"/>
208+
<element value="~^Class \S+\z~i"/>
209+
</property>
210+
</properties>
211+
</rule>
198212
<rule ref="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration"/>
199213
<rule ref="SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration"/>
200214
<rule ref="SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration">
@@ -247,8 +261,38 @@
247261
</rule>
248262

249263
<rule ref="Proton.Spacing.ArrowFunctionSpacing"/>
250-
<rule ref="Proton.Naming.AbstractClassName"/>
251-
<rule ref="Proton.Naming.InterfaceName"/>
264+
265+
<!-- PHP files should only contain code -->
266+
<rule ref="Generic.Files.InlineHTML"/>
267+
268+
<!-- Force array element indentation with 4 spaces -->
269+
<rule ref="Generic.Arrays.ArrayIndent">
270+
<exclude name="Generic.Arrays.ArrayIndent.CloseBraceNotNewLine"/>
271+
</rule>
272+
273+
<rule ref="Squiz.PHP.Eval"/>
274+
275+
<!-- Forbid empty statements -->
276+
<rule ref="Generic.CodeAnalysis.EmptyStatement">
277+
<!-- But allow empty catch -->
278+
<exclude name="Generic.CodeAnalysis.EmptyStatement.DetectedCatch"/>
279+
</rule>
280+
281+
<!-- Enforce strict comparisons -->
282+
<rule ref="SlevomatCodingStandard.Operators.DisallowEqualOperators"/>
283+
284+
<!-- Forbid empty comments -->
285+
<rule ref="SlevomatCodingStandard.Commenting.EmptyComment"/>
286+
287+
<!-- Forbid unused use statements -->
288+
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
289+
<properties>
290+
<property name="searchAnnotations" value="true"/>
291+
</properties>
292+
</rule>
293+
294+
<!-- Requires that attributes are always after documentation comment -->
295+
<rule ref="SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment"/>
252296

253297
<exclude-pattern>*/node_modules/*</exclude-pattern>
254298
<exclude-pattern>*/vendor/*</exclude-pattern>

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"minimum-stability": "dev",
1212
"prefer-stable": true,
1313
"scripts": {
14-
"test-print": "phpcs --standard=tests/phpcs.xml --report=summary tests",
15-
"test": "phpcs --standard=tests/phpcs.xml --report=csv tests | sort -r | cut -f 2,3,4,6 -d ',' > tests/out.csv && diff tests/expected_csv.txt tests/out.csv"
14+
"test-print": "phpcs --standard=phpcs.xml --report=summary tests",
15+
"test": "phpcs --standard=phpcs.xml --report=csv tests | sort -r | cut -f 2,3,4,6 -d ',' > tests/out.csv && diff tests/expected_csv.txt tests/out.csv"
1616
},
1717
"require": {
1818
"php": "^7.4 || ^8.0",

tests/phpcs.xml renamed to phpcs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Proton PHP Coding Standard
55
</description>
66

7-
<config name="installed_paths" value="../Proton"/>
7+
<config name="installed_paths" value="Proton"/>
88

99
<rule ref="Proton"/>
1010

tests/correct/AbstractClassOk.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,18 @@ public function getOne(): int
1212
{
1313
return 1;
1414
}
15+
16+
/**
17+
* Method comment
18+
*/
19+
#[Attribute1, Attribute2('var')]
20+
#[Attribute3(), Attribute4]
21+
public function method(
22+
/** @param int $parameter */
23+
#[Attribute1] #[Attribute2] #[Attribute3]
24+
#[Attribute4] #[Attribute5] #[Attribute6]
25+
int $parameter,
26+
): void {
27+
echo $parameter;
28+
}
1529
}

tests/correct/ClassOk.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,15 @@ final class ClassOk
2020
* Foo.
2121
*/
2222
protected array $foo = [];
23+
private array $bar = [];
2324
private string $test = 'foo';
2425

2526
public function __construct(array $config)
2627
{
28+
$this->bar = [
29+
'bar' => 'foo',
30+
];
31+
2732
$this->foo = $config + [
2833
'foo' => 'bar',
2934
];
@@ -42,6 +47,7 @@ public function ping(Request $request): mixed
4247
);
4348
} catch (\RuntimeException) {
4449
// ...
50+
} catch (\Exception) {
4551
}
4652

4753
return $request->query->get('test');
@@ -72,6 +78,10 @@ public function foo(string $a, string $b): bool|string
7278
throw new \RuntimeException();
7379
}
7480

81+
if ($a === 'y') {
82+
throw new \Exception('');
83+
}
84+
7585
return false;
7686
}
7787

tests/correct/ValidTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Proton\Test;
6+
7+
trait ValidTrait
8+
{
9+
public function getOne(): int
10+
{
11+
return 1;
12+
}
13+
}

0 commit comments

Comments
 (0)