Skip to content

Commit 7f5aa85

Browse files
authored
Merge pull request #64 from infinum/feat/control-structures
Disallow alternative control structure syntax
2 parents 47c1587 + d6e70c6 commit 7f5aa85

15 files changed

+230
-2
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"EightshiftCS\\Eightshift\\Tests\\Commenting\\FunctionCommentUnitTest::testSniff":4,"EightshiftCS\\Eightshift\\Tests\\ControlStructures\\DisallowAlternativeSyntaxUnitTest::testSniff":4,"EightshiftCS\\Eightshift\\Tests\\Security\\HelpersEscapeUnitTest::testSniff":4,"EightshiftCS\\Eightshift\\Tests\\Shortcodes\\DisallowDoShortcodeUnitTest::testSniff":4},"times":{"EightshiftCS\\Eightshift\\Tests\\Commenting\\FunctionCommentUnitTest::testSniff":0.002,"EightshiftCS\\Eightshift\\Tests\\ControlStructures\\DisallowAlternativeSyntaxUnitTest::testSniff":0,"EightshiftCS\\Eightshift\\Tests\\Security\\HelpersEscapeUnitTest::testSniff":0,"EightshiftCS\\Eightshift\\Tests\\Shortcodes\\DisallowDoShortcodeUnitTest::testSniff":0}}

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ The semantic versioning started from version 0.2.1.
1010

1111
_No documentation available about unreleased changes yet._
1212

13+
## [3.1.0](https://github.com/infinum/eightshift-coding-standards/compare/3.0.1...3.1.0)
14+
15+
### Added
16+
- `Eightshift.ControlStructures.DisallowAlternativeSyntax` sniff — enforces curly braces for all control structures; disallows `endif`, `endforeach`, `endfor`, `endwhile`, and `endswitch`.
17+
18+
### Fixed
19+
- PHPUnit test suite bootstrap — changed `phpunit.xml.dist` to use `AllSniffs.php` as entry point so `PHP_CODESNIFFER_STANDARD_DIRS` globals are properly populated.
20+
- Renamed `ComponentsEscapeUnitTest` files to `HelpersEscapeUnitTest` to match the `HelpersEscapeSniff` name and satisfy sniff feature completeness checks.
21+
1322
## [3.0.1](https://github.com/infinum/eightshift-coding-standards/compare/3.0.0...3.0.1)
1423

