samlev/recursion-guard

一个简单的、零依赖的机制,用于防止PHP中的无限递归

v0.2.0 2024-09-30 07:30 UTC

This package is auto-updated.

Last update: 2024-09-30 07:35:23 UTC


README

一个简单的、零依赖的机制,用于防止PHP中的无限递归。

Build Status Total Downloads Latest Stable Version License

安装

composer require samlev/recursion-guard

用法

您可以通过使用RecursionGuard\Recurser::call()来执行回调并提供默认结果,防止函数在回调中递归调用。

function bozo_repeat(string $repeat = ''): string
{
    return RecursionGuard\Recurser::call(
        // The callback that we want to call
        fn () => bozo_repeat() . ' : ' . bozo_repeat(),
        // What to return if this function is called recursively
        $repeat ?: 'bozo(' . random_int(0, 100)) . ')';
    );
}

bozo_repeat(); // 'bozo(4) : bozo(4)'
bozo_repeat('foo'); // 'foo : foo'
bozo_repeat(); // 'bozo(88) : bozo(88)'

请参阅文档以获取更多解释、示例和高级用法。

测试

您可以使用composer命令运行单个测试套件

# Static Analysis with phpstan
composer test:stan

# Architecture tests
composer test:arch
# Documentation tests (tests that cover any code in the documentation)
composer test:docs
# Feature/integration tests
composer test:feat
# Unit tests
composer test:unit

# Code coverage
composer test:coverage

# Mutation tests
composer test:mutate

或者运行分组测试

# All code style checks
composer lint

# Static analysis and main test suites
composer test

代码风格

此项目的代码风格遵循PSR-12,可以使用以下composer命令进行检查

# Code Style checks with phpcs
composer lint:phpcs
# Code Style checks with PHP-CS-Fixer
composer lint:phpcsfixer

# Run all code style checks
composer lint

您可以使用命令尝试自动修复大量代码风格问题

composer lint:fix

贡献

通过PR的贡献是受欢迎的,无论是完整的更改(包括测试),还是失败测试用例。