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

CPS-IT / project-builder / 24971672608

27 Apr 2026 01:01AM UTC coverage: 96.239% (-0.3%) from 96.489%
24971672608

push

github

web-flow
[TASK] Update all dependencies (#924)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

2226 of 2313 relevant lines covered (96.24%)

14.32 hits per line

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

82.35
/src/Helper/StringHelper.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "cpsit/project-builder".
7
 *
8
 * Copyright (C) 2022 Elias Häußler <e.haeussler@familie-redlich.de>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace CPSIT\ProjectBuilder\Helper;
25

26
use CPSIT\ProjectBuilder\Exception;
27
use CPSIT\ProjectBuilder\StringCase;
28

29
/**
30
 * StringHelper.
31
 *
32
 * @author Elias Häußler <e.haeussler@familie-redlich.de>
33
 * @license GPL-3.0-or-later
34
 */
35
final class StringHelper
36
{
37
    private const REGEX_WORDS = '/[A-Z]{2,}(?=[A-Z][a-z]+\d*|\b)|[A-Z]?[a-z]+\d*|[A-Z]|\d+/';
38

39
    /**
40
     * @param value-of<StringCase> $case
41
     *
42
     * @throws Exception\StringConversionException
43
     */
44
    public static function convertCase(string $string, string $case): string
12✔
45
    {
46
        return match ($case) {
×
47
            StringCase::Lower->value => strtolower($string),
12✔
48
            StringCase::Upper->value => strtoupper($string),
11✔
49
            StringCase::Snake->value => strtolower(implode('_', self::splitStringIntoChunks($string))),
8✔
50
            StringCase::UpperCamel->value => str_replace(' ', '', ucwords($string)),
7✔
51
            StringCase::LowerCamel->value => str_replace(' ', '', lcfirst(ucwords($string))),
12✔
52
        };
×
53
    }
54

55
    /**
56
     * @param array<string, mixed> $replacePairs
57
     */
58
    public static function interpolate(string $string, array $replacePairs): string
9✔
59
    {
60
        foreach (array_keys($replacePairs) as $replaceKey) {
9✔
61
            $replacePairs['{'.trim($replaceKey, '{}').'}'] = $replacePairs[$replaceKey];
8✔
62
            unset($replacePairs[$replaceKey]);
8✔
63
        }
64

65
        return strtr($string, $replacePairs);
9✔
66
    }
67

68
    /**
69
     * @return list<string>
70
     *
71
     * @see https://www.geeksforgeeks.org/how-to-convert-a-string-to-snake-case-using-javascript/
72
     */
73
    private static function splitStringIntoChunks(string $string): array
1✔
74
    {
75
        if (false === preg_match_all(self::REGEX_WORDS, $string, $matches)) {
1✔
76
            throw Exception\StringConversionException::forUnmatchedString($string);
×
77
        }
78

79
        return $matches[0];
1✔
80
    }
81
}
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