Skip to content

Downgrade: how to handle memory_reset_peak_usage() (no polyfill possible) #9890

Description

@roxblnfk

Feature Request

Downgrading from PHP 8.2 to 8.1 currently leaves memory_reset_peak_usage() untouched, so the resulting code fatals on < 8.2 (Call to undefined function). I'd like to add a downgrade rule for it, but unlike array_is_list() / json_validate() / stream_isatty() this function cannot be polyfilled: it resets the engine's internal memory peak counter, and there is no userland API to do that before 8.2. Any downgrade therefore silently drops the semantics (a later memory_get_peak_usage() returns the non-reset peak). Because that's a behavior/policy question, I'd like maintainer guidance before implementing.

Option A — function_exists() guard, no-op fallback

-memory_reset_peak_usage();
+if (function_exists('memory_reset_peak_usage')) {
+    memory_reset_peak_usage();
+}

Runs on 8.2+, no-op on older PHP. Consistent with the existing function_exists idiom, but silently loses the peak reset. Simpler than the polyfill rules — the function is void and takes no arguments, so no closure/snippet is needed, just the guard.

Option B — don't add a rule

Leave the call as-is. The function is purely diagnostic/profiling, so arguably it's more honest to let the developer decide than to silently change memory-monitoring behavior.

Question

Would you accept Option A, prefer no rule, or have another shape in mind? Happy to open the PR once the direction is clear.

For reference, the semantically-clean sibling case ReflectionMethod::hasPrototype() (also 8.2, but polyfillable via getPrototype() + try/catch) is already up as rectorphp/rector-downgrade-php#398.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions