{"id":15024768,"url":"https://github.com/phpstan/phpstan-strict-rules","last_synced_at":"2025-05-13T21:08:15.479Z","repository":{"id":39351655,"uuid":"110360928","full_name":"phpstan/phpstan-strict-rules","owner":"phpstan","description":"Extra strict and opinionated rules for PHPStan","archived":false,"fork":false,"pushed_at":"2025-04-15T15:31:13.000Z","size":397,"stargazers_count":641,"open_issues_count":40,"forks_count":51,"subscribers_count":17,"default_branch":"2.0.x","last_synced_at":"2025-04-27T03:33:09.899Z","etag":null,"topics":["php","php7","phpstan","safety","static-analysis","static-code-analysis","strongly-typed"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phpstan.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-11T16:27:34.000Z","updated_at":"2025-04-25T18:17:04.000Z","dependencies_parsed_at":"2023-12-29T11:02:33.624Z","dependency_job_id":"f1c48b24-7ea7-44ff-a78d-a380d5bb0da9","html_url":"https://github.com/phpstan/phpstan-strict-rules","commit_stats":{"total_commits":267,"total_committers":32,"mean_commits":8.34375,"dds":"0.33333333333333337","last_synced_commit":"a4a6a08bd4a461e516b9c3b8fdbf0f1883b34158"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpstan%2Fphpstan-strict-rules","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpstan%2Fphpstan-strict-rules/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpstan%2Fphpstan-strict-rules/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phpstan%2Fphpstan-strict-rules/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phpstan","download_url":"https://codeload.github.com/phpstan/phpstan-strict-rules/tar.gz/refs/heads/2.0.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251254703,"owners_count":21559863,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","php7","phpstan","safety","static-analysis","static-code-analysis","strongly-typed"],"created_at":"2024-09-24T20:00:53.706Z","updated_at":"2025-04-28T15:24:02.628Z","avatar_url":"https://github.com/phpstan.png","language":"PHP","readme":"# Extra strict and opinionated rules for PHPStan\n\n[![Build](https://github.com/phpstan/phpstan-strict-rules/workflows/Build/badge.svg)](https://github.com/phpstan/phpstan-strict-rules/actions)\n[![Latest Stable Version](https://poser.pugx.org/phpstan/phpstan-strict-rules/v/stable)](https://packagist.org/packages/phpstan/phpstan-strict-rules)\n[![License](https://poser.pugx.org/phpstan/phpstan-strict-rules/license)](https://packagist.org/packages/phpstan/phpstan-strict-rules)\n\n[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:\n\n| Configuration Parameters               | Rule Description                                                                                        |\n|:---------------------------------------|:--------------------------------------------------------------------------------------------------------|\n| `booleansInConditions`                 | Require booleans in `if`, `elseif`, ternary operator, after `!`, and on both sides of `\u0026\u0026` and `\\|\\|`.  |\n| `booleansInLoopConditions`             | Require booleans in `while` and `do while` loop conditions.                                             |\n| `numericOperandsInArithmeticOperators` | Require numeric operands or arrays in `+` and numeric operands in `-`/`*`/`/`/`**`/`%`.                 |\n| `numericOperandsInArithmeticOperators` | Require numeric operand in `$var++`, `$var--`, `++$var`and `--$var`.                                    |\n| `strictFunctionCalls`                  | These functions contain a `$strict` parameter for better type safety, it must be set to `true`:\u003cbr\u003e* `in_array` (3rd parameter)\u003cbr\u003e* `array_search` (3rd parameter)\u003cbr\u003e* `array_keys` (3rd parameter; only if the 2nd parameter `$search_value` is provided)\u003cbr\u003e* `base64_decode` (2nd parameter). |\n| `overwriteVariablesWithLoop`           | Variables assigned in `while` loop condition and `for` loop initial assignment cannot be used after the loop.  |\n| `overwriteVariablesWithLoop`           | Variables set in foreach that's always looped thanks to non-empty arrays cannot be used after the loop. |\n| `switchConditionsMatchingType`         | Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to  unexpected results. |\n| `dynamicCallOnStaticMethod`            | Check that statically declared methods are called statically.                                           |\n| `disallowedEmpty`                      | Disallow `empty()` - it's a very loose comparison (see [manual](https://php.net/empty)), it's recommended to use more  strict one. |\n| `disallowedShortTernary`               | Disallow short ternary operator (`?:`) - implies weak comparison, it's recommended to use null coalesce operator (`??`)  or ternary operator with strict condition. |\n| `noVariableVariables`                  | Disallow variable variables (`$$foo`, `$this-\u003e$method()` etc.).                                         |\n| `overwriteVariablesWithLoop`           | Disallow overwriting variables with foreach key and value variables.                                    |\n| `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. |\n|                                        | Correct case for referenced and called function names.                                                  |\n| `matchingInheritedMethodNames`         | Correct case for inherited and implemented method names.                                                |\n|                                        | Contravariance for parameter types and covariance for return types in inherited methods (also known as Liskov substitution principle - LSP).|\n|                                        | Check LSP even for static methods.                                                                      |\n| `requireParentConstructorCall`         | Require calling parent constructor.                                                                     |\n| `disallowedBacktick`                   | Disallow usage of backtick operator (`` $ls = `ls -la` ``).                                             |\n| `closureUsesThis`                      | Closure should use `$this` directly instead of using `$this` variable indirectly.                       |\n\nAdditional rules are coming in subsequent releases!\n\n\n## Installation\n\nTo use this extension, require it in [Composer](https://getcomposer.org/):\n\n```\ncomposer require --dev phpstan/phpstan-strict-rules\n```\n\nIf you also install [phpstan/extension-installer](https://github.com/phpstan/extension-installer) then you're all set!\n\n\u003cdetails\u003e\n  \u003csummary\u003eManual installation\u003c/summary\u003e\n\nIf you don't want to use `phpstan/extension-installer`, include rules.neon in your project's PHPStan config:\n\n```\nincludes:\n    - vendor/phpstan/phpstan-strict-rules/rules.neon\n```\n\u003c/details\u003e\n\n## Disabling rules\n\nYou can disable rules using configuration parameters:\n\n```neon\nparameters:\n\tstrictRules:\n\t\tdisallowedLooseComparison: false\n\t\tbooleansInConditions: false\n\t\tbooleansInLoopConditions: false\n\t\tuselessCast: false\n\t\trequireParentConstructorCall: false\n\t\tdisallowedBacktick: false\n\t\tdisallowedEmpty: false\n\t\tdisallowedImplicitArrayCreation: false\n\t\tdisallowedShortTernary: false\n\t\toverwriteVariablesWithLoop: false\n\t\tclosureUsesThis: false\n\t\tmatchingInheritedMethodNames: false\n\t\tnumericOperandsInArithmeticOperators: false\n\t\tstrictFunctionCalls: false\n\t\tdynamicCallOnStaticMethod: false\n\t\tswitchConditionsMatchingType: false\n\t\tnoVariableVariables: false\n\t\tstrictArrayFilter: false\n\t\tillegalConstructorMethodCall: false\n```\n\nAside 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).\n\n## Enabling rules one-by-one\n\nIf you don't want to start using all the available strict rules at once but only one or two, you can!\n\nYou can disable all rules from the included `rules.neon` with:\n\n```neon\nparameters:\n\tstrictRules:\n\t\tallRules: false\n```\n\nThen you can re-enable individual rules with configuration parameters:\n\n```neon\nparameters:\n\tstrictRules:\n\t\tallRules: false\n\t\tbooleansInConditions: true\n```\n\nEven 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).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpstan%2Fphpstan-strict-rules","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphpstan%2Fphpstan-strict-rules","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphpstan%2Fphpstan-strict-rules/lists"}