Skip to content

Conversation

@Alamin30
Copy link

@Alamin30 Alamin30 commented Nov 3, 2025

Summary

This PR improves the PostgresGrammar::customOperators() method by replacing a redundant double array_filter call with a single, explicit filter closure. This results in cleaner logic, fewer operations, and more explicit intent without changing behavior.

Problem

The current implementation uses nested array_filter calls:

array_filter(array_filter($operators, 'is_string'))

This is redundant because:

  1. The inner array_filter($operators, 'is_string') filters to keep only string values
  2. The outer array_filter(...) without a callback removes falsy values (including empty strings)
  3. This requires two passes through the array

Solution

Replace the double filter with a single, explicit filter:

array_filter($operators, fn ($op) => is_string($op) && $op)

This approach:

  • ✅ Performs the same filtering in a single pass
  • ✅ Is more readable and explicit about the intent
  • ✅ Improves performance (minor but measurable)
  • ✅ Maintains backward compatibility
  • ✅ Passes all existing tests

Changes

File: src/Illuminate/Database/Query/Grammars/PostgresGrammar.php

Before:

public static function customOperators(array $operators)
{
    static::$customOperators = array_values(
        array_merge(static::$customOperators, array_filter(array_filter($operators, 'is_string')))
    );
}

After:

public static function customOperators(array $operators)
{
    static::$customOperators = array_values(
        array_merge(static::$customOperators, array_filter($operators, fn ($op) => is_string($op) && $op))
    );
}

Testing

All existing tests pass, including the specific test that validates this behavior:

vendor/bin/phpunit --filter testCustomOperators tests/Database/DatabasePostgresQueryGrammarTest.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants