WordPress-Coding-Standards/bin/class-ruleset-test.php
jrfnl 3d92e2fd6a Travis: add a new check for early detection of ruleset problems
WPCS itself uses `WordPress-Extra` + `WordPress-Docs` for its CS, which includes `WordPress-Core`.

Those rulesets were therefore "tested" on each build, just by running them against the WPCS code.
However, the WPCS native CS ruleset also excludes some things and the CS check ignores warnings for the Travis run and it also left the `WordPress-VIP` and `WordPress` rulesets _untested_.

This PR intends to fix this by adding a minimal test file which should trigger most sniffs and testing each ruleset against it.

This should, from now on, prevent the ruleset issues we've seen related to the moving and deprecating of sniffs.
This will also serve as an early warning system in case any of the upstream sniffs which are included in the rulesets would be removed/renamed or otherwise have a change in behaviour which WPCS will need to take into account.

Notes:
* This is complimentary to the unit tests as those are run in a ruleset-agnostic manner, i.e. the ruleset isn't loaded and any settings in the ruleset are ignored.
* At this moment, the test file is incomplete, but complete enough for our purposes.
    Further improvement of the test file in the future to make sure that all sniffs are triggered is recommended.
* The check only needs to be run once against every PHPCS version supported/tested.
    With that in mind, "old-style" ignore annotations have been used.
    Once the minimum PHPCS version WPCS supports goes beyond PHPCS 3.2.0, those should be swopped out for selective ignores instead.
* The check should be run on a high PHP version to prevent triggering the `Generic.PHP.Syntax` sniff when modern PHP code is included in the test file to ensure that this triggers the appropriate sniffs.
    At this moment, the most modern syntax included in nullable types which was introduced in PHP 7.1 which is why that is the PHP version against which these tests are currently run.
2018-07-24 23:36:00 +02:00

115 lines
2.2 KiB
PHP

<?php
/**
* Simple file which should not have any issues when testing against the WPCS rulesets.
*
* Most - if not all - sniffs should be triggered by this file.
*
* Used to do a simple CI test on the rulesets.
*
* Currently covered - based on the rulesets as of July 24 2018:
* - Every WPCS native sniff is triggered.
* - Every WPCS + PHPCS sniff within the Core ruleset is triggered.
* - Every WPCS + PHPCS sniff within the VIP ruleset is triggered.
*
* @package WPCS\WordPressCodingStandards
*/
echo 'Hello world';
$a = function_call();
/**
* Class docblock.
*/
class Ruleset_Test {
/**
* Constant docblock.
*
* @var bool
*/
const A = false;
/**
* Property docblock.
*
* @var int
*/
public $a = 123;
/**
* Property docblock.
*
* @var array
*/
protected $array_prop = array(
'a' => 1,
'b' => 2,
);
/**
* Function docblock.
*
* @param int $param_a Testing.
* @param bool|null $param_b Testing.
*
* @return void
*/
public function testing( $param_a, ?bool $param_b = false ): void {
?>
<a href="<?php echo esc_html( $param_a ); ?>"><?php echo (int) $param_b; ?></a>
<?php
$a = ( $cond ) ? true : false;
$b = function ( $a ) use ( $param_a ) {
if ( preg_match( '`' . preg_quote( $param_a, '`' ) . '`i', $param_b ) === 1 ) {
echo esc_html( "doublequoted $string" );
} elseif ( $this->a ) {
$ab = $a % $b + $c / $d && $param_a || $param_b >> $bitshift;
}
};
$c = $this->array_prop['a'];
$d = new self();
$e = apply_filter( 'filter_name', $d, $c );
if ( $a == $b ) { // WPCS: loose comparison OK.
$f = isset( $_GET['nonce'] ) ? 1 : 2; // WPCS: CSRF ok, input var ok.
}
// @codingStandardsIgnoreLine
$g = @eval( 'return true;' );
switch ( $param_a ) {
case 1:
echo esc_html( self::A );
break;
case 2:
include_once 'some-file.php';
continue;
}
}
/**
* Function docblock.
*
* @return void
*/
public function test_goto() {
$i = 0;
$j = 50;
for ( ; $i < 100; $i++ ) {
while ( $j-- ) {
if ( 17 === $j ) {
// @codingStandardsIgnoreLine
goto end;
}
}
}
// @codingStandardsIgnoreLine
end:
echo 'This is a goto - it needs to be here to prevent parse errors';
}
}