Skip to content

Commit

Permalink
Fix WordPressVIPMinimum ruleset test cases for site_option grouping i…
Browse files Browse the repository at this point in the history
…n RestrictedFunctionsSniff
  • Loading branch information
rebeccahum committed Mar 26, 2021
1 parent 6f77a54 commit f503500
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 53 deletions.
36 changes: 2 additions & 34 deletions WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
/**
* WordPressVIPMinimum_Sniffs_Files_IncludingFileSniff.
*
* Checks for custom variables, functions and constants, and external URLs used in file inclusion.
* Checks that __DIR__, dirname( __FILE__ ) or plugin_dir_path( __FILE__ )
* is used when including or requiring files.
*
* @package VIPCS\WordPressVIPMinimum
*/
Expand Down Expand Up @@ -54,17 +55,6 @@ class IncludingFileSniff extends AbstractFunctionRestrictionsSniff {
'WP_PLUGIN_DIR',
];

/**
* List of keywords allowed for use in custom constants.
* Note: Customizing this property will overwrite current default values.
*
* @var array
*/
public $allowedKeywords = [
'PATH',
'DIR',
];

/**
* Functions used for modify slashes.
*
Expand Down Expand Up @@ -132,11 +122,6 @@ public function process_token( $stackPtr ) {
return;
}

if ( $this->has_custom_path( $this->tokens[ $nextToken ]['content'] ) === true ) {
// The construct is using a constant with an allowed keyword.
return;
}

if ( array_key_exists( $this->tokens[ $nextToken ]['content'], $this->restrictedConstants ) === true ) {
// The construct is using one of the restricted constants.
$message = '`%s` constant might not be defined or available. Use `%s()` instead.';
Expand Down Expand Up @@ -187,21 +172,4 @@ public function process_token( $stackPtr ) {
$message = 'Absolute include path must be used. Use `get_template_directory()`, `get_stylesheet_directory()` or `plugin_dir_path()`.';
$this->phpcsFile->addError( $message, $nextToken, 'NotAbsolutePath' );
}

/**
* Check if a content string contains a keyword in custom paths.
*
* @param string $content Content string.
*
* @return bool True if the string partially matches a keyword in $allowedCustomKeywords, false otherwise.
*/
private function has_custom_path( $content ) {
foreach ( $this->allowedKeywords as $keyword ) {
if ( strpos( $content, $keyword ) !== false ) {
return true;
}
}

return false;
}
}
11 changes: 1 addition & 10 deletions WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,4 @@ require_once "my_file.php"; // Not absolute path.
require '../my_file.php'; // Not absolute path.
require '../../my_file.php'; // Not absolute path.
include( 'http://www.google.com/bad_file.php' ); // External URL.
include_once("http://www.google.com/bad_file.php"); // External URL.

// Allowed keywords
include 'https://path.com/bad_file.php'; // Error - external URL with keyword from $allowedKeywords.
require $path; // Warning - custom variable with keyword from $allowedKeywords.
include_once dir_function(); // Error - custom functionm with keyword from $allowedKeywords.
require CUSTOM_CONSTANT_DIR . 'file.php'; // OK.
require_once ( VIPCS_PATH ) . 'file.php'; // OK.
include_once
DIR_CUSTOM , 'file.php'; // OK.
include_once("http://www.google.com/bad_file.php"); // External URL.
3 changes: 0 additions & 3 deletions WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public function getErrorList() {
26 => 1,
27 => 1,
28 => 1,
31 => 1,
];
}

Expand All @@ -44,8 +43,6 @@ public function getWarningList() {
19 => 1,
20 => 1,
21 => 1,
32 => 1,
33 => 1,
];
}

Expand Down
6 changes: 3 additions & 3 deletions WordPressVIPMinimum/ruleset-test.inc
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,9 @@ chown(); // Error.
chmod(); // Error.
lchgrp(); // Error.
lchown(); // Error.
add_site_option( 'foo', $bar ); // Error.
update_site_option( $bar, $foo, true ); // Error.
delete_site_option( $foo ); // Error.



wp_mail(); // Warning.
mail(); // Warning.
is_multi_author(); // Warning.
Expand Down
3 changes: 0 additions & 3 deletions WordPressVIPMinimum/ruleset-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,6 @@
390 => 1,
391 => 1,
392 => 1,
393 => 1,
394 => 1,
395 => 1,
402 => 1,
415 => 1,
425 => 1,
Expand Down

0 comments on commit f503500

Please sign in to comment.