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

Variadic Extension Laravel Package

friends-of-behat/variadic-extension

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require friends-of-behat/variadic-extension --dev
    

    Add to behat.yml:

    extensions:
        FriendsOfBehat\VariadicExtension: ~
    
  2. First Use Case: Replace repetitive step definitions like:

    /**
     * @When I have :count items
     */
    public function iHaveOneItem() { ... }
    
    /**
     * @When I have :count items
     */
    public function iHaveTwoItems($count) { ... }
    

    With a single variadic method:

    /**
     * @When I have :count items
     */
    public function iHaveItems($count, ...$additionalArgs) { ... }
    

Implementation Patterns

Core Workflow

  1. Step Definition Consolidation: Use variadic arguments (...$args) to handle dynamic argument counts:

    /**
     * @When I perform action :action on :target with :options
     */
    public function performAction($action, $target, ...$options) {
        if (empty($options)) {
            // Handle minimal args
        } else {
            // Process additional args
        }
    }
    
  2. Integration with Laravel:

    • Service Container: Inject dependencies via Laravel's DI:
      public function __construct(private UserRepository $users) {}
      
    • Context Initialization: Use Laravel's app() helper in contexts:
      $this->users = app(UserRepository::class);
      
  3. Type Safety: Combine with PHP 7.4+ typed properties or return types:

    public function calculateTotal(float ...$prices): float { ... }
    

Advanced Patterns

  • Argument Validation:
    public function processItems(...$items) {
        if (count($items) < 1) {
            throw new \InvalidArgumentException("At least one item required");
        }
    }
    
  • Default Values:
    public function configure($name, ...$config) {
        $defaults = ['timeout' => 30];
        $merged = array_merge($defaults, $config);
    }
    

Gotchas and Tips

Common Pitfalls

  1. Argument Order Sensitivity: Variadic args must be last in the method signature. This will fail:

    // ❌ Wrong
    public function example(...$args, $required) { ... }
    
  2. Behat Step Matching:

    • Overly generic step definitions may cause ambiguity. Prefix with context:
      @user When I have :count items
      
    • Use @Given/@When/@Then consistently to avoid conflicts.
  3. Performance:

    • Avoid complex logic in step definitions. Offload to services:
      $this->userService->process(...$args);
      

Debugging Tips

  • Enable Verbose Output:

    behat --verbose
    

    Reveals argument parsing details.

  • Check Step Registry:

    behat --dry-run
    

    Validates step definitions before execution.

Extension Points

  1. Custom Argument Parsers: Extend FriendsOfBehat\VariadicExtension\ArgumentParser for domain-specific types.

  2. Laravel-Specific:

    • Service Binding: Bind Behat contexts to Laravel's container:
      $this->app->bind(FeatureContext::class, function () {
          return new FeatureContext(app(UserRepository::class));
      });
      
    • Environment Awareness: Use Laravel's config in contexts:
      $this->app['config']['behat.timeout'];
      
  3. Testing:

    • Mock variadic args in PHPUnit:
      $this->context->processItems(...['item1', 'item2']);
      
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware