Skip to content
Snippets Groups Projects
Commit 3a3c8164 authored by catch's avatar catch
Browse files

Issue #3471932 by mstrelan, bbrala, quietone: Fix callables that are expected...

Issue #3471932 by mstrelan, bbrala, quietone: Fix callables that are expected to return bool but don't
parent c5193b30
No related branches found
No related tags found
15 merge requests!11131[10.4.x-only-DO-NOT-MERGE]: Issue ##2842525 Ajax attached to Views exposed filter form does not trigger callbacks,!3878Removed unused condition head title for views,!3818Issue #2140179: $entity->original gets stale between updates,!3742Issue #3328429: Create item list field formatter for displaying ordered and unordered lists,!3731Claro: role=button on status report items,!3154Fixes #2987987 - CSRF token validation broken on routes with optional parameters.,!3133core/modules/system/css/components/hidden.module.css,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!2812Issue #3312049: [Followup] Fix Drupal.Commenting.FunctionComment.MissingReturnType returns for NULL,!2062Issue #3246454: Add weekly granularity to views date sort,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!617Issue #3043725: Provide a Entity Handler for user cancelation,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493
Pipeline #305182 passed with warnings
Pipeline: drupal

#305189

    Showing
    with 111 additions and 9 deletions
    <?php
    declare(strict_types=1);
    namespace Drupal\Component\Utility;
    /**
    * Provides methods to filter arrays.
    *
    * @ingroup utility
    */
    class FilterArray {
    /**
    * Removes empty strings from an array.
    *
    * This method removes all empty strings from the input array. This is
    * particularly useful to preserve 0 whilst filtering other falsy values. The
    * values are first cast to a string before comparison.
    *
    * @param array $value
    * The array to filter.
    *
    * @return array
    * The filtered array.
    */
    public static function removeEmptyStrings(array $value): array {
    return array_filter($value, static fn ($item) => (string) $item !== '');
    }
    }
    ......@@ -2,6 +2,7 @@
    namespace Drupal\Core\Datetime\Element;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\Component\Utility\NestedArray;
    use Drupal\Component\Utility\Variable;
    use Drupal\Core\Datetime\DateHelper;
    ......@@ -345,7 +346,7 @@ protected static function checkEmptyInputs($input, $parts) {
    // \Drupal\Core\Datetime\Element\Datelist::valueCallback().
    unset($input['object']);
    // Filters out empty array values, any valid value would have a string length.
    $filtered_input = array_filter($input, 'strlen');
    $filtered_input = FilterArray::removeEmptyStrings($input);
    return array_diff($parts, array_keys($filtered_input));
    }
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\Core\TypedData\Plugin\DataType;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    use Drupal\Core\TypedData\Attribute\DataType;
    use Drupal\Core\TypedData\ComplexDataInterface;
    ......@@ -92,7 +93,7 @@ public function getString() {
    $strings[] = $item->getString();
    }
    // Remove any empty strings resulting from empty items.
    return implode(', ', array_filter($strings, 'mb_strlen'));
    return implode(', ', FilterArray::removeEmptyStrings($strings));
    }
    /**
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\Core\TypedData\Plugin\DataType;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    use Drupal\Core\TypedData\Attribute\DataType;
    use Drupal\Core\TypedData\ComplexDataInterface;
    ......@@ -106,7 +107,7 @@ public function getString() {
    $strings[] = $property->getString();
    }
    // Remove any empty strings resulting from empty items.
    return implode(', ', array_filter($strings, 'mb_strlen'));
    return implode(', ', FilterArray::removeEmptyStrings($strings));
    }
    /**
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\field\Plugin\migrate\process\d6;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\migrate\Attribute\MigrateProcess;
    use Drupal\migrate\MigrateExecutableInterface;
    use Drupal\migrate\ProcessPluginBase;
    ......@@ -27,7 +28,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
    if (isset($global_settings['allowed_values'])) {
    $list = explode("\n", $global_settings['allowed_values']);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    $list = FilterArray::removeEmptyStrings($list);
    switch ($field_type) {
    case 'boolean';
    $option = preg_replace('/^option_/', '', $row->getSourceProperty('property'));
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\field\Plugin\migrate\process\d6;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\migrate\Attribute\MigrateProcess;
    use Drupal\migrate\MigrateExecutableInterface;
    use Drupal\migrate\ProcessPluginBase;
    ......@@ -29,7 +30,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
    if (isset($global_settings['allowed_values'])) {
    $list = explode("\n", $global_settings['allowed_values']);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    $list = FilterArray::removeEmptyStrings($list);
    switch ($field_type) {
    case 'list_string':
    case 'list_integer':
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\field\Plugin\migrate\process\d6;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\migrate\Attribute\MigrateProcess;
    use Drupal\migrate\MigrateExecutableInterface;
    use Drupal\migrate\ProcessPluginBase;
    ......@@ -50,7 +51,7 @@ public function getSettings($field_type, $global_settings, $original_field_type
    if (isset($global_settings['allowed_values'])) {
    $list = explode("\n", $global_settings['allowed_values']);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    $list = FilterArray::removeEmptyStrings($list);
    switch ($field_type) {
    case 'list_string':
    case 'list_integer':
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\sqlite\Driver\Database\sqlite;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\Core\Database\Connection as DatabaseConnection;
    use Drupal\Core\Database\DatabaseNotFoundException;
    use Drupal\Core\Database\ExceptionHandler;
    ......@@ -246,7 +247,7 @@ public static function sqlFunctionGreatest() {
    */
    public static function sqlFunctionLeast() {
    // Remove all NULL, FALSE and empty strings values but leaves 0 (zero) values.
    $values = array_filter(func_get_args(), 'strlen');
    $values = FilterArray::removeEmptyStrings(func_get_args());
    return count($values) < 1 ? NULL : min($values);
    }
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\user\Plugin\migrate\process\d6;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\migrate\Attribute\MigrateProcess;
    use Drupal\migrate\MigrateExecutableInterface;
    use Drupal\migrate\ProcessPluginBase;
    ......@@ -27,7 +28,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
    $allowed_values = [];
    $list = explode("\n", $translation);
    $list = array_map('trim', $list);
    $list = array_filter($list, 'strlen');
    $list = FilterArray::removeEmptyStrings($list);
    if ($field_type === 'list_string') {
    foreach ($list as $value) {
    $allowed_values[] = ['label' => $value];
    ......
    ......@@ -2,6 +2,7 @@
    namespace Drupal\views\Plugin\views;
    use Drupal\Component\Utility\FilterArray;
    use Drupal\Component\Utility\Html;
    use Drupal\Component\Utility\Unicode;
    use Drupal\Component\Utility\UrlHelper;
    ......@@ -758,7 +759,7 @@ public static function breakString($str, $force_int = FALSE) {
    // Filter any empty matches (Like from '++' in a string) and reset the
    // array keys. 'strlen' is used as the filter callback so we do not lose
    // 0 values (would otherwise evaluate == FALSE).
    $value = array_values(array_filter($value, 'strlen'));
    $value = array_values(FilterArray::removeEmptyStrings($value));
    if ($force_int) {
    $value = array_map('intval', $value);
    ......
    <?php
    declare(strict_types=1);
    namespace Drupal\Tests\Component\Utility;
    use Drupal\Component\Utility\FilterArray;
    use PHPUnit\Framework\TestCase;
    /**
    * Test filter array functions.
    *
    * @group Utility
    *
    * @coversDefaultClass \Drupal\Component\Utility\FilterArray
    */
    class FilterArrayTest extends TestCase {
    /**
    * Tests removing empty strings.
    *
    * @dataProvider providerRemoveEmptyStrings
    * @covers ::removeEmptyStrings
    */
    public function testRemoveEmptyStrings(array $values, array $expected): void {
    $this->assertEquals($expected, array_values(FilterArray::removeEmptyStrings($values)));
    }
    /**
    * Data provider for testRemoveEmptyStrings().
    *
    * @see testRemoveEmptyStrings()
    */
    public static function providerRemoveEmptyStrings(): \Generator {
    yield 'strings' => [
    ['', ' ', '0', 'true', 'false'],
    [' ', '0', 'true', 'false'],
    ];
    yield 'integers' => [
    [-1, 0, 1],
    [-1, 0, 1],
    ];
    yield 'null, true, false' => [
    [NULL, TRUE, FALSE],
    [TRUE],
    ];
    $stringable = new class implements \Stringable {
    public function __toString(): string {
    return 'foo';
    }
    };
    yield 'non-scalar' => [
    [new $stringable()],
    [new $stringable()],
    ];
    }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment