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

The-oGlow / ezlogging / 18263869522

05 Oct 2025 08:21PM UTC coverage: 84.516% (+11.0%) from 73.543%
18263869522

push

github

ollily
#1: fix github actions

24 of 36 new or added lines in 3 files covered. (66.67%)

11 existing lines in 4 files now uncovered.

262 of 310 relevant lines covered (84.52%)

13.7 hits per line

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

84.85
/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
        $sepChar  = $withTextSep ? '"' : '';
3✔
41
        $output   = '';
3✔
42
        $valueIdx = 0;
3✔
43
        if (is_array($anyData) || (is_object($anyData) && is_subclass_of($anyData, ArrayAccess::class))) {
3✔
44
            /**
45
             * @psalm-suppress PossibleRawObjectIteration,PossiblyInvalidIterator
46
             * @phpstan-ignore foreach.nonIterable
47
             */
48
            foreach ($anyData as $key => $value) {
3✔
49
                // @phpstan-ignore ternary.condNotBoolean
50
                $output .= ($valueIdx ? $glue : '') . ($withKeys ? (is_int($key) ? $key : "'" . $key . "'") . '=>' : '');
3✔
51
                if (is_array($value)) {
3✔
52
                    $arrOutput = $this->implode_recursive($glue, $value, $withTextSep, $withKeys);
3✔
53
                    if (!empty($arrOutput)) {
3✔
54
                        $output .= '[' . $arrOutput . ']';
3✔
55
                    } else {
NEW
56
                        $output .= '[]';
×
57
                    }
58
                } else {
59
                    if (is_object($value)) {
3✔
60
                        $objOutput = $this->implode_recursive($glue, $value, $withTextSep, $withKeys);
1✔
61
                        if (!empty($objOutput)) {
1✔
62
                            $output .= '{' . $objOutput . '}';
1✔
63
                        } else {
NEW
64
                            $output .= '{}';
×
65
                        }
66
                    } else {
67
                        $output .= $sepChar . ((string) $value) . $sepChar;
2✔
68
                    }
69
                }
70
                $valueIdx++;
3✔
71
            }
72
        } else {
73
            if (is_object($anyData)) {
1✔
74
                if ($anyData instanceof Stringable) {
1✔
NEW
75
                    $output = $anyData->__toString();
×
76
                } else {
77
                    $output = get_class($anyData);
1✔
78
                }
79
            } else {
NEW
80
                $output = $anyData;
×
81
            }
82
        }
83

84
        return $output;
3✔
85
    }
86

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

116
        return $output;
10✔
117
    }
118
}
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