Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a38aa25

Browse files
committedMar 28, 2025·
README.md: Match rule descriptions to parameters in table
1 parent 3ecc310 commit a38aa25

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed
 

‎README.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,29 @@
66

77
[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:
88

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.
24-
* Disallow variable variables (`$$foo`, `$this->$method()` etc.)
25-
* 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)
30-
* Check LSP even for static methods
31-
* Require calling parent constructor
32-
* Disallow usage of backtick operator (`` $ls = `ls -la` ``)
33-
* Closure should use `$this` directly instead of using `$this` variable indirectly
9+
| Configuration Parameters | Rule Description |
10+
|:---------------------------------------|:--------------------------------------------------------------------------------------------------------|
11+
| `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. |
22+
| `noVariableVariables` | Disallow variable variables (`$$foo`, `$this->$method()` etc.). |
23+
| `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).|
28+
| | Check LSP even for static methods. |
29+
| `requireParentConstructorCall` | Require calling parent constructor. |
30+
| `disallowedBacktick` | Disallow usage of backtick operator (`` $ls = `ls -la` ``). |
31+
| `closureUsesThis` | Closure should use `$this` directly instead of using `$this` variable indirectly. |
3432

3533
Additional rules are coming in subsequent releases!
3634

@@ -84,7 +82,7 @@ parameters:
8482
illegalConstructorMethodCall: false
8583
```
8684

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).
8886

8987
## Enabling rules one-by-one
9088

@@ -107,4 +105,4 @@ parameters:
107105
booleansInConditions: true
108106
```
109107

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

Comments
 (0)
Please sign in to comment.