@@ -7,6 +7,7 @@ Add them to your `phpstan.neon` configuration file under the section `services`.
7
7
Ensures that the nesting level of ` if ` and ` try-catch ` statements does not exceed a specified limit.
8
8
9
9
** Configuration Example:**
10
+
10
11
``` neon
11
12
-
12
13
class: Phauthentic\PHPStanRules\CleanCode\ControlStructureNestingRule
@@ -21,6 +22,7 @@ Ensures that the nesting level of `if` and `try-catch` statements does not excee
21
22
Checks that methods do not have more than a specified number of arguments.
22
23
23
24
** Configuration Example:**
25
+
24
26
``` neon
25
27
-
26
28
class: Phauthentic\PHPStanRules\CleanCode\TooManyArgumentsRule
@@ -35,6 +37,7 @@ Checks that methods do not have more than a specified number of arguments.
35
37
Ensures that classes matching specified patterns are declared as ` readonly ` .
36
38
37
39
** Configuration Example:**
40
+
38
41
``` neon
39
42
-
40
43
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeReadonlyRule
@@ -53,6 +56,7 @@ The constructor takes an array of namespace dependencies. The key is the namespa
53
56
In the example below nothing from ` App\Domain ` can depend on anything from ` App\Controller ` .
54
57
55
58
** Configuration Example:**
59
+
56
60
``` neon
57
61
-
58
62
class: Phauthentic\PHPStanRules\Architecture\DependencyConstraintsRule
@@ -69,6 +73,7 @@ In the example below nothing from `App\Domain` can depend on anything from `App\
69
73
Ensures that classes matching specified patterns are declared as ` final ` .
70
74
71
75
** Configuration Example:**
76
+
72
77
``` neon
73
78
-
74
79
class: Phauthentic\PHPStanRules\Architecture\ClassMustBeFinalRule
@@ -83,6 +88,7 @@ Ensures that classes matching specified patterns are declared as `final`.
83
88
Ensures that classes inside namespaces matching a given regex must have names matching at least one of the provided patterns.
84
89
85
90
** Configuration Example:**
91
+
86
92
``` neon
87
93
-
88
94
class: Phauthentic\PHPStanRules\Architecture\ClassnameMustMatchPatternRule
@@ -104,6 +110,7 @@ Ensures that classes inside namespaces matching a given regex must have names ma
104
110
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 ` .
105
111
106
112
** Configuration Example:**
113
+
107
114
``` neon
108
115
-
109
116
class: Phauthentic\PHPStanRules\Architecture\CatchExceptionOfTypeNotAllowedRule
@@ -118,6 +125,7 @@ Ensures that specific exception types are not caught in catch blocks. This is us
118
125
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
126
120
127
** Configuration Example:**
128
+
121
129
``` neon
122
130
-
123
131
class: Phauthentic\PHPStanRules\Architecture\MethodsReturningBoolMustFollowNamingConventionRule
@@ -132,6 +140,7 @@ Ensures that methods returning boolean values follow a specific naming conventio
132
140
Ensures that methods matching a class and method name pattern have a specific signature, including parameter types, names, and count.
133
141
134
142
** Configuration Example:**
143
+
135
144
``` neon
136
145
-
137
146
class: Phauthentic\PHPStanRules\Architecture\MethodSignatureMustMatchRule
@@ -151,16 +160,18 @@ Ensures that methods matching a class and method name pattern have a specific si
151
160
tags:
152
161
- phpstan.rules.rule
153
162
```
163
+
154
164
- ` pattern ` : Regex for ` ClassName::methodName ` .
155
165
- ` minParameters ` /` maxParameters ` : Minimum/maximum number of parameters.
156
166
- ` signature ` : List of expected parameter types and (optionally) name patterns.
157
167
- ` visibilityScope ` : Optional visibility scope (e.g., ` public ` , ` protected ` , ` private ` ).
158
168
159
169
## Method Must Return Type Rule {#method-must-return-type-rule}
160
170
161
- Ensures that methods matching a class and method name pattern have a specific return type, nullability, or are void.
171
+ Ensures that methods matching a class and method name pattern have a specific return type, nullability, or are void. Supports union types with "oneOf" and "allOf" configurations.
162
172
163
173
** Configuration Example:**
174
+
164
175
``` neon
165
176
-
166
177
class: Phauthentic\PHPStanRules\Architecture\MethodMustReturnTypeRule
@@ -182,13 +193,41 @@ Ensures that methods matching a class and method name pattern have a specific re
182
193
pattern: '/^MyClass::reset$/'
183
194
type: 'void'
184
195
nullable: false
185
- void: true
196
+ void: false
197
+ objectTypePattern: null
198
+ -
199
+ pattern: '/^MyClass::getValue$/'
200
+ nullable: false
201
+ void: false
202
+ oneOf: ['int', 'string', 'bool']
203
+ objectTypePattern: null
204
+ -
205
+ pattern: '/^MyClass::getUnionType$/'
206
+ nullable: false
207
+ void: false
208
+ allOf: ['int', 'string']
209
+ objectTypePattern: null
210
+ -
211
+ pattern: '/^MyClass::getAnyType$/'
212
+ anyOf: ['object', 'void']
213
+ objectTypePattern: null
214
+ -
215
+ pattern: '/^MyClass::getEntity$/'
216
+ anyOf: ['regex:/^App\\Entity\\/', 'void']
186
217
objectTypePattern: null
187
218
tags:
188
219
- phpstan.rules.rule
189
220
```
221
+
190
222
- ` pattern ` : Regex for ` ClassName::methodName ` .
191
- - ` type ` : Expected return type (` int ` , ` string ` , ` object ` , etc.).
223
+ - ` type ` : Expected return type (` int ` , ` string ` , ` object ` , ` void ` , etc.). When using ` oneOf ` or ` allOf ` , this field is optional .
192
224
- ` nullable ` : Whether the return type must be nullable.
193
- - ` void ` : Whether the method must return void.
225
+ - ` void ` : Legacy field for void return types (use ` type: ' void' ` instead) .
194
226
- ` objectTypePattern ` : Regex for object return types (if ` type ` is ` object ` ).
227
+ - ` oneOf ` : Array of types where one must match (for union types).
228
+ - ` allOf ` : Array of types where all must be present in the union type.
229
+ - ` anyOf ` : Alias for ` oneOf ` - array of types where one must match.
230
+
231
+ ** Regex Support** : You can use regex patterns in ` oneOf ` , ` allOf ` , and ` anyOf ` arrays by prefixing them with ` regex: ` . For example:
232
+ - ` 'regex:/^App\\Entity\\/' ` - matches any class starting with "App\Entity\"
233
+ - ` 'regex:/^UserEntity$/' ` - matches exactly "UserEntity"
0 commit comments