Skip to content

Fix Integer::fromNumeric bypassing subclass validation - #7

Open
MisterIcy wants to merge 3 commits into
mainfrom
fix/integer-from-numeric-late-static-binding
Open

MisterIcy wants to merge 3 commits into
mainfrom
fix/integer-from-numeric-late-static-binding

Conversation

@MisterIcy

Copy link
Copy Markdown
Member

Problem

Integer::fromNumeric() used new self, so it always returned a plain Integer and skipped subclass validation. PositiveInteger::fromNumeric('-42') and PositiveInteger::fromNumeric('0') succeeded, as did NonNegativeInteger::fromNumeric('-1').

Fix

Use new static. The declared return type stays self (project targets PHP 7.4).

Tests

  • Positive / NonNegative: fromNumeric returns the called class; rejects invalid values (negative string, negative float, zero for Positive); NonNegative accepts '0'.
  • Integer: still accepts negatives, returns exactly Integer, rejects non-numeric strings.

Full suite passes (55 tests).

fromNumeric() used new self, so PositiveInteger::fromNumeric('-42') and
NonNegativeInteger::fromNumeric('-1') returned a plain Integer without
running the subclass constructor checks. Use new static instead.

Add tests covering fromNumeric for Integer, PositiveInteger and
NonNegativeInteger.
Bump the composer platform to 8.0 and declare a php >=8.0 requirement.
Refresh composer.lock content hash.
Drop PHP 7.4 from the matrix, add 8.4 and 8.5, and run mutation tests
on every version now that the 7.4 exclusion is moot.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant