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
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace StarterKitStandard\Sniffs\ControlStructures;

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

/**
* Disallows alternative (colon) control-structure syntax.
*
* PHP only emits T_ENDIF/T_ENDFOREACH/T_ENDWHILE/T_ENDFOR/T_ENDSWITCH tokens when the
* alternative `if (...) : ... endif;` syntax is used, so matching directly on those end
* tokens is sufficient - no need to inspect scope openers/closers.
*/
final class DisallowAlternativeSyntaxSniff implements Sniff
{
/**
* @return array<int, int>
*/
public function register()
{
return [
T_ENDIF,
T_ENDFOREACH,
T_ENDWHILE,
T_ENDFOR,
T_ENDSWITCH,
];
}

/**
* @param File $phpcsFile
* @param int $stackPtr
*
* @return void
*/
public function process(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
$keyword = $tokens[$stackPtr]['content'];

$phpcsFile->addError(
'Alternative control-structure syntax ("%s") is not allowed; use brace syntax instead.',
$stackPtr,
'Found',
[$keyword]
);
}
}
6 changes: 6 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
namespace/ABSPATH guard, never loaded as-is. The rest of stubs/ is real PHP copied verbatim
into the theme root during conversion, so it stays linted (must pass composer lint). -->
<exclude-pattern>*/stubs/*.snippet.php</exclude-pattern>
<!-- Custom sniff tooling, not theme code - not scanned against theme style rules. -->
<exclude-pattern>*/.phpcs/*</exclude-pattern>

<!--
#############################################################################
Expand All @@ -72,4 +74,8 @@
<!-- Use PHPCompatibilityWP standard -->
<rule ref="PHPCompatibilityWP"/>

<!-- Disallow alternative (colon) control-structure syntax (if/endif, foreach/endforeach, ...)
- required by stubs/classic-theme/CONVERT.md's brace-syntax rule for classic templates. -->
<rule ref="./.phpcs/StarterKitStandard/Sniffs/ControlStructures/DisallowAlternativeSyntaxSniff.php"/>

</ruleset>
Loading