You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can disallow control structures like else, elseif, goto (#257)
Checking params inside ( ... ) doesn't work at the moment, so you can disallow all `declare()`s but can't re-allow e.g. `declare(strict-types = 1)`.
If you try to disallow `else if` with the space, an exception will be thrown, because `else if` is parsed as `else` followed by `if`, so disallowing `else if` with the space wouldn't have the desired effect and the result would be unexpected.
Close#68
Copy file name to clipboardExpand all lines: docs/custom-rules.md
+27Lines changed: 27 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -10,6 +10,7 @@ There are several different types (and configuration keys) that can be disallowe
10
10
6.`disallowedSuperglobals` - for usages of superglobal variables like `$GLOBALS` or `$_POST`
11
11
7.`disallowedAttributes` - for attributes like `#[Entity(class: Foo::class, something: true)]`
12
12
8.`disallowedEnums` - for enums, both pure & backed, like `Suit::Hearts` (like class constants, enums need to be split to `enum: Suit` & `case: Hearts` in the configuration, see notes below)
13
+
9.`disallowedControlStructures` - for control structures like `if`, `else`, `elseif`, loops, `require` & `include`, and `goto`
13
14
14
15
Use them to add rules to your `phpstan.neon` config file. I like to use a separate file (`disallowed-calls.neon`) for these which I'll include later on in the main `phpstan.neon` config file. Here's an example, update to your needs:
15
16
@@ -165,10 +166,36 @@ You can treat some language constructs as functions and disallow it in `disallow
165
166
166
167
To disallow naive object creation (`new ClassName()` or `new $classname`), disallow `NameSpace\ClassName::__construct` in `disallowedMethodCalls`. Works even when there's no constructor defined in that class.
167
168
169
+
You can also disallow control structures, see below.
170
+
168
171
### Constants
169
172
170
173
When [disallowing constants](disallowing-constants.md) please be aware of limitations and special requirements, see [docs](disallowing-constants.md).
171
174
172
175
### Enums
173
176
174
177
Similar to disallowing constants, enums have some limitations, see [docs](disallowing-enums.md).
178
+
179
+
### Control structures
180
+
181
+
You can forbid [control structures](https://www.php.net/language.control-structures) like `if`, `else`, `elseif` (don't write it as `else if` because `else if` is parsed as `else` followed by `if` producing unexpected results), loops, `break`, `continue`, `goto`, `require`, `include` (and the `_once` variants).
182
+
183
+
Currently, parameters are not checked, so it's not possible to disallow for example `declare`, but re-allow `declare(strict-types = 1)`.
184
+
185
+
You can use `controlStructure` or `structure` directives, and both can be specified as arrays:
0 commit comments