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
Copy file name to clipboardExpand all lines: README.md
+25-27Lines changed: 25 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -6,31 +6,29 @@
6
6
7
7
[PHPStan](https://phpstan.org/) focuses on finding bugs in your code. But in PHP there's a lot of leeway in how stuff can be written. This repository contains additional rules that revolve around strictly and strongly typed code with no loose casting for those who want additional safety in extremely defensive programming:
8
8
9
-
* Require booleans in `if`, `elseif`, ternary operator, after `!`, and on both sides of `&&` and `||`.
10
-
* Require booleans in `while` and `do while` loop conditions.
11
-
* Require numeric operands or arrays in `+` and numeric operands in `-`/`*`/`/`/`**`/`%`.
12
-
* Require numeric operand in `$var++`, `$var--`, `++$var`and `--$var`.
13
-
* These functions contain a `$strict` parameter for better type safety, it must be set to `true`:
14
-
*`in_array` (3rd parameter)
15
-
*`array_search` (3rd parameter)
16
-
*`array_keys` (3rd parameter; only if the 2nd parameter `$search_value` is provided)
17
-
*`base64_decode` (2nd parameter)
18
-
* Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.
19
-
* Variables set in foreach that's always looped thanks to non-empty arrays cannot be used after the loop.
20
-
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
21
-
* Check that statically declared methods are called statically.
22
-
* Disallow `empty()` - it's a very loose comparison (see [manual](https://php.net/empty)), it's recommended to use more strict one.
23
-
* Disallow short ternary operator (`?:`) - implies weak comparison, it's recommended to use null coalesce operator (`??`) or ternary operator with strict condition.
* Disallow overwriting variables with foreach key and value variables
26
-
* Always true `instanceof`, type-checking `is_*` functions and strict comparisons `===`/`!==`. These checks can be turned off by setting `checkAlwaysTrueInstanceof`/`checkAlwaysTrueCheckTypeFunctionCall`/`checkAlwaysTrueStrictComparison` to false.
27
-
* Correct case for referenced and called function names.
28
-
* Correct case for inherited and implemented method names.
29
-
* Contravariance for parameter types and covariance for return types in inherited methods (also known as Liskov substitution principle - LSP)
|`booleansInConditions`| Require booleans in `if`, `elseif`, ternary operator, after `!`, and on both sides of `&&` and `\|\|`. |
12
+
|`booleansInLoopConditions`| Require booleans in `while` and `do while` loop conditions. |
13
+
|`numericOperandsInArithmeticOperators`| Require numeric operands or arrays in `+` and numeric operands in `-`/`*`/`/`/`**`/`%`. |
14
+
|`numericOperandsInArithmeticOperators`| Require numeric operand in `$var++`, `$var--`, `++$var`and `--$var`. |
15
+
|`strictFunctionCalls`| These functions contain a `$strict` parameter for better type safety, it must be set to `true`:<br>*`in_array` (3rd parameter)<br>*`array_search` (3rd parameter)<br>*`array_keys` (3rd parameter; only if the 2nd parameter `$search_value` is provided)<br>*`base64_decode` (2nd parameter). |
16
+
|`overwriteVariablesWithLoop`| Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop. |
17
+
|`overwriteVariablesWithLoop`| Variables set in foreach that's always looped thanks to non-empty arrays cannot be used after the loop. |
18
+
|`switchConditionsMatchingType`| Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results. |
19
+
|`dynamicCallOnStaticMethod`| Check that statically declared methods are called statically. |
20
+
|`disallowedEmpty`| Disallow `empty()` - it's a very loose comparison (see [manual](https://php.net/empty)), it's recommended to use more strict one. |
21
+
|`disallowedShortTernary`| Disallow short ternary operator (`?:`) - implies weak comparison, it's recommended to use null coalesce operator (`??`) or ternary operator with strict condition. |
|`overwriteVariablesWithLoop`| Disallow overwriting variables with foreach key and value variables. |
24
+
|`checkAlwaysTrueInstanceof`, `checkAlwaysTrueCheckTypeFunctionCall`, `checkAlwaysTrueStrictComparison`| Always true `instanceof`, type-checking `is_*` functions and strict comparisons `===`/`!==`. These checks can be turned off by setting `checkAlwaysTrueInstanceof`, `checkAlwaysTrueCheckTypeFunctionCall` and `checkAlwaysTrueStrictComparison` to false. |
25
+
|| Correct case for referenced and called function names. |
26
+
|`matchingInheritedMethodNames`| Correct case for inherited and implemented method names. |
27
+
|| Contravariance for parameter types and covariance for return types in inherited methods (also known as Liskov substitution principle - LSP).|
|`closureUsesThis`| Closure should use `$this` directly instead of using `$this` variable indirectly. |
34
32
35
33
Additional rules are coming in subsequent releases!
36
34
@@ -84,7 +82,7 @@ parameters:
84
82
illegalConstructorMethodCall: false
85
83
```
86
84
87
-
Aside from introducing new custom rules, phpstan-strict-rules also [change the default values of some configuration parameters](./rules.neon#L1) that are present in PHPStan itself. These parameters are [documented on phpstan.org](https://phpstan.org/config-reference#stricter-analysis).
85
+
Aside from introducing new custom rules, phpstan-strict-rules also [change the default values of some configuration parameters](https://github.com/phpstan/phpstan-strict-rules/blob/1.6.x/rules.neon#L1) that are present in PHPStan itself. These parameters are [documented on phpstan.org](https://phpstan.org/config-reference#stricter-analysis).
88
86
89
87
## Enabling rules one-by-one
90
88
@@ -107,4 +105,4 @@ parameters:
107
105
booleansInConditions: true
108
106
```
109
107
110
-
Even with `strictRules.allRules` set to `false`, part of this package is still in effect. That's because phpstan-strict-rules also [change the default values of some configuration parameters](./rules.neon#L1) that are present in PHPStan itself. These parameters are [documented on phpstan.org](https://phpstan.org/config-reference#stricter-analysis).
108
+
Even with `strictRules.allRules` set to `false`, part of this package is still in effect. That's because phpstan-strict-rules also [change the default values of some configuration parameters](https://github.com/phpstan/phpstan-strict-rules/blob/1.6.x/rules.neon#L1) that are present in PHPStan itself. These parameters are [documented on phpstan.org](https://phpstan.org/config-reference#stricter-analysis).
0 commit comments