spaze/phpstan-disallowed-calls
Composer 安装命令:
composer require --dev spaze/phpstan-disallowed-calls
包简介
PHPStan rules to detect disallowed method & function calls, constant, namespace, attribute, property & superglobal usages, with powerful rules to re-allow a call or a usage in places where it should be allowed.
关键字:
README 文档
README
PHPStan rules to detect disallowed calls and more, without running the code.
There are some functions, methods, constants, namespaces, attributes, variables and properties which should not be used in production code. One good example is the var_dump() function,
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 the foo() function (or any other supported items like constants or method calls etc.) 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
- Allow in classes, child classes, classes implementing an interface (same as the
instanceofoperator) - Allow in class with given attributes
- Allow in methods or functions with given attributes
- Allow in class with given attributes on any method
- Allow in type hint positions (param types, return types -
disallowedNamespacesanddisallowedClassesonly)
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
- For Symfony
spaze/phpstan-disallowed-calls 适用场景与选型建议
spaze/phpstan-disallowed-calls 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.31M 次下载、GitHub Stars 达 333, 最近一次更新时间为 2018 年 10 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「static analysis」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spaze/phpstan-disallowed-calls 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spaze/phpstan-disallowed-calls 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spaze/phpstan-disallowed-calls 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Sentiment analysis library for PHP.
Database Standardization and Analysis Tool for Laravel
Enforce architecture by defining modules with allowed dependencies. Detects forbidden, uncovered, missing and unused module dependencies in PHP projects.
Arc5 provides a set of static classes for the Mvc5 Framework
统计信息
- 总下载量: 22.31M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 335
- 点击次数: 26
- 依赖项目数: 409
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2018-10-31