Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Bypass Finals Laravel Package

dg/bypass-finals

DG\BypassFinals lets you bypass PHP’s final classes and methods at runtime so you can mock, extend, or patch code that’s otherwise locked down—useful for testing legacy dependencies. Lightweight, Composer-ready, and works with popular test frameworks.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Testing Legacy/Third-Party Dependencies: Enables mocking of final classes/methods in vendor libraries (e.g., Symfony, Laravel core, or legacy codebases) without forking or refactoring, directly supporting testability as a non-functional requirement.
  • CI/CD Reliability: Eliminates flaky tests caused by unmockable final constructs in integration tests, reducing false negatives in pipelines and improving release velocity.
  • Build vs. Buy Decision: Avoids custom solutions (e.g., manual eval() hacks or test-specific code branches) that introduce technical debt and maintenance overhead.
  • Roadmap for Modernization: Justifies incremental refactoring by reducing urgency to immediately remove final keywords from legacy code, aligning with strategic debt reduction.
  • Laravel-Specific Use Cases:
    • Testing final classes in Laravel’s core (e.g., Illuminate\Foundation\Application) or third-party packages (e.g., spatie/laravel-permission).
    • Mocking final methods in payment gateways (e.g., Stripe SDK) or authentication providers during unit testing.
    • Enabling feature flags or plugin architectures where final classes block dependency injection in tests.

When to Consider This Package

Adopt When:

  • Testing Blockers Exist: Your test suite is gated by final classes/methods in vendor code or legacy systems, preventing mocking or dependency injection.
  • Vendor Lock-In: You rely on third-party libraries (e.g., SDKs, frameworks) that declare critical classes as final, making them untestable without this tool.
  • Legacy Modernization: You’re incrementally refactoring a codebase but need to test old code before removing final keywords.
  • PHPUnit/Mockery Integration: Your team already uses these tools and wants a native, maintainable solution over manual workarounds.
  • CI/CD Stability: Flaky tests due to unmockable final constructs are causing regression risks or delayed releases.

Avoid When:

  • Refactoring Is Feasible: If you can remove final keywords via clean code reviews or PRs, prioritize that over runtime bypasses (this package is a temporary solution).
  • Production Use: Never enable this in live environments—it violates type safety and could break runtime behavior (e.g., readonly properties in PHP 8+).
  • Legal Constraints: The NOASSERTION license may pose risks for commercial projects; consult legal if unsure.
  • Alternatives Exist: For PHP 8.2+, explore native traits or interface-based designs to replace final dependencies.
  • High-Maintenance Risk: If your team lacks test environment isolation (e.g., accidental production deployment), this package introduces operational risk.

How to Pitch It (Stakeholders)

For Executives:

*"This package resolves a critical bottleneck in our testing pipeline: unmockable final classes in third-party libraries and legacy code are blocking test coverage for key integration points. By enabling mocking without altering vendor code, we can:

  • Reduce production risks by ensuring comprehensive test coverage for dependencies like Stripe SDKs or Laravel core.
  • Accelerate feature delivery by unblocking CI/CD for teams waiting on test stability.
  • Lower technical debt by avoiding custom hacks or forks of vendor libraries. It’s a low-risk, high-reward investment—strictly for tests, with zero impact on production."*

For Engineering Teams:

*"This solves a painful but common problem: testing code that depends on final classes (e.g., Symfony’s Request, Laravel’s Application, or vendor SDKs). Here’s how we’ll use it:

  • Install once, use everywhere: Add to composer.json as a dev dependency and configure it in phpunit.xml or tests/bootstrap.php.
  • Seamless integration: Works with PHPUnit, Mockery, or Pest—just mock final classes like any other, e.g., $mock = $this->createMock(FinalClass::class).
  • Safety first: Only runs in tests/CI—we’ll add environment checks to prevent accidental production use.
  • Future-proofing: While we’ll eventually refactor final keywords, this lets us test today and clean up later. Tradeoff: Minimal CI overhead (~5–10% slower tests) for unblocked test coverage—worth it for critical paths."*

For Architects/Tech Leads:

*"Key considerations:

  1. Scope: This is a test-only tool—we must enforce environment isolation (e.g., fail builds if loaded outside testing).
  2. Risk Mitigation:
    • Disable OPcache in tests to avoid bytecode conflicts.
    • Monitor for false positives (e.g., bypassed final behavior differing from production).
  3. Alternatives: If we see high maintenance costs (e.g., PHP 8.2+ readonly quirks), we’ll reassess.
  4. Long-Term: Use this as a temporary bridge while we refactor final dependencies in phases. Proposal: Pilot with 1–2 high-impact test suites (e.g., payment gateway integration) before rolling out broadly."*
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope