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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

71.43
/src/Metadata/Util/CamelCaseToSnakeCaseNameConverter.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
/*
15
 * This file is part of the Symfony package.
16
 *
17
 * (c) Fabien Potencier <fabien@symfony.com>
18
 *
19
 * For the full copyright and license information, please view the LICENSE
20
 * file that was distributed with this source code.
21
 */
22

23
namespace ApiPlatform\Metadata\Util;
24

25
/**
26
 * CamelCase to Underscore name converter.
27
 *
28
 * @internal
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
class CamelCaseToSnakeCaseNameConverter
33
{
34
    /**
35
     * @param array|null $attributes     The list of attributes to rename or null for all attributes
36
     * @param bool       $lowerCamelCase Use lowerCamelCase style
37
     */
38
    public function __construct(
39
        private readonly ?array $attributes = null,
40
        private readonly bool $lowerCamelCase = true,
41
    ) {
42
    }
596✔
43

44
    public function normalize(string $propertyName): string
45
    {
46
        if (null === $this->attributes || \in_array($propertyName, $this->attributes, true)) {
×
47
            return strtolower(preg_replace('/[A-Z]/', '_\\0', lcfirst($propertyName)));
×
48
        }
49

50
        return $propertyName;
×
51
    }
52

53
    public function denormalize(string $propertyName): string
54
    {
55
        $camelCasedName = preg_replace_callback(
100✔
56
            '/(^|_|\.)+(.)/',
100✔
57
            fn ($match) => ('.' === $match[1] ? '_' : '').strtoupper($match[2]),
100✔
58
            $propertyName
100✔
59
        );
100✔
60

61
        if ($this->lowerCamelCase) {
100✔
62
            $camelCasedName = lcfirst($camelCasedName);
100✔
63
        }
64

65
        if (null === $this->attributes || \in_array($camelCasedName, $this->attributes, true)) {
100✔
66
            return $camelCasedName;
100✔
67
        }
68

69
        return $propertyName;
×
70
    }
71
}
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

© 2025 Coveralls, Inc