w3bdesign / wp-algolia-woo-indexer

✏️ WordPress plugin that sends WooCommerce products to Algolia. Used with headless eCommerce.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Refactoring

w3bdesign opened this issue · comments

Here are some suggestions for refactoring the code:

  1. Rename the class to follow the PHP naming convention - AlgoliaWooIndexer
  2. Extract constants to a separate class - AlgoliaWooIndexerConstants
  3. Extract functions to separate classes - AlgoliaWooIndexerSettings, AlgoliaWooIndexerNonce, etc.
  4. Use snake_case for function and variable names instead of camelCase
  5. Add docblock comments for all functions and variables
  6. Validate input in the functions where they are used, not in a global validate function
  7. Use self::class instead of get_called_class()
  8. Avoid static properties and methods where possible. Use dependency injection instead.
  9. Move activation/deactivation hooks to a separate file.

For example:

class AlgoliaWooIndexerConstants {
  const PLUGIN_NAME = 'Algolia Woo Indexer'; 
  // ... other constants
}

class AlgoliaWooIndexerSettings {
  public function algolia_woo_indexer_settings_page() {
    // Render settings page
  }
  
  public function validate_settings() {
   // Validate settings
  }
}

class AlgoliaWooIndexer {

  public function __construct(AlgoliaWooIndexerSettings $settings) {
    $this->settings = $settings;
  }
  
  public function do_something() {
    $this->settings->algolia_woo_indexer_settings_page();  
  }
}

Hope this helps! Let me know if you have any other questions.