• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

The-oGlow / ezkoding / 40125501008

24 Oct 2026 07:28PM UTC coverage: 80.655% (-3.8%) from 84.414%
40125501008

push

github

ollily
#1 cleancode

517 of 641 relevant lines covered (80.66%)

7.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.71
/src/Tools/String/ImplodeTrait.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of ezkoding
7
 *
8
 * (c) 2025 Oliver Glowa, coding.glowa.com
9
 *
10
 * This source file is subject to the Apache-2.0 license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13

14
namespace ollily\Tools\String;
15

16
use ArrayAccess;
17
use Stringable;
18

19
trait ImplodeTrait
20
{
21
    /**
22
     * Default glue character.
23
     *
24
     * @see ImplodeTrait::array_flatten()
25
     * @see ImplodeTrait::implode_recursive()
26
     */
27
    public const string DEFAULT_GLUE = ',';
28

29
    /**
30
     * Default separate character for csv.
31
     *
32
     * @see ImplodeTrait::array_flatten()
33
     * @see ImplodeTrait::implode_recursive()
34
     */
35
    public const string DEFAULT_ITEM_SEP = ';';
36

37
    /**
38
     * Recursively implodes an array with optional key inclusion.
39
     *
40
     * @param string $glue        value that glues elements together
41
     * @param mixed  $anyData     multi-dimensional array to recursively implode
42
     * @param bool   $withTextSep add a text seperator (") around each value of a scalar type (default: false)
43
     * @param bool   $withKeys    include keysForValue before their values (default: false)
44
     *
45
     * @return string imploded array
46
     *
47
     * @see https://www.php.net/manual/en/language.types.type-system.php
48
     */
49
    // @phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
50
    final public static function implode_recursive(string $glue, mixed $anyData, bool $withTextSep = false, bool $withKeys = false): string // NOSONAR: php:S100
3 ✔
51
    {
52
        $output   = '';
3 ✔
53
        $valueIdx = 0;
3 ✔
54
        $textSep  = $withTextSep ? '"' : '';
3 ✔
55
        $objWithArray = (is_object($anyData) && is_subclass_of($anyData, ArrayAccess::class));
3 ✔
56

57
        if (is_array($anyData) || $objWithArray) {
3 ✔
58
            /**
59
             * Not recognize it is an Iterable.
60
             *
61
             * @phpstan-ignore foreach.nonIterable
62
             */
63
            foreach ($anyData as $key => $value) {
3 ✔
64
                $currKey = ($withKeys ? (is_int($key) ? $key : "'$key'") . '=>' : ''); // NOSONAR: php:S3358
3 ✔
65
                $output .= ($valueIdx > 0 ? $glue : '') .  $currKey;
3 ✔
66

67
                if (is_array($value)) {
3 ✔
68
                    $arrOutput = self::implode_recursive($glue, $value, $withTextSep, $withKeys);
1 ✔
69
                    if (!empty($arrOutput)) {
1 ✔
70
                        $output .= '[' . $arrOutput . ']';
1 ✔
71
                    } else {
72
                        $output .= '[]';
×
73
                    }
74
                } else {
75
                    if (is_object($value)) {
3 ✔
76
                        $objOutput = self::implode_recursive($glue, $value, $withTextSep, $withKeys);
1 ✔
77
                        if (!empty($objOutput)) {
1 ✔
78
                            $output .= '{' . $objOutput . '}';
1 ✔
79
                        } else {
80
                            $output .= '{}';
×
81
                        }
82
                    } else {
83
                        $output .= $textSep . ((string) $value) . $textSep;
2 ✔
84
                    }
85
                }
86
                $valueIdx++;
3 ✔
87
            }
88
        } else {
89
            if (is_object($anyData)) {
1 ✔
90
                if ($anyData instanceof Stringable) {
1 ✔
91
                    $output = $anyData->__toString();
×
92
                } else {
93
                    $output = $anyData::class;
1 ✔
94
                }
95
            } else {
96
                $output = $anyData;
×
97
            }
98
        }
99

100
        return $output;
3 ✔
101
    }
102

103
    /**
104
     * Flatten a multidimensional anyData to one dimension, optionally preserving keys.
105
     * Original found on {@link https://stackoverflow.com/a/526633}.
106
     *
107
     * @param array<mixed> $anyData      the anyData to flatten
108
     * @param int          $preserveKeys 0 to not preserve keys (default),
109
     *                                   1 to preserve string keys only,
110
     *                                   2 to preserve all keys
111
     * @param array<mixed> $output       internal use argument for recursion
112
     *
113
     * @return array<mixed>
114
     *
115
     * @see https://stackoverflow.com/a/526633
116
     */
117
    // @phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
118
    final public function array_flatten(array $anyData, int $preserveKeys = 0, array &$output = []): array // NOSONAR: php:S100
4 ✔
119
    {
120
        foreach ($anyData as $anyKey => $anyValue) {
4 ✔
121
            if (is_array($anyValue)) {
3 ✔
122
                $output = $this->array_flatten($anyValue, $preserveKeys, $output);
2 ✔
123
            } elseif ($preserveKeys + (int)is_string($anyKey) > 1) {
3 ✔
124
                $output[$anyKey] = $anyValue;
×
125
            } else {
126
                $output[] = $anyValue;
3 ✔
127
            }
128
        }
129

130
        return $output;
4 ✔
131
    }
132
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc