Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed Oct 31, 2024
1 parent 7875c42 commit bc7fc8b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 120 deletions.
205 changes: 85 additions & 120 deletions classes/class-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,11 @@ class Base {
const SCORE_TARGET = 200;

/**
* An instance of the \Progress_Planner\Settings class.
* An array of instantiated objects.
*
* @var \Progress_Planner\Settings|null
* @var array<string, object>
*/
private $settings;

/**
* An instance of the Query class.
*
* @var \Progress_Planner\Query|null
*/
private $query;

/**
* An instance of the \Progress_Planner\Date class.
*
* @var \Progress_Planner\Date|null
*/
private $date;

/**
* An instance of the \Progress_Planner\Lessons class.
*
* @var \Progress_Planner\Lessons|null
*/
private $lessons;

/**
* An instance of the \Progress_Planner\Page_Types class.
*
* @var \Progress_Planner\Page_Types|null
*/
private $page_types;

/**
* An instance of the \Progress_Planner\Chart class.
*
* @var \Progress_Planner\Chart|null
*/
public $chart;

/**
* An instance of the \Progress_Planner\Suggested_Tasks class.
*
* @var \Progress_Planner\Suggested_Tasks|null
*/
private $suggested_tasks;

/**
* An instance of the \Progress_Planner\Todo class.
*
* @var \Progress_Planner\Todo|null
*/
private $todo;
private $cached = [];

/**
* An object containing all popovers.
Expand All @@ -82,13 +33,6 @@ class Base {
*/
private $popovers;

/**
* An object containing all badges.
*
* @var \Progress_Planner\Badges|null
*/
private $badges;

/**
* An instance of the \Progress_Planner\Admin\Page_Settings class.
*
Expand All @@ -109,27 +53,13 @@ class Base {
*/
private $admin;

/**
* An instance of the \Progress_Planner\Onboard class.
*
* @var \Progress_Planner\Onboard|null
*/
private $onboard;

/**
* An object containing actions classes.
*
* @var \stdClass|null
*/
private $actions;

/**
* An instance of the \Progress_Planner\Rest_API class.
*
* @var \Progress_Planner\Rest_API|null
*/
private $rest_api;

/**
* An instance of the \Progress_Planner\Cache class.
*
Expand Down Expand Up @@ -171,79 +101,114 @@ public function init() {
}
$this->get_admin()->editor = new \Progress_Planner\Admin\Editor();

$this->actions = new \stdClass();
$this->actions->content = new \Progress_Planner\Actions\Content();
$this->actions->content_scan = new \Progress_Planner\Actions\Content_Scan();
$this->actions->maintenance = new \Progress_Planner\Actions\Maintenance();
$this->cached['actions'] = new \stdClass();
$this->cached['actions']->content = new \Progress_Planner\Actions\Content();
$this->cached['actions']->content_scan = new \Progress_Planner\Actions\Content_Scan();
$this->cached['actions']->maintenance = new \Progress_Planner\Actions\Maintenance();

// REST API.
$this->rest_api = new Rest_API();
$this->cached['rest_api'] = new Rest_API();

// Onboarding.
$this->onboard = new Onboard();
$this->cached['onboard'] = new Onboard();

// To-do.
$this->todo = new Todo();
$this->cached['todo'] = new Todo();

\add_filter( 'plugin_action_links_' . plugin_basename( PROGRESS_PLANNER_FILE ), [ $this, 'add_action_links' ] );

// We need to initialize some classes early.
$this->page_types = new Page_Types();
$this->settings = new Settings();
$this->settings_page = new \Progress_Planner\Admin\Page_Settings();
$this->suggested_tasks = new Suggested_Tasks();
$this->cached['page_types'] = new Page_Types();
$this->cached['settings'] = new Settings();
$this->cached['settings_page'] = new \Progress_Planner\Admin\Page_Settings();
$this->cached['suggested_tasks'] = new Suggested_Tasks();
}

/**
* Get the popovers instance.
*
* @return \stdClass
*/
public function get_popovers() {
if ( ! $this->popovers ) {
$this->popovers = new \stdClass();
}
$this->popovers->badges = new \Progress_Planner\Popovers\Badges();
$this->popovers->settings = new \Progress_Planner\Popovers\Settings();

return $this->popovers;
}

/**
* Get the settings page instance.
*
* @return \Progress_Planner\Admin\Page_Settings
*/
public function get_settings_page() {
if ( ! $this->settings_page ) {
$this->settings_page = new \Progress_Planner\Admin\Page_Settings();
}
return $this->settings_page;
}

/** Get the helpers instance.
*
* @return \stdClass
*/
public function get_helpers() {
if ( ! $this->helpers ) {
$this->helpers = new \stdClass();
$this->helpers->content = new \Progress_Planner\Activities\Content_Helpers();
}
return $this->helpers;
}

/**
* Get the admin instance.
*
* @return \stdClass
*/
public function get_admin() {
if ( ! $this->admin ) {
$this->admin = new \stdClass();
}
return $this->admin;
}

/**
* Get the actions instance.
*
* @return \stdClass
*/
public function get_actions() {
if ( ! $this->actions ) {
$this->actions = new \stdClass();
}
return $this->actions;
}

/**
* Magic method to get properties.
* We use this to avoid a lot of code duplication.
*
* If we call $this->get_settings(), we need to check if $this->settings exists.
* If it exists and is null, we create a new instance of the `Settings` class.
*
* @param string $name The name of the property.
* @param array $arguments The arguments passed to the class constructor.
*
* @return mixed
*/
public function __call( $name, $arguments ) {
$map = [
'popovers' => [
'badges' => '\Progress_Planner\Popovers\Badges',
'settings' => '\Progress_Planner\Popovers\Settings',
],
'settings_page' => '\Progress_Planner\Admin\Page_Settings',
'helpers' => [
'content' => '\Progress_Planner\Activities\Content_Helpers',
],
'admin' => [],
'actions' => [],
];
$property_name = str_replace( 'get_', '', $name );
if ( ! property_exists( $this, $property_name ) ) {
return null;
$cache_name = str_replace( 'get_', '', $name );
if ( isset( $this->cached[ $cache_name ] ) ) {
return $this->cached[ $cache_name ];
}

if ( is_null( $this->$property_name ) ) {
if ( isset( $map[ $property_name ] ) ) {
if ( is_array( $map[ $property_name ] ) ) {
$this->$property_name = new \stdClass();
foreach ( $map[ $property_name ] as $key => $class_name ) {
if ( class_exists( $class_name ) ) {
$this->$property_name->$key = new $class_name( $arguments );
}
}
} elseif ( class_exists( $map[ $property_name ] ) ) {
$this->$property_name = new $map[ $property_name ]( $arguments );
}
} else {
$class_name = 'Progress_Planner\\' . implode( '_', array_map( 'ucfirst', explode( '_', $property_name ) ) );
if ( class_exists( $class_name ) ) {
$this->$property_name = new $class_name( $arguments );
}
if ( ! isset( $this->cached[ $cache_name ] ) ) {
$class_name = 'Progress_Planner\\' . implode( '_', array_map( 'ucfirst', explode( '_', $cache_name ) ) );
if ( class_exists( $class_name ) ) {
$this->cached[ $cache_name ] = new $class_name( $arguments );
return $this->cached[ $cache_name ];
}
}
return $this->$property_name;
}

/**
Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,13 @@ parameters:
- '#Method [a-zA-Z0-9\\_\:\(\)]+ return type has no value type specified in iterable type array.#'
- '#Method [a-zA-Z0-9\\_\:\(\)]+ has parameter \$[a-zA-Z0-9\\_]+ with no value type specified in iterable type array.#'
- '#Variable \$[a-zA-Z0-9\\_]+ might not be defined.#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_badges\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_chart\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_date\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_lessons\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_page_types\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_query\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_settings\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_suggested_tasks\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_todo\(\).#'
- '#Call to an undefined method Progress_Planner\\Base\:\:get_onboard\(\).#'

0 comments on commit bc7fc8b

Please sign in to comment.