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

keradus / PHP-CS-Fixer / 17678835382

12 Sep 2025 03:24PM UTC coverage: 94.69% (-0.06%) from 94.75%
17678835382

push

github

keradus
fix typo

1 of 1 new or added line in 1 file covered. (100.0%)

1042 existing lines in 177 files now uncovered.

28424 of 30018 relevant lines covered (94.69%)

45.5 hits per line

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

92.73
/src/Utils.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of PHP CS Fixer.
7
 *
8
 * (c) Fabien Potencier <fabien@symfony.com>
9
 *     Dariusz Rumiński <dariusz.ruminski@gmail.com>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14

15
namespace PhpCsFixer;
16

17
use PhpCsFixer\Fixer\FixerInterface;
18
use PhpCsFixer\Tokenizer\Token;
19

20
/**
21
 * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
22
 * @author Graham Campbell <hello@gjcampbell.co.uk>
23
 * @author Odín del Río <odin.drp@gmail.com>
24
 *
25
 * @internal
26
 *
27
 * @deprecated This is a God Class anti-pattern. Don't expand it. It is fine to use logic that is already here (that's why we don't trigger deprecation warnings), but over time logic should be moved to dedicated, single-responsibility classes.
28
 *
29
 * @no-named-arguments Parameter names are not covered by the backward compatibility promise.
30
 */
