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

Polyfill Php73 Laravel Package

symfony/polyfill-php73

Symfony Polyfill for PHP 7.3: provides fallback implementations of core features for older PHP versions, including array_key_first/last, hrtime, is_countable, and JsonException, ensuring consistent behavior across environments.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require symfony/polyfill-php73
    

    Add to composer.json under require:

    "symfony/polyfill-php73": "^1.37.0"
    
  2. First Use Case: Replace legacy array traversal with array_key_first in a Laravel controller:

    // Before (PHP <7.3)
    $firstKey = array_search($value, $array) ?: reset($array);
    
    // After (works on PHP <7.3 with polyfill)
    $firstKey = array_key_first($array);
    
  3. Verify Compatibility: Check PHP version in composer.json:

    "config": {
        "platform": {
            "php": "7.2"
        }
    }
    

Where to Look First


Implementation Patterns

Usage Patterns

  1. Array Introspection:

    // Eloquent: Get first key of attributes
    $firstAttribute = array_key_first($model->getAttributes());
    
    // Collections: Optimize array operations
    $lastKey = array_key_last($request->all());
    
  2. Error Handling:

    try {
        $data = json_decode($json, false, 512, JSON_THROW_ON_ERROR);
    } catch (JsonException $e) {
        Log::error("Invalid JSON: " . $e->getMessage());
    }
    
  3. Performance Metrics:

    $start = hrtime(true);
    // Code to benchmark
    $duration = hrtime(true) - $start;
    
  4. Countable Checks:

    if (is_countable($items)) {
        $count = count($items);
    }
    

Workflows

  1. Phased Migration:

    • Use JsonException in API routes now, upgrade PHP later.
    • Example: Replace json_decode hacks with JSON_THROW_ON_ERROR.
  2. Package Development:

    • Target PHP 7.2+ in composer.json but use array_key_first internally.
    • Example: Laravel package using hrtime for benchmarking.
  3. Cross-Environment Consistency:

    • Standardize array_key_first usage across dev/staging/prod (PHP 7.2–8.x).

Integration Tips

  • Laravel Collections: Extend Illuminate\Support\Collection to use polyfilled functions:

    $firstKey = $collection->keys()->first();
    // Internally uses array_key_first if polyfill is loaded.
    
  • Service Providers: Autoload polyfills conditionally:

    if (version_compare(PHP_VERSION, '7.3.0', '<')) {
        require __DIR__.'/../../vendor/autoload_files.php';
    }
    
  • Testing: Mock polyfills in PHPUnit:

    $this->getMockBuilder('Symfony\Polyfill\Php73\Php73')
         ->disableOriginalConstructor()
         ->onlyMethods(['array_key_first'])
         ->getMock();
    

Gotchas and Tips

Pitfalls

  1. Function Overrides:

    • Polyfills may conflict with custom implementations of array_key_first or JsonException.
    • Fix: Audit codebase for redefined functions before installing.
  2. PHP Version Mismatch:

    • Deploying on PHP <7.0 will fail (polyfill requires PHP 5.3.2+).
    • Fix: Enforce php:7.0 minimum in composer.json.
  3. Unused Polyfills:

    • Polyfills load even if unused, adding minor overhead.
    • Fix: Use PHPStan/Psalm to detect dead code:
      vendor/bin/phpstan analyse --level 5
      
  4. JsonException Incompatibility:

    • Some libraries expect JsonException to extend RuntimeException.
    • Fix: Check JsonException inheritance:
      if (!($e instanceof \JsonException)) {
          // Fallback for older libraries
      }
      

Debugging

  • Verify Polyfill Load:

    if (!function_exists('array_key_first')) {
        throw new \RuntimeException('Polyfill not loaded!');
    }
    
  • Check PHP Version:

    composer validate --strict
    
  • Isolate Issues: Test polyfills in a fresh Laravel install:

    composer create-project laravel/laravel test-polyfill
    cd test-polyfill
    composer require symfony/polyfill-php73
    

Tips

  1. Performance:

    • hrtime is ~10x more precise than microtime(true) but adds ~0.1ms overhead.
    • Tip: Cache hrtime results for benchmarking:
      static $start = hrtime(true);
      
  2. Laravel-Specific:

    • Use array_key_first in Illuminate\Support\Arr helpers:
      Arr::firstKey($array, $key, $default);
      
  3. Future-Proofing:

    • Remove polyfill post-PHP upgrade:
      composer remove symfony/polyfill-php73
      
  4. Extension Points:

    • Override polyfill behavior by redefining functions before autoloading:
      function array_key_first(array $array): mixed {
          // Custom logic
      }
      require __DIR__.'/vendor/autoload.php';
      
  5. CI/CD:

    • Test polyfills on PHP 7.2 in GitHub Actions:
      jobs:
        test:
          runs-on: ubuntu-latest
          strategy:
            matrix:
              php: ['7.2', '7.4', '8.1']
      
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.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle