| Total Complexity | 49 | 
| Total Lines | 405 | 
| Duplicated Lines | 0 % | 
| Changes | 19 | ||
| Bugs | 0 | Features | 8 | 
Complex classes like Algolia_Send_Products often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Algolia_Send_Products, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 84 | class Algolia_Send_Products  | 
            ||
| 85 |     { | 
            ||
| 86 | const PLUGIN_NAME = 'Algolia Woo Indexer';  | 
            ||
| 87 | const PLUGIN_TRANSIENT = 'algowoo-plugin-notice';  | 
            ||
| 88 | |||
| 89 | /**  | 
            ||
| 90 | * The Algolia instance  | 
            ||
| 91 | *  | 
            ||
| 92 | * @var \Algolia\AlgoliaSearch\SearchClient  | 
            ||
| 93 | */  | 
            ||
| 94 | private static $algolia = null;  | 
            ||
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * Check if we can connect to Algolia, if not, handle the exception, display an error and then return  | 
            ||
| 98 | */  | 
            ||
| 99 | public static function can_connect_to_algolia()  | 
            ||
| 100 |         { | 
            ||
| 101 |             try { | 
            ||
| 102 | self::$algolia->listApiKeys();  | 
            ||
| 103 |             } catch (\Algolia\AlgoliaSearch\Exceptions\UnreachableException $error) { | 
            ||
| 104 | add_action(  | 
            ||
| 105 | 'admin_notices',  | 
            ||
| 106 |                     function () { | 
            ||
| 107 | echo '<div class="error notice">  | 
            ||
| 108 |                             <p>' . esc_html__('An error has been encountered. Please check your application ID and API key. ', 'algolia-woo-indexer') . '</p> | 
            ||
| 109 | </div>';  | 
            ||
| 110 | }  | 
            ||
| 111 | );  | 
            ||
| 112 | return;  | 
            ||
| 113 | }  | 
            ||
| 114 | }  | 
            ||
| 115 | |||
| 116 | /**  | 
            ||
| 117 | * check if the field is enabled and shall be sent  | 
            ||
| 118 | *  | 
            ||
| 119 | * @param mixed $field name of field to be checked according to BASIC_FIELDS  | 
            ||
| 120 | * @return boolean true if enable, false is not enabled  | 
            ||
| 121 | */  | 
            ||
| 122 | public static function is_basic_field_enabled($field)  | 
            ||
| 123 |         { | 
            ||
| 124 | $fieldValue = get_option(ALGOWOO_DB_OPTION . BASIC_FIELD_PREFIX . $field);  | 
            ||
| 125 | return $fieldValue;  | 
            ||
| 126 | }  | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * helper function to add a field to a record while checking their state  | 
            ||
| 130 | *  | 
            ||
| 131 | * @param array $record existing record where the field and value shall be added to  | 
            ||
| 132 | * @param string $field name of field to be checked according to BASIC_FIELDS  | 
            ||
| 133 | * @param mixed $value data to be added to the record array named to $field  | 
            ||
| 134 | * @param boolean $skip_basic_field_validation set to true if it is not a basic field to skip validation  | 
            ||
| 135 | * @return array $record previous passed $record with added field data  | 
            ||
| 136 | */  | 
            ||
| 137 | public static function add_to_record($record, $field, $value, $skip_basic_field_validation = false)  | 
            ||
| 138 |         { | 
            ||
| 139 | /**  | 
            ||
| 140 | * only if enabled or validation skipped and not empty  | 
            ||
| 141 | */  | 
            ||
| 142 |             if ((!self::is_basic_field_enabled($field) && !$skip_basic_field_validation) || empty($value)) { | 
            ||
| 143 | return $record;  | 
            ||
| 144 | }  | 
            ||
| 145 | |||
| 146 | $record[$field] = $value;  | 
            ||
| 147 | return $record;  | 
            ||
| 148 | }  | 
            ||
| 149 | |||
| 150 | /**  | 
            ||
| 151 | * Get sale price or regular price based on product type  | 
            ||
| 152 | *  | 
            ||
| 153 | * @param mixed $product Product to check  | 
            ||
| 154 | * @return array ['sale_price' => $sale_price,'regular_price' => $regular_price] Array with regular price and sale price  | 
            ||
| 155 | */  | 
            ||
| 156 | public static function get_product_type_price($product)  | 
            ||
| 157 |         { | 
            ||
| 158 | $sale_price = 0;  | 
            ||
| 159 | $regular_price = 0;  | 
            ||
| 160 |             if ($product->is_type('simple')) { | 
            ||
| 161 | $sale_price = $product->get_sale_price();  | 
            ||
| 162 | $regular_price = $product->get_regular_price();  | 
            ||
| 163 |             } elseif ($product->is_type('variable')) { | 
            ||
| 164 |                 $sale_price     =  $product->get_variation_sale_price('min', true); | 
            ||
| 165 |                 $regular_price  =  $product->get_variation_regular_price('max', true); | 
            ||
| 166 | }  | 
            ||
| 167 | return array(  | 
            ||
| 168 | 'sale_price' => $sale_price,  | 
            ||
| 169 | 'regular_price' => $regular_price  | 
            ||
| 170 | );  | 
            ||
| 171 | }  | 
            ||
| 172 | |||
| 173 | |||
| 174 | /**  | 
            ||
| 175 | * Checks if stock management is enabled and if so, returns quantity and status  | 
            ||
| 176 | *  | 
            ||
| 177 | * @param mixed $product Product to check  | 
            ||
| 178 | * @return array ['stock_quantity' => $stock_quantity,'stock_status' => $stock_status] Array with quantity and status. if stock management is disabled, false will be returned,  | 
            ||
| 179 | */  | 
            ||
| 180 | public static function get_product_stock_data($product)  | 
            ||
| 181 |         { | 
            ||
| 182 |             if ($product->get_manage_stock()) { | 
            ||
| 183 | return array(  | 
            ||
| 184 | 'stock_quantity' => $product->get_stock_quantity(),  | 
            ||
| 185 | 'stock_status' => $product->get_stock_status()  | 
            ||
| 186 | );  | 
            ||
| 187 | }  | 
            ||
| 188 | return false;  | 
            ||
| 189 | }  | 
            ||
| 190 | |||
| 191 | /**  | 
            ||
| 192 | * Get product tags  | 
            ||
| 193 | *  | 
            ||
| 194 | * @param mixed $product Product to check  | 
            ||
| 195 | * @return array ['tag1', 'tag2', ...] simple array with associated tags  | 
            ||
| 196 | */  | 
            ||
| 197 | public static function get_product_tags($product)  | 
            ||
| 198 |         { | 
            ||
| 199 | $tags = get_the_terms($product->get_id(), 'product_tag');  | 
            ||
| 200 | $term_array = array();  | 
            ||
| 201 |             if (is_array($tags)) { | 
            ||
| 202 |                 foreach ($tags as $tag) { | 
            ||
| 203 | $name = get_term($tag)->name;  | 
            ||
| 204 | array_push($term_array, $name);  | 
            ||
| 205 | }  | 
            ||
| 206 | }  | 
            ||
| 207 | return $term_array;  | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 | /**  | 
            ||
| 211 | * Get product categories  | 
            ||
| 212 | *  | 
            ||
| 213 | * @param mixed $product Product to check  | 
            ||
| 214 | * @return array ['tag1', 'tag2', ...] simple array with associated categories  | 
            ||
| 215 | */  | 
            ||
| 216 | public static function get_product_categories($product)  | 
            ||
| 217 |         { | 
            ||
| 218 | $categories = get_the_terms($product->get_id(), 'product_cat');  | 
            ||
| 219 | $term_array = array();  | 
            ||
| 220 |             foreach ($categories as $category) { | 
            ||
| 221 | $name = get_term($category)->name;  | 
            ||
| 222 | $slug = get_term($category)->slug;  | 
            ||
| 223 | array_push($term_array, array(  | 
            ||
| 224 | "name" => $name,  | 
            ||
| 225 | "slug" => $slug  | 
            ||
| 226 | ));  | 
            ||
| 227 | }  | 
            ||
| 228 | return $term_array;  | 
            ||
| 229 | }  | 
            ||
| 230 | |||
| 231 | /**  | 
            ||
| 232 | * Get attributes from product  | 
            ||
| 233 | *  | 
            ||
| 234 | * @param mixed $product Product to check  | 
            ||
| 235 | * @return array ['pa_name' => ['value1', 'value2']] Array with key set to the product attribute internal name and values as array. returns false if not attributes found.  | 
            ||
| 236 | */  | 
            ||
| 237 | public static function get_product_attributes($product)  | 
            ||
| 334 | }  | 
            ||
| 335 | |||
| 336 | /**  | 
            ||
| 337 | * Send WooCommerce products to Algolia  | 
            ||
| 338 | *  | 
            ||
| 339 | * @param Int $id Product to send to Algolia if we send only a single product  | 
            ||
| 340 | * @return void  | 
            ||
| 341 | */  | 
            ||
| 342 | public static function send_products_to_algolia($id = '')  | 
            ||
| 489 | </div>';  | 
            ||
| 490 | }  | 
            ||
| 491 | }  | 
            ||
| 492 | }  | 
            ||
| 493 | 
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths