PHPackages                             phpstan/phpstan-strict-rules - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. phpstan/phpstan-strict-rules

ActivePhpstan-extension[Testing &amp; Quality](/categories/testing)

phpstan/phpstan-strict-rules
============================

Extra strict and opinionated rules for PHPStan

2.0.10(3mo ago)69661.6M—3.2%61[39 issues](https://github.com/phpstan/phpstan-strict-rules/issues)[7 PRs](https://github.com/phpstan/phpstan-strict-rules/pulls)20MITPHPPHP ^7.4 || ^8.0CI passing

Since Nov 26Pushed today13 watchersCompare

[ Source](https://github.com/phpstan/phpstan-strict-rules)[ Packagist](https://packagist.org/packages/phpstan/phpstan-strict-rules)[ RSS](/packages/phpstan-phpstan-strict-rules/feed)WikiDiscussions 2.0.x Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (63)Used By (20)

Extra strict and opinionated rules for PHPStan
==============================================

[](#extra-strict-and-opinionated-rules-for-phpstan)

[![Build](https://github.com/phpstan/phpstan-strict-rules/workflows/Build/badge.svg)](https://github.com/phpstan/phpstan-strict-rules/actions)[![Latest Stable Version](https://camo.githubusercontent.com/82a8893993cfbc691677b24639896a3d358e0c710f16137a7c13de8cc38cd691/68747470733a2f2f706f7365722e707567782e6f72672f7068707374616e2f7068707374616e2d7374726963742d72756c65732f762f737461626c65)](https://packagist.org/packages/phpstan/phpstan-strict-rules)[![License](https://camo.githubusercontent.com/5ae804198f83c126e2b5d331f4daaaadeac6a2e665b62f66bc495f3c693ccddc/68747470733a2f2f706f7365722e707567782e6f72672f7068707374616e2f7068707374616e2d7374726963742d72756c65732f6c6963656e7365)](https://packagist.org/packages/phpstan/phpstan-strict-rules)

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

Configuration ParametersRule Description`booleansInConditions`Require booleans in `if`, `elseif`, ternary operator, after `!`, and on both sides of `&&` and `||`.`booleansInLoopConditions`Require booleans in `while` and `do while` loop conditions.`uselessCast`Detect useless casts — e.g. casting to `int` when the expression is already `int`.`numericOperandsInArithmeticOperators`Require numeric operands in `+`, `-`, `*`, `/`, `%`, `**`, `+$var`, `-$var`, `$var++`, `$var--`, `++$var` and `--$var`.`strictFunctionCalls`These functions contain a `$strict` parameter for better type safety, it must be set to `true`:
\* `in_array` (3rd parameter)
\* `array_search` (3rd parameter)
\* `array_keys` (3rd parameter; only if the 2nd parameter `$search_value` is provided)
\* `base64_decode` (2nd parameter).`overwriteVariablesWithLoop`\* Disallow overwriting variables with `foreach` key and value variables.
\* Disallow overwriting variables with `for` loop initial assignment.`switchConditionsMatchingType`Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.`dynamicCallOnStaticMethod`Check that statically declared methods are called statically.`disallowedEmpty`Disallow `empty()` - it's a very loose comparison (see [manual](https://php.net/empty)), it's recommended to use more strict one.`disallowedLooseComparison`Disallow loose comparison via `==` and `!=`.`disallowedShortTernary`Disallow short ternary operator (`?:`) - implies weak comparison, it's recommended to use null coalesce operator (`??`) or ternary operator with strict condition.`noVariableVariables`Disallow variable variables (`$$foo`, `$this->$method()` etc.).`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.Correct case for referenced and called function names.`matchingInheritedMethodNames`Correct case for inherited and implemented method names.Contravariance for parameter types and covariance for return types in inherited methods (also known as Liskov substitution principle - LSP).Check LSP even for static methods.`requireParentConstructorCall`Require calling parent constructor.`disallowedBacktick`Disallow usage of backtick operator (`$ls = `ls -la``).`disallowedImplicitArrayCreation`Disallow implicit array creation through `$var[] =` when the variable does not exist yet.`strictArrayFilter`Require `array_filter()` to have a callback parameter to avoid loose comparison semantics.`illegalConstructorMethodCall`Disallow calling `__construct()` on an existing object or as a static call outside of parent constructor.`closureUsesThis`Closure should use `$this` directly instead of using `$this` variable indirectly.Additional rules are coming in subsequent releases!

Installation
------------

[](#installation)

To use this extension, require it in [Composer](https://getcomposer.org/):

```
composer require --dev phpstan/phpstan-strict-rules

```

If you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!

 Manual installationIf you don't want to use `phpstan/extension-installer`, include rules.neon in your project's PHPStan config:

```
includes:
    - vendor/phpstan/phpstan-strict-rules/rules.neon

```

Disabling rules
---------------

[](#disabling-rules)

You can disable rules using configuration parameters:

```
parameters:
	strictRules:
		disallowedLooseComparison: false
		booleansInConditions: false
		booleansInLoopConditions: false
		uselessCast: false
		requireParentConstructorCall: false
		disallowedBacktick: false
		disallowedEmpty: false
		disallowedImplicitArrayCreation: false
		disallowedShortTernary: false
		overwriteVariablesWithLoop: false
		closureUsesThis: false
		matchingInheritedMethodNames: false
		numericOperandsInArithmeticOperators: false
		strictFunctionCalls: false
		dynamicCallOnStaticMethod: false
		switchConditionsMatchingType: false
		noVariableVariables: false
		strictArrayFilter: false
		illegalConstructorMethodCall: false
```

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

Enabling rules one-by-one
-------------------------

[](#enabling-rules-one-by-one)

If you don't want to start using all the available strict rules at once but only one or two, you can!

You can disable all rules from the included `rules.neon` with:

```
parameters:
	strictRules:
		allRules: false
```

Then you can re-enable individual rules with configuration parameters:

```
parameters:
	strictRules:
		allRules: false
		booleansInConditions: true
```

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

###  Health Score

79

—

ExcellentBetter than 100% of packages

Maintenance91

Actively maintained with recent releases

Popularity74

Solid adoption and visibility

Community60

Healthy contributor diversity

Maturity82

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 64.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~57 days

Recently: every ~38 days

Total

54

Last Release

81d ago

Major Versions

0.12.11 → 1.0.02021-10-11

1.6.1 → 2.0.02024-10-26

1.6.2 → 2.0.22025-01-19

PHP version history (5 changes)0.9PHP ~7.0

0.10PHP ~7.1

0.12.4PHP ^7.1 || ^8.0

1.2.0PHP ^7.2 || ^8.0

2.0.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/258cf2d2422f7e9ac5f7e1bb7a331f3fb717a11e51de7a3de2d6025507bd63cf?d=identicon)[ondrejmirtes](/maintainers/ondrejmirtes)

---

Top Contributors

[![ondrejmirtes](https://avatars.githubusercontent.com/u/104888?v=4)](https://github.com/ondrejmirtes "ondrejmirtes (199 commits)")[![renovate[bot]](https://avatars.githubusercontent.com/in/2740?v=4)](https://github.com/renovate[bot] "renovate[bot] (22 commits)")[![staabm](https://avatars.githubusercontent.com/u/120441?v=4)](https://github.com/staabm "staabm (11 commits)")[![renovate-bot](https://avatars.githubusercontent.com/u/25180681?v=4)](https://github.com/renovate-bot "renovate-bot (10 commits)")[![localheinz](https://avatars.githubusercontent.com/u/605483?v=4)](https://github.com/localheinz "localheinz (9 commits)")[![adaamz](https://avatars.githubusercontent.com/u/4347332?v=4)](https://github.com/adaamz "adaamz (8 commits)")[![ruudk](https://avatars.githubusercontent.com/u/104180?v=4)](https://github.com/ruudk "ruudk (5 commits)")[![VincentLanglet](https://avatars.githubusercontent.com/u/9052536?v=4)](https://github.com/VincentLanglet "VincentLanglet (4 commits)")[![herndlm](https://avatars.githubusercontent.com/u/5738896?v=4)](https://github.com/herndlm "herndlm (4 commits)")[![MartinMystikJonas](https://avatars.githubusercontent.com/u/2094752?v=4)](https://github.com/MartinMystikJonas "MartinMystikJonas (3 commits)")[![kukulich](https://avatars.githubusercontent.com/u/260445?v=4)](https://github.com/kukulich "kukulich (3 commits)")[![lookyman](https://avatars.githubusercontent.com/u/3863468?v=4)](https://github.com/lookyman "lookyman (3 commits)")[![Majkl578](https://avatars.githubusercontent.com/u/144181?v=4)](https://github.com/Majkl578 "Majkl578 (3 commits)")[![carusogabriel](https://avatars.githubusercontent.com/u/16328050?v=4)](https://github.com/carusogabriel "carusogabriel (2 commits)")[![mad-briller](https://avatars.githubusercontent.com/u/28307684?v=4)](https://github.com/mad-briller "mad-briller (2 commits)")[![Kocal](https://avatars.githubusercontent.com/u/2103975?v=4)](https://github.com/Kocal "Kocal (2 commits)")[![kamil-zacek](https://avatars.githubusercontent.com/u/17525718?v=4)](https://github.com/kamil-zacek "kamil-zacek (2 commits)")[![iluuu1994](https://avatars.githubusercontent.com/u/1752683?v=4)](https://github.com/iluuu1994 "iluuu1994 (2 commits)")[![nelson6e65](https://avatars.githubusercontent.com/u/9272498?v=4)](https://github.com/nelson6e65 "nelson6e65 (1 commits)")[![step-security-bot](https://avatars.githubusercontent.com/u/89328645?v=4)](https://github.com/step-security-bot "step-security-bot (1 commits)")

---

Tags

phpphp7phpstansafetystatic-analysisstatic-code-analysisstrongly-typedstatic analysis

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/phpstan-phpstan-strict-rules/health.svg)

```
[![Health](https://phpackages.com/badges/phpstan-phpstan-strict-rules/health.svg)](https://phpackages.com/packages/phpstan-phpstan-strict-rules)
```

###  Alternatives

[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k43.5M5.2k](/packages/larastan-larastan)[phpstan/phpstan-symfony

Symfony Framework extensions and rules for PHPStan

78768.9M1.5k](/packages/phpstan-phpstan-symfony)[phpstan/phpstan-doctrine

Doctrine extensions for PHPStan

66766.6M1.1k](/packages/phpstan-phpstan-doctrine)[phpstan/phpstan-phpunit

PHPUnit extensions and rules for PHPStan

529102.1M8.6k](/packages/phpstan-phpstan-phpunit)[spaze/phpstan-disallowed-calls

PHPStan rules to detect disallowed method &amp; function calls, constant, namespace, attribute, property &amp; superglobal usages, with powerful rules to re-allow a call or a usage in places where it should be allowed.

33320.0M375](/packages/spaze-phpstan-disallowed-calls)[phpstan/phpstan-deprecation-rules

PHPStan rules for detecting usage of deprecated classes, methods, properties, constants and traits.

44979.0M6.4k](/packages/phpstan-phpstan-deprecation-rules)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
