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

lmc-eu / php-coding-standard / 13558276316

03 Jan 2025 07:16PM UTC coverage: 81.481%. Remained the same
13558276316

push

github

OndraM
Update PHP CS Fixer to fixed version

176 of 216 relevant lines covered (81.48%)

19.86 hits per line

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

80.95
/src/Helper/Naming.php
1
<?php declare(strict_types=1);
2

3
/*
4
 * Originally part of https://github.com/symplify/symplify
5
 *
6
 * MIT License
7
 *
8
 * (c) 2020 Tomas Votruba <tomas.vot@gmail.com>
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in all
18
 * copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
 * SOFTWARE.
27
 */
28

29
namespace Lmc\CodingStandard\Helper;
30

31
use PHP_CodeSniffer\Files\File;
32
use SlevomatCodingStandard\Helpers\NamespaceHelper;
33
use SlevomatCodingStandard\Helpers\ReferencedNameHelper;
34

35
final class Naming
36
{
37
    /**
38
     * @var string
39
     */
40
    private const NAMESPACE_SEPARATOR = '\\';
41

42
    /**
43
     * @var mixed[][]
44
     */
45
    private array $referencedNamesByFilePath = [];
46

47
    /**
48
     * @var string[][]
49
     */
50
    private array $fqnClassNameByFilePathAndClassName = [];
51

52
    public function getClassName(File $file, int $classNameStartPosition): string
48✔
53
    {
54
        $tokens = $file->getTokens();
48✔
55

56
        $firstNamePart = $tokens[$classNameStartPosition]['content'];
48✔
57

58
        // is class <name>
59
        if ($this->isClassName($file, $classNameStartPosition)) {
48✔
60
            $namespace = NamespaceHelper::findCurrentNamespaceName($file, $classNameStartPosition);
48✔
61
            if ($namespace) {
48✔
62
                return $namespace . '\\' . $firstNamePart;
×
63
            }
64

65
            return $firstNamePart;
48✔
66
        }
67

68
        $classNameParts = [];
36✔
69
        $classNameParts[] = $firstNamePart;
36✔
70

71
        $nextTokenPointer = $classNameStartPosition + 1;
36✔
72
        while ($tokens[$nextTokenPointer]['code'] === T_NS_SEPARATOR) {
36✔
73
            ++$nextTokenPointer;
×
74
            $classNameParts[] = $tokens[$nextTokenPointer]['content'];
×
75
            ++$nextTokenPointer;
×
76
        }
77

78
        $completeClassName = implode(self::NAMESPACE_SEPARATOR, $classNameParts);
36✔
79

80
        $fqnClassName = $this->getFqnClassName($file, $completeClassName, $classNameStartPosition);
36✔
81
        if ($fqnClassName !== '') {
36✔
82
            return ltrim($fqnClassName, self::NAMESPACE_SEPARATOR);
36✔
83
        }
84

85
        return $completeClassName;
×
86
    }
87

88
    private function getFqnClassName(File $file, string $className, int $classTokenPosition): string
36✔
89
    {
90
        $referencedNames = $this->getReferencedNames($file);
36✔
91

92
        foreach ($referencedNames as $referencedName) {
36✔
93
            if (isset($this->fqnClassNameByFilePathAndClassName[$file->path][$className])) {
36✔
94
                return $this->fqnClassNameByFilePathAndClassName[$file->path][$className];
×
95
            }
96

97
            $resolvedName = NamespaceHelper::resolveClassName(
36✔
98
                $file,
36✔
99
                $referencedName->getNameAsReferencedInFile(),
36✔
100
                $classTokenPosition,
36✔
101
            );
36✔
102

103
            if ($referencedName->getNameAsReferencedInFile() === $className) {
36✔
104
                $this->fqnClassNameByFilePathAndClassName[$file->path][$className] = $resolvedName;
36✔
105

106
                return $resolvedName;
36✔
107
            }
108
        }
109

110
        return '';
×
111
    }
112

113
    /**
114
     * As in:
115
     * class <name>
116
     */
117
    private function isClassName(File $file, int $position): bool
48✔
118
    {
119
        return (bool) $file->findPrevious(T_CLASS, $position, max(1, $position - 3));
48✔
120
    }
121

122
    /**
123
     * @return mixed[]
124
     */
125
    private function getReferencedNames(File $file): array
36✔
126
    {
127
        if (isset($this->referencedNamesByFilePath[$file->path])) {
36✔
128
            return $this->referencedNamesByFilePath[$file->path];
×
129
        }
130

131
        $referencedNames = ReferencedNameHelper::getAllReferencedNames($file, 0);
36✔
132

133
        $this->referencedNamesByFilePath[$file->path] = $referencedNames;
36✔
134

135
        return $referencedNames;
36✔
136
    }
137
}
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