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

Nsa Laravel Package

nyholm/nsa

Access non-public properties and methods in PHP for testing and debugging. Nyholm/nsa provides a tiny helper to bypass visibility restrictions (private/protected) without changing production code, making it easy to inspect internals and call hidden methods safely.

View on GitHub
Deep Wiki
Context7

Getting Started

nyholm/nsa is a lightweight testing utility focused on simplifying test code by providing quick, no-boilerplate access to PHP's internal state—particularly reflection, closures, and private/protected members—without relying on reflection manually. To start:

  • Require it via composer require --dev nyholm/nsa
  • In your tests, use the Nsa class directly: use Nsa\Nsa;
  • First use case: accessing private properties on objects for assertions:
    $user = new User('Alice');
    $name = Nsa::getProperty($user, 'name'); // returns 'Alice' even if private
    $this->assertEquals('Alice', $name);
    

Implementation Patterns

  • Property Access: Bypass visibility modifiers with Nsa::getProperty() and Nsa::setProperty()—ideal for testing internal state without changing it to public.
  • Method Invocation: Call private/protected methods via Nsa::getMethod($object, 'methodName')->invoke($object, ...).
  • Closure Creation: Use Nsa::getClosure($object, 'methodName') to get a reusable closure for repeated calls (e.g., in data providers).
  • Namespace & Trait Helpers: When testing traits, Nsa::getNamespace() and Nsa::getTraits() help introspect classes dynamically.
  • Integration: Commonly used in unit tests ( PHPUnit, Pest) where real behavior must be verified without refactoring production code. Often paired with test doubles to assert calls to internal methods.

Gotchas and Tips

  • Performance: Avoid repeated calls to Nsa::getMethod() in loops—cache the method closure instead:
    $method = Nsa::getMethod($service, 'compute');  
    $result = $method->invoke($service, $input);
    
  • PHP Version: Requires PHP 7.0+; uses ReflectionMethod, ReflectionProperty, and closures under the hood.
  • Not for Production: Strictly a dev dependency—do not autoload in production. Ensure nyholm/nsa is listed under require-dev in composer.json.
  • Limited Scope: Only handles objects and class methods/properties; not designed for static properties or global functions (use other tools like Roave\BetterReflection for advanced cases).
  • Typical Bug: Forgetting to pass the object instance when invoking methods—e.g., $method->invoke(null) fails. Always use Nsa::getMethod($object, 'foo')$method->invoke($object, ...).
  • Extensibility: The package is tiny (~200 LoC)—consider extending it with your own helper like Nsa::invoke($obj, 'method', ...) to streamline common patterns.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport