Skip to content

Improved getters - #57

Merged
petrknap merged 1 commit into
mainfrom
refactored-getters
Oct 24, 2026
Merged

petrknap merged 1 commit into
mainfrom
refactored-getters

Conversation

@petrknap

@petrknap petrknap commented Oct 24, 2026 •

Copy link
Copy Markdown
Owner
  • This pull request does not contain any breaking change.
  • I asked the AI to briefly summarize, use and improve the feature(s), and I'm okay with the output:

Overview of Changes

This pull request removes a previously introduced enforcement mechanism that triggered a notice (E_USER_NOTICE) if get() was called on an Optional without first checking isPresent(). Additionally, it cleans up and enhances tests (including alignment changes in Java test specifications, renaming Another to Alternative, and expanding conditional type annotations for orElseThrow()).


Detailed Review by File

1. src/Optional.php

  • Removal of wasPresent Tracking:

  • Deleted the wasPresent tracking property (private bool|null $wasPresent = null), the check inside get(), the state update inside isPresent(), and the triggerNotice() helper method.

  • Impact: Calling get() directly will no longer trigger a user notice advising developers to call isPresent() first. This simplifies the codebase and aligns more closely with standard Java Optional behavior (which simply throws an exception if the value is missing, rather than generating warnings).

  • Improved PhpDoc:

  • Refined the return/throws type annotation for orElseThrow() to accurately reflect union types based on whether an explicit exception supplier is provided:
    @throws ($exceptionSupplier is null ? Exception\CouldNotGetValueOfEmptyOptional : E)

2. Tests (tests/JavaSe8/ & tests/OptionalTest.php)

  • Test Renaming & Refactoring:

  • Renamed AnotherOption.php to AlternativeOption.php and updated references across Java test cases (OptionalTest.java and OptionalTestCase.php).

  • Expanded Test Assertions (OptionalTest.java):

  • Added more robust assertions verifying fallback values, orElseGet(), handling of null fallbacks, and comprehensive exception throwing (NoSuchElementException and AlternativeException).


Conclusion & Recommendation

  • Code Quality: Excellent. The code is clean, well-tested, and maintains strict static analysis compliance (phpstan, phpcs).
  • Design Decision: Removing the strict notice for calling get() without isPresent() makes the library less intrusive and more aligned with expected Java-style semantics where exceptions handle empty states.
  • Verdict: Approved ✅ (Ready to merge, assuming CI checks and test suites pass successfully).

@petrknap petrknap self-assigned this Oct 24, 2026
@petrknap petrknap added the documentation Improvements or additions to documentation label Oct 24, 2026
@petrknap
petrknap marked this pull request as ready for review October 24, 2026 08:40
@petrknap
petrknap merged commit 3899da2 into main Oct 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant