interitty/infinite-loop-prevention

A simple infinite loop prevention tool.

v1.0.5 2024-05-19 20:27 UTC

This package is auto-updated.

Last update: 2024-05-19 18:28:12 UTC


README

A simple infinite loop prevention tool that help developers to prevent the error Maximum function nesting level of '256' reached, aborting!.

Requirements

Installation

The best way to install interitty/infinite-loop-prevention is using Composer:

composer require interitty/infinite-loop-prevention

Usage

For cases when an infinite loop can happen, especially when code work with a given callback, it can be handy to call preventive assertion in a non-production environment just to be sure, that code can't fall into an infinite loop.

Because the code is based on debug_backtrace, it will be better to disable the mechanism on the production environment by setting the static InfiniteLoopPrevention::$productionMode variable.

For better performance in the production environment, it will be better to use the assert, because the execution and whole code generating can be disabled by the zend.assertions in the php.ini file.

It only needs to call the static method without any parameters just inside the method or function where the given callback is called.

Example: Usage of checkFinite()


function test(): void
{
    assert(InfiniteLoopPrevention::checkFinite());
    test();
}

test();