Skip to content

Commit

Permalink
Improve handling of the stroustrup coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin committed Apr 14, 2020
1 parent a8056cd commit e6b5a86
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 128 deletions.
21 changes: 21 additions & 0 deletions SubmittyStandard/Sniffs/ControlStructures/DoWhileSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Verifies that control statements conform to their coding standards.
*
* @author Matthew Peveler
*/

declare(strict_types=1);

namespace SubmittyStandard\Sniffs\ControlStructures;

use PHP_CodeSniffer\Standards\Squiz\Sniffs\ControlStructures\ControlSignatureSniff;

class DoWhileSniff extends ControlSignatureSniff {
public function register() {
return [
T_WHILE
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,14 @@

namespace SubmittyStandard\Sniffs\ControlStructures;

use PHP_CodeSniffer\Sniffs\AbstractPatternSniff;
use PHPCSExtra\Universal\Sniffs\ControlStructures\IfElseDeclarationSniff;

class StroustrupStructureSniff extends AbstractPatternSniff {
// phpcs:disable SubmittyStandard.NamingConventions.ValidVariableName.MemberNotSnakeCase
/**
* If true, comments will be ignored if they are found in the code.
*
* @var boolean
*/
public $ignoreComments = true;
// phpcs: enable

protected function getPatterns() {
class StroustrupStructureSniff extends IfElseDeclarationSniff {
public function register() {
return [
'do {EOL...} while (...);EOL',
'while (...) {EOL',
'for (...) {EOL',
'if (...) {EOL',
'foreach (...) {EOL',
'}EOLelse if (...) {EOL',
'}EOLelseif (...) {EOL',
'}EOLelse {EOL',
'do {EOL',
'try {EOL',
'}EOLcatch (...) {EOL'
\T_ELSE,
\T_ELSEIF,
\T_CATCH
];
}
}
9 changes: 9 additions & 0 deletions SubmittyStandard/Tests/ControlStructures/DoWhileUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

do {
} while (true);

// do-while is special
do {
}
while(true);
18 changes: 18 additions & 0 deletions SubmittyStandard/Tests/ControlStructures/DoWhileUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace SubmittyStandard\Tests\ControlStructures;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class DoWhileUnitTest extends AbstractSniffUnitTest {
public function getErrorList() {
return [
8 => 1,
9 => 1
];
}

public function getWarningList() {
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ elseif (false) {
else {
}

do {
} while (true);

// do-while is special
do {
}
while(true);

try {
} catch ($exc) {
}
Expand All @@ -34,10 +26,3 @@ try
{
} catch (exc) {
}

try
{
}
catch (exc)
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ public function getErrorList() {
4 => 1,
5 => 1,
6 => 1,
20 => 1,
25 => 1,
33 => 1,
35 => 1,
38 => 1,
41 => 1
17 => 1,
27 => 1
];
}

Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
"require": {
"php": ">=7.1",
"squizlabs/php_codesniffer": "^3.5.4",
"slevomat/coding-standard": "^6.1"
"slevomat/coding-standard": "^6.1",
"phpcsstandards/phpcsutils": "dev-develop",
"phpcsstandards/phpcsextra": "dev-develop"
},
"require-dev": {
"pcov/clobber": "^2.0",
Expand Down
Loading

0 comments on commit e6b5a86

Please sign in to comment.