Skip to content

Commit e6b5a86

Browse files
committed
Improve handling of the stroustrup coding style
1 parent a8056cd commit e6b5a86

File tree

8 files changed

+261
-128
lines changed

8 files changed

+261
-128
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* Verifies that control statements conform to their coding standards.
5+
*
6+
* @author Matthew Peveler
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace SubmittyStandard\Sniffs\ControlStructures;
12+
13+
use PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ControlSignatureSniff;
14+
15+
class DoWhileSniff extends ControlSignatureSniff {
16+
public function register() {
17+
return [
18+
T_WHILE
19+
];
20+
}
21+
}

SubmittyStandard/Sniffs/ControlStructures/StroustrupStructureSniff.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,14 @@
1010

1111
namespace SubmittyStandard\Sniffs\ControlStructures;
1212

13-
use PHP_CodeSniffer\Sniffs\AbstractPatternSniff;
13+
use PHPCSExtra\Universal\Sniffs\ControlStructures\IfElseDeclarationSniff;
1414

15-
class StroustrupStructureSniff extends AbstractPatternSniff {
16-
// phpcs:disable SubmittyStandard.NamingConventions.ValidVariableName.MemberNotSnakeCase
17-
/**
18-
* If true, comments will be ignored if they are found in the code.
19-
*
20-
* @var boolean
21-
*/
22-
public $ignoreComments = true;
23-
// phpcs: enable
24-
25-
protected function getPatterns() {
15+
class StroustrupStructureSniff extends IfElseDeclarationSniff {
16+
public function register() {
2617
return [
27-
'do {EOL...} while (...);EOL',
28-
'while (...) {EOL',
29-
'for (...) {EOL',
30-
'if (...) {EOL',
31-
'foreach (...) {EOL',
32-
'}EOLelse if (...) {EOL',
33-
'}EOLelseif (...) {EOL',
34-
'}EOLelse {EOL',
35-
'do {EOL',
36-
'try {EOL',
37-
'}EOLcatch (...) {EOL'
18+
\T_ELSE,
19+
\T_ELSEIF,
20+
\T_CATCH
3821
];
3922
}
4023
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
do {
4+
} while (true);
5+
6+
// do-while is special
7+
do {
8+
}
9+
while(true);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace SubmittyStandard\Tests\ControlStructures;
4+
5+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
6+
7+
class DoWhileUnitTest extends AbstractSniffUnitTest {
8+
public function getErrorList() {
9+
return [
10+
8 => 1,
11+
9 => 1
12+
];
13+
}
14+
15+
public function getWarningList() {
16+
return [];
17+
}
18+
}

SubmittyStandard/Tests/ControlStructures/StroustrupStructureUnitTest.inc

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,6 @@ elseif (false) {
1313
else {
1414
}
1515

16-
do {
17-
} while (true);
18-
19-
// do-while is special
20-
do {
21-
}
22-
while(true);
23-
2416
try {
2517
} catch ($exc) {
2618
}
@@ -34,10 +26,3 @@ try
3426
{
3527
} catch (exc) {
3628
}
37-
38-
try
39-
{
40-
}
41-
catch (exc)
42-
{
43-
}

SubmittyStandard/Tests/ControlStructures/StroustrupStructureUnitTest.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,8 @@ public function getErrorList() {
1010
4 => 1,
1111
5 => 1,
1212
6 => 1,
13-
20 => 1,
14-
25 => 1,
15-
33 => 1,
16-
35 => 1,
17-
38 => 1,
18-
41 => 1
13+
17 => 1,
14+
27 => 1
1915
];
2016
}
2117

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"require": {
77
"php": ">=7.1",
88
"squizlabs/php_codesniffer": "^3.5.4",
9-
"slevomat/coding-standard": "^6.1"
9+
"slevomat/coding-standard": "^6.1",
10+
"phpcsstandards/phpcsutils": "dev-develop",
11+
"phpcsstandards/phpcsextra": "dev-develop"
1012
},
1113
"require-dev": {
1214
"pcov/clobber": "^2.0",

0 commit comments

Comments
 (0)