Skip to content

Commit b7c135c

Browse files
authored
✨ Add SlevomatCodingStandard.Classes.ClassStructure (#5)
1 parent 107799a commit b7c135c

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

PreviousNextDrupal/ruleset.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@
66
<!-- PSR2.Methods -->
77
<rule ref="PSR2.Methods.FunctionClosingBrace.SpacingBeforeClose"/>
88

9+
<!-- SlevomatCodingStandard.Classes -->
10+
<rule ref="SlevomatCodingStandard.Classes.ClassStructure">
11+
<properties>
12+
<property name="groups" type="array">
13+
<element value="uses"/>
14+
<element value="enum cases"/>
15+
<element value="public constants"/>
16+
<element value="constants"/>
17+
<element value="properties"/>
18+
<element value="constructor"/>
19+
<!-- We cant order by public/protected methods yet as it would
20+
make PHPUnit setup() drift to bottom. Wait until resolved:
21+
https://github.com/slevomat/coding-standard/issues/1098 -->
22+
<element value="methods"/>
23+
</property>
24+
</properties>
25+
</rule>
26+
927
<!-- Drupal -->
1028
<rule ref="Drupal" />
1129

tests/Sniffs/ClassStructureTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace PreviousNext\CodingStandard\Tests\Sniffs;
6+
7+
use SlevomatCodingStandard\Sniffs\Classes\ClassStructureSniff;
8+
9+
/**
10+
* @covers \SlevomatCodingStandard\Sniffs\Classes\ClassStructureSniff
11+
*/
12+
final class ClassStructureTest extends Base {
13+
14+
public function testNoError(): void {
15+
$report = self::checkFile(__DIR__ . '/fixtures/ClassStructureNoError.php');
16+
self::assertNoSniffErrorInFile($report);
17+
}
18+
19+
public function testError(): void {
20+
$report = self::checkFile(__DIR__ . '/fixtures/ClassStructureError.php');
21+
self::assertSame(8, $report->getErrorCount());
22+
self::assertSniffError($report, 33, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
23+
self::assertSniffError($report, 36, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
24+
self::assertSniffError($report, 38, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
25+
self::assertSniffError($report, 41, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
26+
self::assertSniffError($report, 43, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
27+
self::assertSniffError($report, 45, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
28+
self::assertSniffError($report, 47, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
29+
self::assertSniffError($report, 49, ClassStructureSniff::CODE_INCORRECT_GROUP_ORDER);
30+
}
31+
32+
protected static function getSniffName(): string {
33+
return 'SlevomatCodingStandard.Classes.ClassStructure';
34+
}
35+
36+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Sniffs\fixtures;
6+
7+
/**
8+
* The class.
9+
*/
10+
final class ClassStructureError {
11+
12+
/**
13+
* A method.
14+
*/
15+
protected function baz(): void {
16+
}
17+
18+
/**
19+
* A method.
20+
*/
21+
private function bar(): void {
22+
}
23+
24+
/**
25+
* A method.
26+
*/
27+
public function foo(): void {
28+
}
29+
30+
/**
31+
* A method.
32+
*/
33+
public function __construct() {
34+
}
35+
36+
protected $golf = NULL;
37+
38+
private $foxtrot = NULL;
39+
40+
41+
public $delta = NULL;
42+
43+
protected const CHARLIE = 'charlie';
44+
45+
private const BRAVO = 'bravo';
46+
47+
public const ALPHA = 'alpha';
48+
49+
use Foo;
50+
51+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Sniffs\fixtures;
6+
7+
/**
8+
* The class.
9+
*/
10+
final class ClassStructureNoError {
11+
12+
use Foo;
13+
14+
public const ALPHA = 'alpha';
15+
private const BRAVO = 'bravo';
16+
protected const CHARLIE = 'charlie';
17+
18+
public $delta = NULL;
19+
private $foxtrot = NULL;
20+
protected $golf = NULL;
21+
22+
/**
23+
* A method.
24+
*/
25+
public function __construct() {
26+
}
27+
28+
/**
29+
* A method.
30+
*/
31+
public function foo(): void {
32+
}
33+
34+
/**
35+
* A method.
36+
*/
37+
private function bar(): void {
38+
}
39+
40+
/**
41+
* A method.
42+
*/
43+
protected function baz(): void {
44+
}
45+
46+
}

0 commit comments

Comments
 (0)