1524
### Changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0"?>
2+
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
4+
title="Disallow Alternative Control Structure Syntax">
5+
<standard>
6+
<![CDATA[
7+
Alternative control structure syntax (endif, endforeach, endfor, endwhile, endswitch)
8+
is not allowed. Use curly braces for all control structures.
9+
]]>
10+
</standard>
11+
<code_comparison>
12+
<code title="Valid: Using curly braces for foreach">
13+
<![CDATA[
14+
foreach ( $items as $item ) {
15+
echo $item;
16+
}
17+
]]>
18+
</code>
19+
<code title="Invalid: Using alternative syntax for foreach">
20+
<![CDATA[
21+
foreach ( $items as $item ) :
22+
echo $item;
23+
endforeach;
24+
]]>
25+
</code>
26+
</code_comparison>
27+
<code_comparison>
28+
<code title="Valid: Using curly braces for if/else">
29+
<![CDATA[
30+
if ( $condition ) {
31+
echo 'yes';
32+
} else {
33+
echo 'no';
34+
}
35+
]]>
36+
</code>
37+
<code title="Invalid: Using alternative syntax for if/else">
38+
<![CDATA[
39+
if ( $condition ) :
40+
echo 'yes';
41+
else :
42+
echo 'no';
43+
endif;
44+
]]>
45+
</code>
46+
</code_comparison>
47+
</documentation>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/**
4+
* Eightshift coding standards for WordPress
5+
*
6+
* @package EightshiftCS
7+
*
8+
* @author Eightshift <team.wordpress@infinum.com>
9+
* @license https://opensource.org/licenses/MIT MIT
10+
* @link https://github.com/infinum/eightshift-coding-standards
11+
*/
12+
13+
namespace EightshiftCS\Eightshift\Sniffs\ControlStructures;
14+
15+
use PHP_CodeSniffer\Sniffs\Sniff;
16+
use PHP_CodeSniffer\Files\File;
17+
18+
/**
19+
* Disallows alternative control structure syntax.
20+
*
21+
* Enforces the use of curly braces for all control structures.
22+
* Alternative syntax keywords (endif, endforeach, endfor, endwhile, endswitch)
23+
* are not allowed.
24+
*
25+
* @since 2.1.0
26+
*/
27+
class DisallowAlternativeSyntaxSniff implements Sniff
28+
{
29+
/**
30+
* Returns an array of tokens this test wants to listen for.
31+
*
32+
* @since 2.1.0
33+
*
34+
* @return array
35+
*/
36+
public function register()
37+
{
38+
return [
39+
\T_ENDIF,
40+
\T_ENDFOREACH,
41+
\T_ENDFOR,
42+
\T_ENDWHILE,
43+
\T_ENDSWITCH,
44+
];
45+
}
46+
47+
/**
48+
* Processes this test when one of its tokens is encountered.
49+
*
50+
* @since 2.1.0
51+
*
52+
* @param File $phpcsFile The file being scanned.
53+
* @param int $stackPtr The position of the current token in the stack.
54+
*
55+
* @return void
56+
*/
57+
public function process(File $phpcsFile, $stackPtr)
58+
{
59+
$phpcsFile->addError(
60+
'Alternative control structure syntax is not allowed; use curly braces instead.',
61+
$stackPtr,
62+
'FoundAlternativeSyntax'
63+
);
64+
}
65+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
$items = [1, 2, 3];
4+
$condition = true;
5+
$i = 0;
6+
$x = 1;
7+
8+
// Bad: alternative syntax.
9+
foreach ($items as $item) : // Ok - opening is fine, error is on closing keyword.
10+
echo $item;
11+
endforeach; // Error.
12+
13+
if ($condition) : // Ok.
14+
echo 'yes';
15+
endif; // Error.
16+
17+
while ($i < 3) : // Ok.
18+
$i++;
19+
endwhile; // Error.
20+
21+
for ($i = 0; $i < 3; $i++) : // Ok.
22+
echo $i;
23+
endfor; // Error.
24+
25+
switch ($x) : // Ok.
26+
case 1:
27+
echo 'one';
28+
break;
29+
endswitch; // Error.
30+
31+
// Good: curly brace syntax.
32+
foreach ($items as $item) { // Ok.
33+
echo $item;
34+
}
35+
36+
if ($condition) { // Ok.
37+
echo 'yes';
38+
}
39+
40+
while ($i < 3) { // Ok.
41+
$i++;
42+
}
43+
44+
for ($i = 0; $i < 3; $i++) { // Ok.
45+
echo $i;
46+
}
47+
48+
switch ($x) { // Ok.
49+
case 1:
50+
echo 'one';
51+
break;
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* Unit test class for DisallowAlternativeSyntax sniff.
5+
*
6+
* @package EightshiftCS
7+
*
8+
* @author Eightshift <team.wordpress@infinum.com>
9+
* @license https://opensource.org/licenses/MIT MIT
10+
* @link https://github.com/infinum/eightshift-coding-standards
11+
*/
12+
13+
namespace EightshiftCS\Eightshift\Tests\ControlStructures;
14+
15+
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
16+
17+
/**
18+
* Unit test class for the DisallowAlternativeSyntax sniff.
19+
*
20+
* @covers \EightshiftCS\Eightshift\Sniffs\ControlStructures\DisallowAlternativeSyntaxSniff
21+
*
22+
* @since 2.1.0
23+
*/
24+
class DisallowAlternativeSyntaxUnitTest extends AbstractSniffUnitTest
25+
{
26+
/**
27+
* Returns the lines where errors should occur.
28+
*
29+
* @return array<int, int> Key is the line number, value is the number of expected errors.
30+
*/
31+
public function getErrorList(): array
32+
{
33+
return [
34+
11 => 1,
35+
15 => 1,
36+
19 => 1,
37+
23 => 1,
38+
29 => 1,
39+
];
40+
}
41+
42+
/**
43+
* Returns the lines where warnings should occur.
44+
*
45+
* @return array<int, int> Key is the line number, value is the number of expected warnings.
46+
*/
47+
public function getWarningList(): array
48+
{
49+
return [];
50+
}
51+
}

Eightshift/Tests/Security/ComponentsEscapeUnitTest.1.inc renamed to Eightshift/Tests/Security/HelpersEscapeUnitTest.1.inc

File renamed without changes.

Eightshift/Tests/Security/ComponentsEscapeUnitTest.2.inc renamed to Eightshift/Tests/Security/HelpersEscapeUnitTest.2.inc

File renamed without changes.

Eightshift/Tests/Security/ComponentsEscapeUnitTest.3.inc renamed to Eightshift/Tests/Security/HelpersEscapeUnitTest.3.inc

File renamed without changes.

Eightshift/Tests/Security/ComponentsEscapeUnitTest.4.inc renamed to Eightshift/Tests/Security/HelpersEscapeUnitTest.4.inc

File renamed without changes.

0 commit comments

Comments
 (0)