Skip to content

PHP 8.3: Randomizer Additions #1992

@afilina

Description

@afilina

Related to #1589

Analysis

This analysis covers two related changes.

PHP 8.3 adds new methods to the existing \Random\Randomizer class:

final class Random\Randomizer {
    // ...
    public getBytesFromString(string $string, int $length): string
    public nextFloat(): float
    public nextInt(): int
}

PHP 8.3 adds a new enum \Random\IntervalBoundary:

enum IntervalBoundary {
    case ClosedOpen;
    case ClosedClosed;
    case OpenClosed;
    case OpenOpen;
}
  • The \Random\IntervalBoundary class name becomes reserved
  • The RFC also mentions that the random extension reserves the \Random namespace. This is not new and doesn't emit warnings, but perhaps still worth flagging any classes defined in that namespace.

Top 2000 Packages

The only backward-incompatible change is the reserved name, so I looked for all declarations of the \Random namespace, and the only hits I got were stubs in static analysis tools, which is expected and not an issue. Example occurrence locations:

  • /phpstan/php-8-stubs/stubs/ext/random/Random/IntervalBoundary.php
  • /jetbrains/phpstorm-stubs/random/random.php
  • /symfony/polyfill-php82/Resources/stubs/Random/Engine.php

Detection in PHP 8.1

  • \Random: ✅ Not yet introduced in that version

Detection in PHP 8.2

  • Any existing class declarations in \Random namespace: ❌ Existing class
  • Any other class declarations in \Random namespace: ⚠️ Reserved namespace (perhaps no need to check for IntervalBoundary specifically)
  • Usage of the \Random\IntervalBoundary enum: ❌ Doesn't exist yet
  • Randomizer::getBytesFromString: ❌ Doesn't exist yet
  • Randomizer::nextFloat: ❌ Doesn't exist yet
  • Randomizer::nextInt: ❌ Doesn't exist yet

Detection in PHP 8.3

  • Declaration of a \Random\IntervalBoundary class: ❌ Existing class
  • Any other class declarations in \Random namespace: ⚠️ Reserved namespace
  • Usage of the \Random\IntervalBoundary enum: ✅ Valid
  • Usage of \Random\IntervalBoundary cases : ✅ Valid
  • Randomizer::getBytesFromString: ✅ Valid
  • Randomizer::nextFloat: ✅ Valid
  • Randomizer::nextInt: ✅ Valid

Syntax Variations

Detectability

  • Class declarations inside a specific namespace: ✅ Detectable with PHPCompatibility
  • Usage of the IntervalBoundary enum: ✅ Mostly feasible, except when the classname becomes dynamic.
  • Usage of the IntervalBoundary enum cases: ⚠️ Since we can assign an enum to a variable, we can lose track, so not all scenarios can be detected.
  • Usage of new Randomizer methods: ⚠️ Since we can assign the instance to a variable, we can lose track, so not all scenarios can be detected.

Examples in "Syntax Variations" above.

References

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