Skip to content

Commit

Permalink
Add files_preg_match_all helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Jun 24, 2024
1 parent 7a8843b commit 3430e85
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
40 changes: 39 additions & 1 deletion includes/Checker/Checks/Abstract_File_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ final protected static function file_preg_match( $pattern, array $files, array &
/**
* Returns matched files performing a regular expression match on the file contents of the given list of files.
*
* @since n.e.x.t
* @since 1.0.2
*
* @param string $pattern The pattern to search for.
* @param array $files List of absolute file paths.
Expand All @@ -166,6 +166,44 @@ final protected static function files_preg_match( $pattern, array $files ) {
return count( $matched_files ) > 0 ? $matched_files : false;
}

/**
* Returns matched files performing a regular expression match on the file contents of the given list of files with line and column information.
*
* @since 1.0.2
*
* @param string $pattern The pattern to search for.
* @param array $files List of absolute file paths.
* @return array|bool Array of file paths and matched string/pattern if matches were found, false otherwise.
*/
final protected static function files_preg_match_all( $pattern, array $files ) {
$matched_files = array();

foreach ( $files as $file ) {
$matches = array();

$contents = self::file_get_contents( $file );

preg_match_all( $pattern, $contents, $matches, PREG_OFFSET_CAPTURE );

if ( is_array( $matches ) && ! empty( $matches ) ) {
foreach ( $matches[0] as $match ) {
list( $before ) = str_split( $contents, $match[1] );

$exploded = explode( PHP_EOL, $before );
$last_item = end( $exploded );

$matched_files[] = array(
'file' => $file,
'line' => count( $exploded ),
'column' => strlen( $last_item ) + 1,
);
}
}
}

return count( $matched_files ) > 0 ? $matched_files : false;
}

/**
* Performs a check indicating if the needle is contained in the file contents of the given list of files.
*
Expand Down
6 changes: 4 additions & 2 deletions includes/Checker/Checks/Localhost_Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ public function get_categories() {
*/
protected function check_files( Check_Result $result, array $files ) {
$php_files = self::filter_files_by_extension( $files, 'php' );
$files = self::files_preg_match( '#https?://(localhost|127.0.0.1)#', $php_files );
$files = self::files_preg_match_all( '#https?://(localhost|127.0.0.1)#', $php_files );

if ( ! empty( $files ) ) {
foreach ( $files as $file ) {
$this->add_result_error_for_file(
$result,
__( 'Do not use Localhost/127.0.0.1 in your code.', 'plugin-check' ),
'localhost_code_detected',
$file[0]
$file['file'],
$file['line'],
$file['column']
);
}
}
Expand Down

0 comments on commit 3430e85

Please sign in to comment.