Skip to content

Commit ae21ffb

Browse files
author
Florian Krämer
committed
PHPCS & PHPStan fixes
1 parent e1c2b9f commit ae21ffb

14 files changed

+104
-22
lines changed

phpcs.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="Phauthentic">
3+
<arg name="tab-width" value="4"/>
4+
<rule ref="PSR12">
5+
<exclude name="Squiz.WhiteSpace.OperatorSpacing.SpacingAfter"/>
6+
<exclude name="Generic.Files.LineLength.TooLong"/>
7+
</rule>
8+
<file>./src</file>
9+
<file>./tests</file>
10+
</ruleset>

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
parameters:
22
level: 8
33
paths:
4-
- data
4+
- src
55
parallel:
66
maximumNumberOfProcesses: 8
77

src/ControlStructureNestingRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
/**
2020
* A rule to check the nesting level of control structures (if, else, elseif, try, catch).
21+
*
22+
* @implements Rule<Node>
2123
*/
2224
class ControlStructureNestingRule implements Rule
2325
{

src/DependencyConstraintsRule.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Stmt\Use_;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Rules\Rule;
11+
use PHPStan\Rules\RuleError;
1112
use PHPStan\Rules\RuleErrorBuilder;
1213

1314
/**
@@ -56,20 +57,33 @@ public function processNode(Node $node, Scope $scope): array
5657
continue;
5758
}
5859

59-
foreach ($node->uses as $use) {
60-
$usedClassName = $use->name->toString();
61-
foreach ($disallowedDependencyPatterns as $disallowedPattern) {
62-
if (preg_match($disallowedPattern, $usedClassName)) {
63-
$errors[] = RuleErrorBuilder::message(sprintf(
64-
self::ERROR_MESSAGE,
65-
$currentNamespace,
66-
$usedClassName
67-
))
68-
->line($use->getStartLine())
69-
->nonIgnorable()
70-
->build();
60+
$errors = $this->validateUseStatements($node, $disallowedDependencyPatterns, $currentNamespace, $errors);
61+
}
62+
63+
return $errors;
64+
}
7165

72-
}
66+
/**
67+
* @param Node $node
68+
* @param array<string> $disallowedDependencyPatterns
69+
* @param string $currentNamespace
70+
* @param array<RuleError> $errors
71+
* @return array<RuleError>
72+
* @throws \PHPStan\ShouldNotHappenException
73+
*/
74+
public function validateUseStatements(Node $node, array $disallowedDependencyPatterns, string $currentNamespace, array $errors): array
75+
{
76+
foreach ($node->uses as $use) {
77+
$usedClassName = $use->name->toString();
78+
foreach ($disallowedDependencyPatterns as $disallowedPattern) {
79+
if (preg_match($disallowedPattern, $usedClassName)) {
80+
$errors[] = RuleErrorBuilder::message(sprintf(
81+
self::ERROR_MESSAGE,
82+
$currentNamespace,
83+
$usedClassName
84+
))
85+
->line($use->getStartLine())
86+
->build();
7387
}
7488
}
7589
}

src/FinalClassRule.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212

13+
/**
14+
* @implements Rule<Class_>
15+
*/
1316
class FinalClassRule implements Rule
1417
{
1518
private const ERROR_MESSAGE = 'Class %s must be final.';
1619

20+
/**
21+
* @var array<string> An array of regex patterns to match against class names.
22+
* e.g., ['#^App\\Domain\\.*#', '#^App\\Service\\.*#']
23+
*/
1724
protected array $patterns;
1825

26+
/**
27+
* @param array<string> $patterns An array of regex patterns to match against class names.
28+
* Each pattern should be a valid PCRE regex.
29+
*/
1930
public function __construct(array $patterns)
2031
{
2132
$this->patterns = $patterns;

src/NamespaceClassPatternRule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
/**
1515
* PHPStan rule to ensure that classes inside namespaces matching a given regex
1616
* must have names matching at least one of the provided patterns.
17+
*
18+
* @implements Rule<Namespace_>
1719
*/
1820
class NamespaceClassPatternRule implements Rule
1921
{

src/ReadonlyClassRule.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
1212

13+
/**
14+
* @implements Rule<Class_>
15+
*/
1316
class ReadonlyClassRule implements Rule
1417
{
1518
private const ERROR_MESSAGE = 'Class %s must be readonly.';
1619

20+
/**
21+
* @var string[]
22+
*/
1723
protected array $patterns;
1824

25+
/**
26+
* @param string[] $patterns
27+
*/
1928
public function __construct(array $patterns)
2029
{
2130
$this->patterns = $patterns;

src/TooManyArgumentsRule.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,31 @@
77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PHPStan\Analyser\Scope;
10+
use PHPStan\Rules\IdentifierRuleError;
1011
use PHPStan\Rules\Rule;
12+
use PHPStan\Rules\RuleError;
1113
use PHPStan\Rules\RuleErrorBuilder;
14+
use PHPStan\ShouldNotHappenException;
1215

1316
/**
1417
* A configurable rule to check the number of arguments in a method.
18+
*
19+
* @implements Rule<ClassMethod>
1520
*/
1621
class TooManyArgumentsRule implements Rule
1722
{
1823
private const ERROR_MESSAGE = 'Method %s::%s has too many arguments (%d). Maximum allowed is %d.';
1924

2025
private int $maxArguments;
26+
27+
/**
28+
* @var string[]
29+
*/
2130
private array $patterns;
2231

32+
/**
33+
* @param string[] $patterns
34+
*/
2335
public function __construct(int $maxArguments, array $patterns = [])
2436
{
2537
$this->maxArguments = $maxArguments;
@@ -36,9 +48,18 @@ private function isClassMethod(Node $node): bool
3648
return $node instanceof ClassMethod;
3749
}
3850

51+
/**
52+
* Processes the node and checks if it exceeds the maximum number of arguments.
53+
*
54+
* @param Node $node The node to process.
55+
* @param Scope $scope The scope of the node.
56+
* @return RuleError[] An array of rule errors if any.
57+
* @throws ShouldNotHappenException
58+
*/
3959
public function processNode(Node $node, Scope $scope): array
4060
{
41-
if (!$this->isClassMethod($node)
61+
if (
62+
!$this->isClassMethod($node)
4263
|| !$this->matchesPattern($scope)
4364
|| !$this->argumentCountIsExceeded($node)
4465
) {
@@ -54,7 +75,8 @@ public function processNode(Node $node, Scope $scope): array
5475
count($node->params),
5576
$this->maxArguments
5677
)
57-
)->build()
78+
)
79+
->build()
5880
];
5981
}
6082

tests/ControlStructureNestingRuleTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace App;
46

tests/FinalClassRuleTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php declare(strict_types = 1);
1+
<?php
2+
3+
declare(strict_types=1);
24

35
namespace App;
46

0 commit comments

Comments
 (0)