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

lmc-eu / php-coding-standard / 9111901257

16 May 2024 12:04PM UTC coverage: 81.651%. Remained the same
9111901257

push

github

OndraM
Docs: Update README

178 of 218 relevant lines covered (81.65%)

16.77 hits per line

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

91.43
/src/Sniffs/Naming/ClassNameSuffixByParentSniff.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\Sniffs\Naming;
30

31
use Lmc\CodingStandard\Helper\Naming;
32
use Lmc\CodingStandard\Helper\SniffClassWrapper;
33
use PHP_CodeSniffer\Files\File;
34
use PHP_CodeSniffer\Sniffs\Sniff;
35

36
final class ClassNameSuffixByParentSniff implements Sniff
37
{
38
    /** @var string */
39
    private const ERROR_MESSAGE = 'Class "%s" should have suffix "%s" by parent class/interface';
40

41
    /** @var string[] */
42
    public array $defaultParentClassToSuffixMap = [
43
        'Command',
44
        'Controller',
45
        'Repository',
46
        'Presenter',
47
        'Request',
48
        'Response',
49
        'FixerInterface',
50
        'Sniff',
51
        'Exception',
52
        'Handler',
53
    ];
54

55
    /** @var string[] */
56
    public array $extraParentTypesToSuffixes = [];
57

58
    /**
59
     * @return int[]
60
     */
61
    public function register(): array
40✔
62
    {
63
        return [T_CLASS];
40✔
64
    }
65

66
    public function process(File $phpcsFile, $stackPtr): void
40✔
67
    {
68
        $classWrapper = $this->getWrapperForFirstClassInFile($phpcsFile);
40✔
69
        if ($classWrapper === null) {
40✔
70
            return;
×
71
        }
72

73
        $className = $classWrapper->getClassName();
40✔
74
        if ($className === null) {
40✔
75
            return;
×
76
        }
77

78
        $parentClassName = $classWrapper->getParentClassName();
40✔
79
        if ($parentClassName) {
40✔
80
            $this->processType($phpcsFile, $parentClassName, $className, $stackPtr);
30✔
81
        }
82

83
        foreach ($classWrapper->getPartialInterfaceNames() as $interfaceName) {
40✔
84
            $this->processType($phpcsFile, $interfaceName, $className, $stackPtr);
10✔
85
        }
86
    }
87

88
    private function processType(File $file, string $currentParentType, string $className, int $position): void
40✔
89
    {
90
        foreach ($this->getClassToSuffixMap() as $parentType) {
40✔
91
            if (!fnmatch('*' . $parentType, $currentParentType)) {
40✔
92
                continue;
15✔
93
            }
94

95
            // the class that implements $currentParentType, should end with $suffix
96
            $suffix = $this->resolveExpectedSuffix($parentType);
35✔
97
            if (str_ends_with($className, $suffix)) {
35✔
98
                continue;
15✔
99
            }
100

101
            $file->addError(sprintf(self::ERROR_MESSAGE, $className, $suffix), $position, self::class);
20✔
102
        }
103
    }
104

105
    /**
106
     * @return string[]
107
     */
108
    private function getClassToSuffixMap(): array
40✔
109
    {
110
        return array_merge($this->defaultParentClassToSuffixMap, $this->extraParentTypesToSuffixes);
40✔
111
    }
112

113
    /**
114
     * - SomeInterface => Some
115
     * - AbstractSome => Some
116
     */
117
    private function resolveExpectedSuffix(string $parentType): string
35✔
118
    {
119
        if (str_ends_with($parentType, 'Interface')) {
35✔
120
            $parentType = mb_substr($parentType, 0, -mb_strlen('Interface'), 'UTF-8');
10✔
121
        }
122

123
        if (str_starts_with($parentType, 'Abstract')) {
35✔
124
            $parentType = mb_substr($parentType, mb_strlen('Abstract'), null, 'UTF-8');
10✔
125
        }
126

127
        return $parentType;
35✔
128
    }
129

130
    private function getWrapperForFirstClassInFile(File $file): ?SniffClassWrapper
40✔
131
    {
132
        $possibleClassPosition = $file->findNext(T_CLASS, 0);
40✔
133
        if (!is_int($possibleClassPosition)) {
40✔
134
            return null;
×
135
        }
136

137
        return new SniffClassWrapper($file, $possibleClassPosition, new Naming());
40✔
138
    }
139
}
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