staabm/side-effects-detector
Composer 安装命令:
composer require --dev staabm/side-effects-detector
包简介
A static analysis tool to detect side effects in PHP code
关键字:
README 文档
README
Analyzes php-code for side-effects.
When code has no side-effects it can e.g. be used with eval($code) in the same process without interfering.
Side-effects are classified into categories to filter them more easily depending on your use-case.
This library is used e.g. in PHPUnit to improve performance of PHPT test-cases.
Install
composer require staabm/side-effects-detector
Usage
Example:
use staabm\SideEffectsDetector\SideEffectsDetector; $code = '<?php version_compare(PHP_VERSION, "8.0", ">=") or echo("skip because attributes are only available since PHP 8.0");'; $detector = new SideEffectsDetector(); // [SideEffect::STANDARD_OUTPUT] var_dump($detector->getSideEffects($code));
In case functions are called which are not known to have side-effects - e.g. userland functions - null is returned.
use staabm\SideEffectsDetector\SideEffectsDetector; $code = '<?php userlandFunction();'; $detector = new SideEffectsDetector(); // [SideEffect::MAYBE] var_dump($detector->getSideEffects($code));
Code might have multiple side-effects:
use staabm\SideEffectsDetector\SideEffectsDetector; $code = '<?php include "some-file.php"; echo "hello world"; exit(1);'; $detector = new SideEffectsDetector(); // [SideEffect::SCOPE_POLLUTION, SideEffect::STANDARD_OUTPUT, SideEffect::PROCESS_EXIT] var_dump($detector->getSideEffects($code));
Compensate some side-effects
It might be useful to compensate some side-effects, so evaluation of code in the current process is still acceptable:
use staabm\SideEffectsDetector\SideEffectsDetector; function runCodeInLocalSandbox(string $code): string { $code = preg_replace('/^<\?(?:php)?|\?>\s*+$/', '', $code); $code = preg_replace('/declare\S?\([^)]+\)\S?;/', '', $code); // wrap in immediately invoked function to isolate local-side-effects // of $code from our own process $code = '(function() {' . $code . '})();'; // wrap in output buffer to isolate stdout side-effects ob_start(); @eval($code); return ob_get_clean(); } function shouldRunInSubprocess(string $code): bool { $detector = new SideEffectsDetector; $sideEffects = $detector->getSideEffects($code); if ($sideEffects === []) { return false; // no side-effects } foreach ($sideEffects as $sideEffect) { // stdout is fine, we will catch it using output-buffering if ($sideEffect === SideEffect::STANDARD_OUTPUT) { continue; } return true; } return false; } function runCode(string $code) { if (!shouldRunInSubprocess($code)) { return runCodeInLocalSandbox($code); } // run $code in isolation, e.g. in a subprocess // ... }
Disclaimer
Non goals are:
- find the best possible answer for all cases
- add runtime dependencies
- inspect additional metadata like attributes or phpdoc tags
If you are in need of a fully fledged side-effect analysis, use more advanced tools like PHPStan.
Look at the test-suite to get an idea of supported use-cases.
staabm/side-effects-detector 适用场景与选型建议
staabm/side-effects-detector 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97.83M 次下载、GitHub Stars 达 154, 最近一次更新时间为 2024 年 09 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「static analysis」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 staabm/side-effects-detector 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 staabm/side-effects-detector 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 staabm/side-effects-detector 相关的其它包
同方向 / 同关键字的高下载量 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
统计信息
- 总下载量: 97.83M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 154
- 点击次数: 16
- 依赖项目数: 15
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-28