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
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +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}}
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ The semantic versioning started from version 0.2.1.

_No documentation available about unreleased changes yet._

## [3.1.0](https://github.com/infinum/eightshift-coding-standards/compare/3.0.1...3.1.0)

### Added
- `Eightshift.ControlStructures.DisallowAlternativeSyntax` sniff — enforces curly braces for all control structures; disallows `endif`, `endforeach`, `endfor`, `endwhile`, and `endswitch`.

### Fixed
- PHPUnit test suite bootstrap — changed `phpunit.xml.dist` to use `AllSniffs.php` as entry point so `PHP_CODESNIFFER_STANDARD_DIRS` globals are properly populated.
- Renamed `ComponentsEscapeUnitTest` files to `HelpersEscapeUnitTest` to match the `HelpersEscapeSniff` name and satisfy sniff feature completeness checks.

## [3.0.1](https://github.com/infinum/eightshift-coding-standards/compare/3.0.0...3.0.1)

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Disallow Alternative Control Structure Syntax">
<standard>
<![CDATA[
Alternative control structure syntax (endif, endforeach, endfor, endwhile, endswitch)
is not allowed. Use curly braces for all control structures.
]]>
</standard>
<code_comparison>
<code title="Valid: Using curly braces for foreach">
<![CDATA[
foreach ( $items as $item ) {
echo $item;
}
]]>
</code>
<code title="Invalid: Using alternative syntax for foreach">
<![CDATA[
foreach ( $items as $item ) :
echo $item;
endforeach;
]]>
</code>
</code_comparison>
<code_comparison>
<code title="Valid: Using curly braces for if/else">
<![CDATA[
if ( $condition ) {
echo 'yes';
} else {
echo 'no';
}
]]>
</code>
<code title="Invalid: Using alternative syntax for if/else">
<![CDATA[
if ( $condition ) :
echo 'yes';
else :
echo 'no';
endif;
]]>
</code>
</code_comparison>
</documentation>
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* Eightshift coding standards for WordPress
*
* @package EightshiftCS
*
* @author Eightshift <team.wordpress@infinum.com>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/infinum/eightshift-coding-standards
*/

namespace EightshiftCS\Eightshift\Sniffs\ControlStructures;

use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;

/**
* Disallows alternative control structure syntax.
*
* Enforces the use of curly braces for all control structures.
* Alternative syntax keywords (endif, endforeach, endfor, endwhile, endswitch)
* are not allowed.
*
* @since 2.1.0
*/
class DisallowAlternativeSyntaxSniff implements Sniff
{
/**
* Returns an array of tokens this test wants to listen for.
*
* @since 2.1.0
*
* @return array
*/
public function register()
{
return [
\T_ENDIF,
\T_ENDFOREACH,
\T_ENDFOR,
\T_ENDWHILE,
\T_ENDSWITCH,
];
}

/**
* Processes this test when one of its tokens is encountered.
*
* @since 2.1.0
*
* @param File $phpcsFile The file being scanned.
* @param int $stackPtr The position of the current token in the stack.
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$phpcsFile->addError(
'Alternative control structure syntax is not allowed; use curly braces instead.',
$stackPtr,
'FoundAlternativeSyntax'
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

$items = [1, 2, 3];
$condition = true;
$i = 0;
$x = 1;

// Bad: alternative syntax.
foreach ($items as $item) : // Ok - opening is fine, error is on closing keyword.
echo $item;
endforeach; // Error.

if ($condition) : // Ok.
echo 'yes';
endif; // Error.

while ($i < 3) : // Ok.
$i++;
endwhile; // Error.

for ($i = 0; $i < 3; $i++) : // Ok.
echo $i;
endfor; // Error.

switch ($x) : // Ok.
case 1:
echo 'one';
break;
endswitch; // Error.

// Good: curly brace syntax.
foreach ($items as $item) { // Ok.
echo $item;
}

if ($condition) { // Ok.
echo 'yes';
}

while ($i < 3) { // Ok.
$i++;
}

for ($i = 0; $i < 3; $i++) { // Ok.
echo $i;
}

switch ($x) { // Ok.
case 1:
echo 'one';
break;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* Unit test class for DisallowAlternativeSyntax sniff.
*
* @package EightshiftCS
*
* @author Eightshift <team.wordpress@infinum.com>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/infinum/eightshift-coding-standards
*/

namespace EightshiftCS\Eightshift\Tests\ControlStructures;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the DisallowAlternativeSyntax sniff.
*
* @covers \EightshiftCS\Eightshift\Sniffs\ControlStructures\DisallowAlternativeSyntaxSniff
*
* @since 2.1.0
*/
class DisallowAlternativeSyntaxUnitTest extends AbstractSniffUnitTest
{
/**
* Returns the lines where errors should occur.
*
* @return array<int, int> Key is the line number, value is the number of expected errors.
*/
public function getErrorList(): array
{
return [
11 => 1,
15 => 1,
19 => 1,
23 => 1,
29 => 1,
];
}

/**
* Returns the lines where warnings should occur.
*
* @return array<int, int> Key is the line number, value is the number of expected warnings.
*/
public function getWarningList(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

/**
* Unit test class for the FunctionCommentUnitTest sniff.
* Unit test class for the HelpersEscape sniff.
*
* @covers \EightshiftCS\Eightshift\Sniffs\Security\HelpersEscapeSniff
*
Expand Down
3 changes: 3 additions & 0 deletions Eightshift/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@
<exclude name="Eightshift.Commenting.FunctionComment.SpacingAfterParamType"/>
</rule>

<!-- Disallow alternative control structure syntax (endif, endforeach, etc.). Use curly braces. -->
<rule ref="Eightshift.ControlStructures.DisallowAlternativeSyntax"/>

<!-- Use tabs, not spaces. -->
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>

Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<testsuites>
<testsuite name="Eightshift">
<directory suffix="UnitTest.php">./Eightshift/Tests/</directory>
<file>vendor/squizlabs/php_codesniffer/tests/Standards/AllSniffs.php</file>
</testsuite>
</testsuites>
</phpunit>
Loading