定制 spaze/phpstan-disallowed-calls 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.

PHP Tests

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:

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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 spaze/phpstan-disallowed-calls 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 22.31M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 335
  • 点击次数: 26
  • 依赖项目数: 409
  • 推荐数: 2

GitHub 信息

  • Stars: 333
  • Watchers: 5
  • Forks: 23
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-31