spaze / phpstan-disallowed-calls
PHPStan rules to detect disallowed method & function calls, constant, namespace & superglobal usages
Fund package maintenance!
spaze
Installs: 10 361 773
Dependents: 262
Suggesters: 0
Security: 0
Stars: 264
Watchers: 6
Forks: 17
Open Issues: 3
Type:phpstan-extension
Requires
- php: ^7.4 || ^8.0
- phpstan/phpstan: ^1.12.6 || ^2.0
Requires (Dev)
- nette/neon: ^3.3.1
- nikic/php-parser: ^4.13 || ^5.0
- php-parallel-lint/php-console-highlighter: ^1.0
- php-parallel-lint/php-parallel-lint: ^1.2
- phpstan/phpstan-deprecation-rules: ^1.2 || ^2.0
- phpunit/phpunit: ^8.5.14 || ^10.1 || ^11.0
- spaze/coding-standard: ^1.7
- dev-main
- v4.0.1
- v4.0.0
- v3.5.1
- v3.5.0
- v3.4.0
- v3.3.0
- v3.2.0
- v3.1.2
- v3.1.1
- v3.0.0
- v2.16.1
- v2.16.0
- v2.15.1
- v2.15.0
- v2.14.2
- v2.14.1
- v2.13.0
- v2.12.1
- v2.12.0
- v2.11.6
- v2.11.5
- v2.11.4
- v2.11.3
- v2.11.2
- v2.11.1
- v2.11.0
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.5.0
- v2.4.0
- v2.3.2
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v1.11.0
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.2
- v1.2.1
- v1.1.0
- v1.0.1
- v1.0.0
- v0.12.7
- v0.12.6
- v0.12.5
- v0.12.4
- v0.12.3
- v0.12.2
- v0.12.1
- v0.12.0
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- dev-spaze/first-class-callable-syntax
- dev-spaze/dynamic-calls
- dev-spaze/test-anonymous-class
- dev-spaze/dynamic-function-calls
- dev-spaze/allow-in-attributes
This package is auto-updated.
Last update: 2024-12-08 05:14:04 UTC
README
PHPStan rules to detect disallowed calls and more, without running the code.
There are some functions, methods, and constants which should not be used in production code. One good example is var_dump()
,
it is often used to quickly debug problems but should be removed before committing the code. And sometimes it's not.
Another example would be a generic logger. Let's say you're using one of the generic logging libraries but you have your own logger that will add some more info, or sanitize data, before calling the generic logger. Your code should not call the generic logger directly but should instead use your custom logger.
This PHPStan extension will detect such usage, if configured. It should be noted that this extension is not a way to defend against or detect hostile developers, as they can obfuscate the calls for example. This extension is meant to be another pair of eyes, detecting your own mistakes, it doesn't aim to detect-all-the-things.
Tests will provide examples what is currently detected. If it's not covered by tests, it might be, but most probably will not be detected.
*Test.php
files are the tests, start with those, the analyzed test code is in src, required test classes in libs.
Feel free to file issues or create pull requests if you need to detect more calls.
Installation
Install the extension using Composer:
composer require --dev spaze/phpstan-disallowed-calls
PHPStan, the PHP Static Analysis Tool, is a requirement.
If you use phpstan/extension-installer, you are all set and can skip to configuration.
For manual installation, add this to your phpstan.neon
:
includes: - vendor/spaze/phpstan-disallowed-calls/extension.neon
Configuration files
You can start with bundled configuration files.
Custom rules
The extension supports versatile custom rules, too.
Allow some previously disallowed calls or usages
Let's say you have disallowed foo()
with custom rules. But you want to re-allow it when used in your custom wrapper, or when the first parameter equals, or not, a specified value. The extension offers multiple ways of doing that:
- Ignore errors the PHPStan way
- Allow in paths
- Allow in methods or functions
- Allow with specified parameters
- Allow with specified flags
Re-allowing attributes uses a similar configuration.
Disallow disabled functions & classes
Use the provided generator to generate a configuration snippet from PHP's disable_functions
& disable_classes
configuration directives.
Example output
------ --------------------------------------------------------
Line libraries/Report/Processor/CertificateTransparency.php
------ --------------------------------------------------------
116 Calling var_dump() is forbidden, use logger instead
------ --------------------------------------------------------
Case-(in)sensitivity
Function names, method names, class names, namespaces are matched irrespective of their case (disallowing print_r
will also find print_R
calls), while anything else like constants, file names, paths are not.
No other rules
You can also use this extension without any other PHPStan rules. This may be useful if you want to for example check a third-party code for some calls or usage of something.
Running tests
If you want to contribute (awesome, thanks!), you should add/run tests for your contributions.
First install dev dependencies by running composer install
, then run PHPUnit tests with composer test
, see scripts
in composer.json
. Tests are also run on GitHub with Actions on each push.
You can fix coding style issues automatically by running composer cs-fix
.
See also
There's a similar project with a slightly different configuration, created almost at the same time (just a few days difference): PHPStan Banned Code.
Framework or package-specific configurations
- For Nette Framework