Skip to content

Commit

Permalink
Modify function to fix codacy Cyclomatic Complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
Khadreal committed Jan 22, 2025
1 parent 276232d commit 72a3ea0
Showing 1 changed file with 44 additions and 11 deletions.
55 changes: 44 additions & 11 deletions inc/classes/class-imagify-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,46 @@ public static function get_required_exist_wp_metadata_where_clause( $args = arra
return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ];
}

/**
* Prepare query arguments.
*
* @param array $args {
* Optional. An array of arguments.
*
* string $aliases The aliases to use for the meta values.
* bool $matching Set to false to get a query to fetch invalid metas.
* bool $test Test if the site has attachments without required metadata before returning the query. False to bypass the test and get the query anyway.
* bool $prepared Set to true if the query will be prepared with using $wpdb->prepare().
* }.
*
* @return array
*/
private function prepare_query_args( $args ) {
return imagify_merge_intersect( $args, [
'aliases' => [],
'matching' => true,
'test' => true,
'prepared' => false,
] );
}

/**
* Generate query.
*
* @param bool $matching Matching.
* @param string $alias Query alias.
* @param string $regex Query Regex.
*
* @return string
*/
private function generate_query( $matching, $alias, $regex ) {
if ( $matching ) {
return "REVERSE (LOWER( $alias.meta_value )) REGEXP '$regex'";
}

return "REVERSE (LOWER( $alias.meta_value )) NOT REGEXP '$regex'";
}

/**
* Get the SQL part to be used in a WHERE clause, to get only attachments that have a valid file extensions.
* It returns an empty string if the database has no attachments without the required metadada.
Expand All @@ -410,12 +450,9 @@ public static function get_extensions_where_clause( $args = false ) {
static $extensions;
static $query = array();

$args = imagify_merge_intersect( $args, array(
'alias' => array(),
'matching' => true,
'test' => true,
'prepared' => false,
) );
$instance = new self();

$args = $instance->prepare_query_args( $args );

list( $alias, $matching, $test, $prepared ) = array_values( $args );

Expand Down Expand Up @@ -445,11 +482,7 @@ public static function get_extensions_where_clause( $args = false ) {

$regex = '^' . implode( '\..*|^', $extensions ) . '\..*';

if ( $matching ) {
$query[ $key ] = "REVERSE (LOWER( $alias.meta_value )) REGEXP '$regex'";
} else {
$query[ $key ] = "REVERSE (LOWER( $alias.meta_value )) NOT REGEXP '$regex'";
}
$query[ $key ] = $instance->generate_query( $matching, $alias, $regex );

return $prepared ? str_replace( '%', '%%', $query[ $key ] ) : $query[ $key ];
}
Expand Down

0 comments on commit 72a3ea0

Please sign in to comment.