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

The-oGlow / ezlogging / 18278713918

06 Oct 2025 11:02AM UTC coverage: 84.295% (+2.2%) from 82.105%
18278713918

push

github

web-flow
Merge pull request #10 from The-oGlow/develop

Develop

148 of 175 new or added lines in 13 files covered. (84.57%)

12 existing lines in 2 files now uncovered.

263 of 312 relevant lines covered (84.29%)

13.63 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 ezlogging
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
     * Recursively implodes an array with optional key inclusion.
23
     *
24
     * Example of $include_keys output: key, value, key, value, key, value
25
     *
26
     * @param string $glue        value that glues elements together
27
     * @param mixed  $anyData     multi-dimensional array to recursively implode
28
     * @param bool   $withTextSep add a text seperator (") around each value of a scalar type (default: false)
29
     * @param bool   $withKeys    include keysForValue before their values (default: false)
30
     *
31
     * @return string imploded array
32
     *
33
     * @see https://www.php.net/manual/en/language.types.type-system.php
34
     *
35
     * @SuppressWarnings("PHPMD.CamelCaseMethodName")
36
     */
37
    // @phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
38
    protected function implode_recursive(string $glue, $anyData, bool $withTextSep = false, bool $withKeys = false): string // NOSONAR: php:S100
3✔
39
    {
40
        $output   = '';
3✔
41
        $valueIdx = 0;
3✔
42
        $textSep  = $withTextSep ? '"' : '';
3✔
43
        $objWithArray = (is_object($anyData) && is_subclass_of($anyData, ArrayAccess::class));
3✔
44

45
        if (is_array($anyData) || $objWithArray) {
3✔
46
            foreach ($anyData as $key => $value) {
3✔
47
                $currKey = ($withKeys ? (is_int($key) ? $key : "'$key'") . '=>' : ''); // NOSONAR: php:S3358
3✔
48
                $output .= ($valueIdx > 0 ? $glue : '') .  $currKey;
3✔
49

50
                if (is_array($value)) {
3✔
51
                    $arrOutput = $this->implode_recursive($glue, $value, $withTextSep, $withKeys);
3✔
52
                    if (!empty($arrOutput)) {
3✔
53
                        $output .= '[' . $arrOutput . ']';
3✔
54
                    } else {
NEW
55
                        $output .= '[]';
×
56
                    }
57
                } else {
58
                    if (is_object($value)) {
3✔
59
                        $objOutput = $this->implode_recursive($glue, $value, $withTextSep, $withKeys);
1✔
60
                        if (!empty($objOutput)) {
1✔
61
                            $output .= '{' . $objOutput . '}';
1✔
62
                        } else {
NEW
63
                            $output .= '{}';
×
64
                        }
65
                    } else {
66
                        $output .= $textSep . ((string) $value) . $textSep;
2✔
67
                    }
68
                }
69
                $valueIdx++;
3✔
70
            }
71
        } else {
72
            if (is_object($anyData)) {
1✔
73
                if ($anyData instanceof Stringable) {
1✔
NEW
74
                    $output = $anyData->__toString();
×
75
                } else {
76
                    $output = get_class($anyData);
1✔
77
                }
78
            } else {
NEW
79
                $output = $anyData;
×
80
            }
81
        }
82

83
        return $output;
3✔
84
    }
85

86
    /**
87
     * Flatten a multidimensional anyData to one dimension, optionally preserving keys.
88
     * Original found on {@link https://stackoverflow.com/a/526633}.
89
     *
90
     * @param array<mixed,mixed> $anyData      the anyData to flatten
91
     * @param int                $preserveKeys 0 to not preserve keys (default),
92
     *                                         1 to preserve string keys only,
93
     *                                         2 to preserve all keys
94
     * @param array<mixed,mixed> $output       internal use argument for recursion
95
     *
96
     * @return array<mixed,mixed>
97
     *
98
     * @see https://stackoverflow.com/a/526633
99
     *
100
     * @SuppressWarnings("PHPMD.CamelCaseMethodName")
101
     */
102
    // @phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps
103
    public function array_flatten(array $anyData, int $preserveKeys = 0, array &$output = []): array // NOSONAR: php:S100
10✔
104
    {
105
        foreach ($anyData as $anyKey => $anyValue) {
10✔
106
            if (is_array($anyValue)) {
10✔
107
                $output = $this->array_flatten($anyValue, $preserveKeys, $output);
5✔
108
            } elseif ($preserveKeys + (int)is_string($anyKey) > 1) {
10✔
NEW
109
                $output[$anyKey] = $anyValue;
×
110
            } else {
111
                $output[] = $anyValue;
10✔
112
            }
113
        }
114

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

© 2026 Coveralls, Inc