31
final class Utils
32
{
33
    private function __construct()
34
    {
35
        // cannot create instance
UNCOV
36
    }
×
37

38
    /**
39
     * Converts a camel cased string to a snake cased string.
40
     */
41
    public static function camelCaseToUnderscore(string $string): string
42
    {
43
        return mb_strtolower(Preg::replace('/(?<!^)(?<!_)((?=[\p{Lu}][^\p{Lu}])|(?<![\p{Lu}])(?=[\p{Lu}]))/', '_', $string));
13✔
44
    }
45

46
    /**
47
     * Calculate the trailing whitespace.
48
     *
49
     * What we're doing here is grabbing everything after the final newline.
50
     */
51
    public static function calculateTrailingWhitespaceIndent(Token $token): string
52
    {
53
        if (!$token->isWhitespace()) {
7✔
54
            throw new \InvalidArgumentException(\sprintf('The given token must be whitespace, got "%s".', $token->getName()));
1✔
55
        }
56

57
        $str = strrchr(
6✔
58
            str_replace(["\r\n", "\r"], "\n", $token->getContent()),
6✔
59
            "\n"
6✔
60
        );
6✔
61

62
        if (false === $str) {
6✔
63
            return '';
1✔
64
        }
65

66
        return ltrim($str, "\n");
5✔
67
    }
68

69
    /**
70
     * Perform stable sorting using provided comparison function.
71
     *
72
     * Stability is ensured by using Schwartzian transform.
73
     *
74
     * @template T
75
     * @template L of list<T>
76
     * @template R
77
     *
78
     * @param L                   $elements
79
     * @param callable(T): R      $getComparedValue a callable that takes a single element and returns the value to compare
80
     * @param callable(R, R): int $compareValues    a callable that compares two values
81
     *
82
     * @return L
83
     */
84
    public static function stableSort(array $elements, callable $getComparedValue, callable $compareValues): array
85
    {
86
        $sortItems = [];
5✔
87
        foreach ($elements as $index => $element) {
5✔
88
            $sortItems[] = [$element, $index, $getComparedValue($element)];
5✔
89
        }
90

91
        usort($sortItems, static function ($a, $b) use ($compareValues): int {
5✔
92
            $comparison = $compareValues($a[2], $b[2]);
5✔
93

94
            if (0 !== $comparison) {
5✔
95
                return $comparison;
3✔
96
            }
97

98
            return $a[1] <=> $b[1];
3✔
99
        });
5✔
100

101
        return array_map(static fn (array $item) => $item[0], $sortItems); // @phpstan-ignore return.type (PHPStan cannot understand that the result will still be L template)
5✔
102
    }
103

104
    /**
105
     * Sort fixers by their priorities.
106
     *
107
     * @template T of list<FixerInterface>
108
     *
109
     * @param T $fixers
110
     *
111
     * @return T
112
     */
113
    public static function sortFixers(array $fixers): array
114
    {
115
        // Schwartzian transform is used to improve the efficiency and avoid
116
        // `usort(): Array was modified by the user comparison function` warning for mocked objects.
117
        return self::stableSort(
1✔
118
            $fixers,
1✔
119
            static fn (FixerInterface $fixer): int => $fixer->getPriority(),
1✔
120
            static fn (int $a, int $b): int => $b <=> $a
1✔
121
        );
1✔
122
    }
123

124
    /**
125
     * Join names in natural language using specified wrapper (double quote by default).
126
     *
127
     * @param list<string> $names
128
     *
129
     * @throws \InvalidArgumentException
130
     */
131
    public static function naturalLanguageJoin(array $names, string $wrapper = '"', string $lastJoin = 'and'): string
132
    {
133
        if (0 === \count($names)) {
24✔
134
            throw new \InvalidArgumentException('Array of names cannot be empty.');
2✔
135
        }
136

137
        if (\strlen($wrapper) > 1) {
22✔
138
            throw new \InvalidArgumentException('Wrapper should be a single-char string or empty.');
1✔
139
        }
140

141
        $names = array_map(static fn (string $name): string => \sprintf('%2$s%1$s%2$s', $name, $wrapper), $names);
21✔
142

143
        $last = array_pop($names);
21✔
144

145
        if (\count($names) > 0) {
21✔
146
            return implode(', ', $names).' '.$lastJoin.' '.$last;
14✔
147
        }
148

149
        return $last;
7✔
150
    }
151

152
    /**
153
     * Join names in natural language wrapped in backticks, e.g. `a`, `b` and `c`.
154
     *
155
     * @param list<string> $names
156
     *
157
     * @throws \InvalidArgumentException
158
     */
159
    public static function naturalLanguageJoinWithBackticks(array $names, string $lastJoin = 'and'): string
160
    {
161
        return self::naturalLanguageJoin($names, '`', $lastJoin);
7✔
162
    }
163

164
    public static function convertArrayTypeToList(string $type): string
165
    {
UNCOV
166
        $parts = explode('[]', $type);
×
UNCOV
167
        $count = \count($parts) - 1;
×
168

UNCOV
169
        return str_repeat('list<', $count).$parts[0].str_repeat('>', $count);
×
170
    }
171

172
    /**
173
     * @param mixed $value
174
     */
175
    public static function toString($value): string
176
    {
177
        return \is_array($value)
11✔
178
            ? self::arrayToString($value)
6✔
179
            : self::scalarToString($value);
11✔
180
    }
181

182
    /**
183
     * @param mixed $value
184
     */
185
    private static function scalarToString($value): string
186
    {
187
        $str = var_export($value, true);
10✔
188

189
        return Preg::replace('/\bNULL\b/', 'null', $str);
10✔
190
    }
191

192
    /**
193
     * @param array<array-key, mixed> $value
194
     */
195
    private static function arrayToString(array $value): string
196
    {
197
        if (0 === \count($value)) {
6✔
198
            return '[]';
1✔
199
        }
200

201
        $isHash = !array_is_list($value);
5✔
202
        $str = '[';
5✔
203

204
        foreach ($value as $k => $v) {
5✔
205
            if ($isHash) {
5✔
206
                $str .= self::scalarToString($k).' => ';
2✔
207
            }
208

209
            $str .= \is_array($v)
5✔
210
                ? self::arrayToString($v).', '
2✔
211
                : self::scalarToString($v).', ';
5✔
212
        }
213

214
        return substr($str, 0, -2).']';
5✔
215
    }
216
}
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