Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions SlevomatCodingStandard/Sniffs/Classes/ClassStructureSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ private function resolveMethodGroup(File $phpcsFile, int $pointer, string $metho
if ($method === $methodNamePrefix || !StringHelper::startsWith($method, $methodNamePrefix)) {
continue;
}
} elseif (StringHelper::startsWith($requiredName, '*')) {
$methodNameSuffix = substr($requiredName, 1);

if ($method === $methodNameSuffix || !StringHelper::endsWith($method, $methodNameSuffix)) {
continue;
}
} elseif ($method !== $requiredName) {
continue;
}
Expand Down
5 changes: 5 additions & 0 deletions doc/classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Sniff provides the following settings:

* `groups`: order of groups. Use multiple groups in one `<element value="">` to not differentiate among them. You can use specific groups or shortcuts.
* `methodGroups`: custom method groups. Define a custom group for special methods based on their name, annotation, or attribute.
* You can use a `*` as prefix or suffix to filter methods name as seen in the example below.

**List of supported groups**:
uses,
Expand All @@ -68,6 +69,7 @@ constants, properties, static properties, methods, all public methods, all prote
<element key="inject method" value="inject"/>
<element key="inject methods" value="inject*"/>
<element key="phpunit before" value="setUp, @before, #PHPUnit\Framework\Attributes\Before"/>
<element key="phpunit data provider" value="*DataProvider"/>
</property>

<property name="groups" type="array">
Expand Down Expand Up @@ -96,6 +98,9 @@ constants, properties, static properties, methods, all public methods, all prote
<element value="all public methods"/>
<element value="methods"/>

<!-- PHPUnit's data providers are placed after all other public methods using a custom method group -->
<element value="phpunit data provider"/>

<!-- Magic methods are last -->
<element value="magic methods"/>
</property>
Expand Down
8 changes: 5 additions & 3 deletions tests/Sniffs/Classes/ClassStructureSniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ClassStructureSniffTest extends TestCase
'phpunit after class' => 'tearDownAfterClass, @afterClass, #PHPUnit\Framework\Attributes\AfterClass',
'phpunit before' => 'setUp, @before, #PHPUnit\Framework\Attributes\Before',
'phpunit after' => 'tearDown, @after, #PHPUnit\Framework\Attributes\After',
'phpunit data provider' => '*DataProvider',
];

private const METHOD_GROUP_RULES = [
Expand Down Expand Up @@ -67,6 +68,7 @@ class ClassStructureSniffTest extends TestCase
'protected static abstract methods',
'private methods',
'private static methods',
'phpunit data provider',
];

public function testNoErrors(): void
Expand Down Expand Up @@ -220,9 +222,9 @@ public function testErrorsWithMethodGroupRules(): void
self::assertSniffError($report, 33, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 44, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 48, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 67, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 71, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 75, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 72, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 76, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertSniffError($report, 80, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
self::assertAllFixedInFile($report);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ protected function ipsum()
private function dolor()
{
}

public static function fooDataProvider(): \Generator
{
yield 'foo' => ['bar'];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ private function dolor()
{
}

public static function fooDataProvider(): \Generator
{
yield 'foo' => ['bar'];
}

/**
* @after
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ protected function ipsum()
private function dolor()
{
}

public static function fooDataProvider(): \Generator
{
yield 'foo' => ['bar'];
}
}
Loading