Skip to content

Commit a73b83f

Browse files
maryokukulich
authored andcommitted
SlevomatCodingStandard.Classes.ClassStructure: Support for custom method groups based on method name prefix
1 parent c2eab04 commit a73b83f

6 files changed

+56
-5
lines changed

SlevomatCodingStandard/Sniffs/Classes/ClassStructureSniff.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use SlevomatCodingStandard\Helpers\NamespaceHelper;
1515
use SlevomatCodingStandard\Helpers\PropertyHelper;
1616
use SlevomatCodingStandard\Helpers\SniffSettingsHelper;
17+
use SlevomatCodingStandard\Helpers\StringHelper;
1718
use SlevomatCodingStandard\Helpers\TokenHelper;
1819
use function array_diff;
1920
use function array_filter;
@@ -415,8 +416,18 @@ private function resolveMethodGroup(File $phpcsFile, int $pointer, string $metho
415416
{
416417
foreach ($this->getNormalizedMethodGroups() as $group => $methodRequirements) {
417418
foreach ($methodRequirements as $methodRequirement) {
418-
if ($methodRequirement['name'] !== null && $method !== strtolower($methodRequirement['name'])) {
419-
continue;
419+
if ($methodRequirement['name'] !== null) {
420+
$requiredName = strtolower($methodRequirement['name']);
421+
422+
if (StringHelper::endsWith($requiredName, '*')) {
423+
$methodNamePrefix = substr($requiredName, 0, -1);
424+
425+
if ($method === $methodNamePrefix || !StringHelper::startsWith($method, $methodNamePrefix)) {
426+
continue;
427+
}
428+
} elseif ($method !== $requiredName) {
429+
continue;
430+
}
420431
}
421432

422433
if (

doc/classes.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ constants, properties, static properties, methods, all public methods, all prote
6565
<rule ref="SlevomatCodingStandard.Classes.ClassStructure">
6666
<properties>
6767
<property name="methodGroups" type="array">
68+
<element key="inject method" value="inject"/>
69+
<element key="inject methods" value="inject*"/>
6870
<element key="phpunit before" value="setUp, @before, #PHPUnit\Framework\Attributes\Before"/>
6971
</property>
7072

@@ -80,13 +82,21 @@ constants, properties, static properties, methods, all public methods, all prote
8082
<!-- You don't care about the order among the properties. The same can be done with "properties" shortcut -->
8183
<element value="public properties, protected properties, private properties"/>
8284

83-
<!-- Constructor is first, then all public methods, then protected/private methods and magic methods are last -->
85+
<!-- Constructor is first -->
8486
<element value="constructor"/>
8587

86-
<!-- PHPUnit's before hooks are placed before all other public methods using a custom method group regardless their visibility -->
88+
<!-- Then inject method followed by all other inject methods based on their prefix using a custom method group regardless their visibility -->
89+
<element value="inject method"/>
90+
<element value="inject methods"/>
91+
92+
<!-- PHPUnit's before hooks are placed before all other public methods using a custom method group -->
8793
<element value="phpunit before"/>
94+
95+
<!-- Then all public methods, followed by protected/private methods -->
8896
<element value="all public methods"/>
8997
<element value="methods"/>
98+
99+
<!-- Magic methods are last -->
90100
<element value="magic methods"/>
91101
</property>
92102
</properties>

tests/Sniffs/Classes/ClassStructureSniffTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class ClassStructureSniffTest extends TestCase
2727
];
2828

2929
private const METHOD_GROUPS = [
30+
'inject method' => 'inject',
31+
'inject methods' => 'inject*',
3032
'phpunit before class' => 'setUpBeforeClass, @beforeClass, #PHPUnit\Framework\Attributes\BeforeClass',
3133
'phpunit after class' => 'tearDownAfterClass, @afterClass, #PHPUnit\Framework\Attributes\AfterClass',
3234
'phpunit before' => 'setUp, @before, #PHPUnit\Framework\Attributes\Before',
@@ -48,6 +50,8 @@ class ClassStructureSniffTest extends TestCase
4850
'constructor',
4951
'static constructors',
5052
'destructor',
53+
'inject method',
54+
'inject methods',
5155
'phpunit before class',
5256
'phpunit after class',
5357
'phpunit before',
@@ -209,12 +213,14 @@ public function testErrorsWithMethodGroupRules(): void
209213
],
210214
);
211215

212-
self::assertSame(5, $report->getErrorCount());
216+
self::assertSame(7, $report->getErrorCount());
213217
self::assertSniffError($report, 22, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
214218
self::assertSniffError($report, 33, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
215219
self::assertSniffError($report, 44, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
216220
self::assertSniffError($report, 48, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
217221
self::assertSniffError($report, 67, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
222+
self::assertSniffError($report, 71, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
223+
self::assertSniffError($report, 75, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
218224
self::assertAllFixedInFile($report);
219225
}
220226

tests/Sniffs/Classes/data/classStructureSniffErrorsWithMethodGroupRules.fixed.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ public function __destruct()
1010
{
1111
}
1212

13+
public function inject($foo)
14+
{
15+
}
16+
17+
public function injectFoo($foo)
18+
{
19+
}
20+
1321
public static function setUpBeforeClass()
1422
{
1523
}

tests/Sniffs/Classes/data/classStructureSniffErrorsWithMethodGroupRules.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ private function dolor()
6767
protected function afterUsingAnnotation()
6868
{
6969
}
70+
71+
public function injectFoo($foo)
72+
{
73+
}
74+
75+
public function inject($foo)
76+
{
77+
}
7078
}

tests/Sniffs/Classes/data/classStructureSniffNoErrorsWithMethodGroupRules.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ public function __destruct()
1010
{
1111
}
1212

13+
public function inject($foo)
14+
{
15+
}
16+
17+
public function injectFoo($foo)
18+
{
19+
}
20+
1321
public static function setUpBeforeClass()
1422
{
1523
}

0 commit comments

Comments
 (0)