Skip to content

Commit 53e542e

Browse files
Update README and Rules documentation to clarify rules and add new rules
- Revised README to specify that all controllers must be `readonly` and clarified method argument limits. - Enhanced Rules documentation with new rules for catching exceptions and naming conventions for boolean-returning methods. - Corrected class names in configuration examples for consistency.
1 parent c2dc5c4 commit 53e542e

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Additional rules for PHPStan, mostly focused on Clean Code and architecture conv
44

55
The rules help with enforcing certain method signatures, return types and dependency constraints in your codebase.
66

7-
All controllers in your application should be `readonly`, no method should have more than 3 arguments, and no class should have more than 2 nested control structures.
7+
For example, you can configure the rules so that all controllers in your application must be `readonly`, no method should have more than 3 arguments, and no class should have more than 2 nested control structures.
88

99
## Usage
1010

@@ -21,6 +21,8 @@ See [Rules documentation](docs/Rules.md) for a list of available rules and confi
2121
- [Readonly Class Rule](docs/Rules.md#readonly-class-rule)
2222
- [Final Class Rule](docs/Rules.md#final-class-rule)
2323
- [Namespace Class Pattern Rule](docs/Rules.md#namespace-class-pattern-rule)
24+
- [Catch Exception of Type Not Allowed Rule](docs/Rules.md#catch-exception-of-type-not-allowed-rule)
25+
- [Methods Returning Bool Must Follow Naming Convention Rule](docs/Rules.md#methods-returning-bool-must-follow-naming-convention-rule)
2426
- [Method Signature Must Match Rule](docs/Rules.md#method-signature-must-match-rule)
2527
- [Method Must Return Type Rule](docs/Rules.md#method-must-return-type-rule)
2628
- Clean Code Rules:

docs/Rules.md

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,49 @@
22

33
Add them to your `phpstan.neon` configuration file under the section `services`.
44

5-
## Control Structure Nesting Rule
5+
## Control Structure Nesting Rule {#control-structure-nesting-rule}
66

77
Ensures that the nesting level of `if` and `try-catch` statements does not exceed a specified limit.
88

99
**Configuration Example:**
1010
```neon
1111
-
12-
class: Phauthentic\PhpstanRules\CleanCode\ControlStructureNestingRule
12+
class: Phauthentic\PHPStanRules\CleanCode\ControlStructureNestingRule
1313
arguments:
1414
maxNestingLevel: 2
1515
tags:
1616
- phpstan.rules.rule
1717
```
1818

19-
## Too Many Arguments Rule
19+
## Too Many Arguments Rule {#too-many-arguments-rule}
2020

2121
Checks that methods do not have more than a specified number of arguments.
2222

2323
**Configuration Example:**
2424
```neon
2525
-
26-
class: Phauthentic\PhpstanRules\CleanCode\TooManyArgumentsRule
26+
class: Phauthentic\PHPStanRules\CleanCode\TooManyArgumentsRule
2727
arguments:
2828
maxArguments: 3
2929
tags:
3030
- phpstan.rules.rule
3131
```
3232

33-
## Readonly Class Rule
33+
## Readonly Class Rule {#readonly-class-rule}
3434

3535
Ensures that classes matching specified patterns are declared as `readonly`.
3636

3737
**Configuration Example:**
3838
```neon
3939
-
40-
class: Phauthentic\PhpstanRules\Architecture\ReadonlyClassRule
40+
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeReadonlyRule
4141
arguments:
4242
patterns: ['/^App\\Controller\\/']
4343
tags:
4444
- phpstan.rules.rule
4545
```
4646

47-
## Dependency Constraints Rule
47+
## Dependency Constraints Rule {#dependency-constraints-rule}
4848

4949
Enforces dependency constraints between namespaces by checking `use` statements.
5050

@@ -55,7 +55,7 @@ In the example below nothing from `App\Domain` can depend on anything from `App\
5555
**Configuration Example:**
5656
```neon
5757
-
58-
class: Phauthentic\PhpstanRules\Architecture\DependencyConstraintsRule
58+
class: Phauthentic\PHPStanRules\Architecture\DependencyConstraintsRule
5959
arguments:
6060
forbiddenDependencies: [
6161
'/^App\\Domain(?:\\\w+)*$/': ['/^App\\Controller\\/']
@@ -64,28 +64,28 @@ In the example below nothing from `App\Domain` can depend on anything from `App\
6464
- phpstan.rules.rule
6565
```
6666

67-
## Final Class Rule
67+
## Final Class Rule {#final-class-rule}
6868

6969
Ensures that classes matching specified patterns are declared as `final`.
7070

7171
**Configuration Example:**
7272
```neon
7373
-
74-
class: Phauthentic\PhpstanRules\Architecture\FinalClassRule
74+
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeFinalRule
7575
arguments:
7676
patterns: ['/^App\\Service\\/']
7777
tags:
7878
- phpstan.rules.rule
7979
```
8080

81-
## Namespace Class Pattern Rule
81+
## Namespace Class Pattern Rule {#namespace-class-pattern-rule}
8282

8383
Ensures that classes inside namespaces matching a given regex must have names matching at least one of the provided patterns.
8484

8585
**Configuration Example:**
8686
```neon
8787
-
88-
class: Phauthentic\PhpstanRules\Architecture\NamespaceClassPatternRule
88+
class: Phauthentic\PHPStanRules\Architecture\ClassnameMustMatchPatternRule
8989
arguments:
9090
namespaceClassPatterns: [
9191
[
@@ -99,28 +99,42 @@ Ensures that classes inside namespaces matching a given regex must have names ma
9999
- phpstan.rules.rule
100100
```
101101

102-
## Catch Exception of Type Not Allowed Rule
102+
## Catch Exception of Type Not Allowed Rule {#catch-exception-of-type-not-allowed-rule}
103103

104104
Ensures that specific exception types are not caught in catch blocks. This is useful for preventing the catching of overly broad exception types like `Exception`, `Error`, or `Throwable`.
105105

106106
**Configuration Example:**
107107
```neon
108108
-
109-
class: Phauthentic\PhpstanRules\Architecture\CatchExceptionOfTypeNotAllowedRule
109+
class: Phauthentic\PHPStanRules\Architecture\CatchExceptionOfTypeNotAllowedRule
110110
arguments:
111111
forbiddenExceptionTypes: ['Exception', 'Error', 'Throwable']
112112
tags:
113113
- phpstan.rules.rule
114114
```
115115

116-
## Method Signature Must Match Rule
116+
## Methods Returning Bool Must Follow Naming Convention Rule {#methods-returning-bool-must-follow-naming-convention-rule}
117+
118+
Ensures that methods returning boolean values follow a specific naming convention. By default, boolean methods should start with `is`, `has`, `can`, `should`, `was`, or `will`.
119+
120+
**Configuration Example:**
121+
```neon
122+
-
123+
class: Phauthentic\PHPStanRules\Architecture\MethodsReturningBoolMustFollowNamingConventionRule
124+
arguments:
125+
regex: '/^(is|has|can|should|was|will)[A-Z_]/'
126+
tags:
127+
- phpstan.rules.rule
128+
```
129+
130+
## Method Signature Must Match Rule {#method-signature-must-match-rule}
117131

118132
Ensures that methods matching a class and method name pattern have a specific signature, including parameter types, names, and count.
119133

120134
**Configuration Example:**
121135
```neon
122136
-
123-
class: Phauthentic\PhpstanRules\Architecture\MethodSignatureMustMatchRule
137+
class: Phauthentic\PHPStanRules\Architecture\MethodSignatureMustMatchRule
124138
arguments:
125139
signaturePatterns:
126140
-
@@ -142,14 +156,14 @@ Ensures that methods matching a class and method name pattern have a specific si
142156
- `signature`: List of expected parameter types and (optionally) name patterns.
143157
- `visibilityScope`: Optional visibility scope (e.g., `public`, `protected`, `private`).
144158

145-
## Method Must Return Type Rule
159+
## Method Must Return Type Rule {#method-must-return-type-rule}
146160

147161
Ensures that methods matching a class and method name pattern have a specific return type, nullability, or are void.
148162

149163
**Configuration Example:**
150164
```neon
151165
-
152-
class: Phauthentic\PhpstanRules\Architecture\MethodMustReturnTypeRule
166+
class: Phauthentic\PHPStanRules\Architecture\MethodMustReturnTypeRule
153167
arguments:
154168
returnTypePatterns:
155169
-

0 commit comments

Comments
 (0)