Skip to content

Commit 8a25fa6

Browse files
committed
IncludingFileSniff: add property of custom keywords to not trigger sniff
1 parent 1fa0266 commit 8a25fa6

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

WordPressVIPMinimum/Sniffs/Files/IncludingFileSniff.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
/**
1414
* WordPressVIPMinimum_Sniffs_Files_IncludingFileSniff.
1515
*
16-
* Checks that __DIR__, dirname( __FILE__ ) or plugin_dir_path( __FILE__ )
17-
* is used when including or requiring files.
16+
* Checks for custom variables, functions and constants, and external URLs used in file inclusion.
1817
*
1918
* @package VIPCS\WordPressVIPMinimum
2019
*/
@@ -55,6 +54,17 @@ class IncludingFileSniff extends AbstractFunctionRestrictionsSniff {
5554
'WP_PLUGIN_DIR',
5655
];
5756

57+
/**
58+
* List of keywords allowed for use in custom constants.
59+
* Note: Customizing this property will overwrite current default values.
60+
*
61+
* @var array
62+
*/
63+
public $allowedKeywords = [
64+
'PATH',
65+
'DIR',
66+
];
67+
5868
/**
5969
* Functions used for modify slashes.
6070
*
@@ -122,6 +132,11 @@ public function process_token( $stackPtr ) {
122132
return;
123133
}
124134

135+
if ( $this->has_custom_path( $this->tokens[ $nextToken ]['content'] ) === true ) {
136+
// The construct is using a constant with an allowed keyword.
137+
return;
138+
}
139+
125140
if ( array_key_exists( $this->tokens[ $nextToken ]['content'], $this->restrictedConstants ) === true ) {
126141
// The construct is using one of the restricted constants.
127142
$message = '`%s` constant might not be defined or available. Use `%s()` instead.';
@@ -172,4 +187,21 @@ public function process_token( $stackPtr ) {
172187
$message = 'Absolute include path must be used. Use `get_template_directory()`, `get_stylesheet_directory()` or `plugin_dir_path()`.';
173188
$this->phpcsFile->addError( $message, $nextToken, 'NotAbsolutePath' );
174189
}
190+
191+
/**
192+
* Check if a content string contains a keyword in custom paths.
193+
*
194+
* @param string $content Content string.
195+
*
196+
* @return bool True if the string partially matches a keyword in $allowedCustomKeywords, false otherwise.
197+
*/
198+
private function has_custom_path( $content ) {
199+
foreach ( $this->allowedKeywords as $keyword ) {
200+
if ( strpos( $content, $keyword ) !== false ) {
201+
return true;
202+
}
203+
}
204+
205+
return false;
206+
}
175207
}

WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.inc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ require_once "my_file.php"; // Not absolute path.
2525
require '../my_file.php'; // Not absolute path.
2626
require '../../my_file.php'; // Not absolute path.
2727
include( 'http://www.google.com/bad_file.php' ); // External URL.
28-
include_once("http://www.google.com/bad_file.php"); // External URL.
28+
include_once("http://www.google.com/bad_file.php"); // External URL.
29+
30+
// Allowed keywords
31+
include 'https://path.com/bad_file.php'; // Error - external URL with keyword from $allowedKeywords.
32+
require $path; // Warning - custom variable with keyword from $allowedKeywords.
33+
include_once dir_function(); // Error - custom functionm with keyword from $allowedKeywords.
34+
require CUSTOM_CONSTANT_DIR . 'file.php'; // OK.
35+
require_once ( VIPCS_PATH ) . 'file.php'; // OK.
36+
include_once
37+
DIR_CUSTOM , 'file.php'; // OK.

WordPressVIPMinimum/Tests/Files/IncludingFileUnitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public function getErrorList() {
2929
26 => 1,
3030
27 => 1,
3131
28 => 1,
32+
31 => 1,
3233
];
3334
}
3435

@@ -43,6 +44,8 @@ public function getWarningList() {
4344
19 => 1,
4445
20 => 1,
4546
21 => 1,
47+
32 => 1,
48+
33 => 1,
4649
];
4750
}
4851

0 commit comments

Comments
 (0)