Give_Settings_Addon   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 12.5 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 9
loc 72
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A set_default_setting_tab() 9 9 2
A add_settings_page() 0 9 2
A get_settings() 0 20 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Give Settings Page/Tab
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Give_Settings_Addon
7
 * @copyright   Copyright (c) 2016, GiveWP
8
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
9
 * @since       1.8
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit; // Exit if accessed directly
14
}
15
16
if ( ! class_exists( 'Give_Settings_Addon' ) ) :
17
18
	/**
19
	 * Give_Settings_Addon.
20
	 *
21
	 * @sine 1.8
22
	 */
23
	class Give_Settings_Addon extends Give_Settings_Page {
24
		/**
25
		 * Constructor.
26
		 */
27
		public function __construct() {
28
			$this->id    = 'addons';
29
			$this->label = esc_html__( 'Add-ons', 'give' );
30
31
			parent::__construct();
32
		}
33
34
		/**
35
		 * Default setting tab.
36
		 *
37
		 * @since  1.8
38
		 * @param  $setting_tab
39
		 * @return string
40
		 */
41 View Code Duplication
		function set_default_setting_tab( $setting_tab ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
			$default_tab = '';
43
44
			// Set default tab to first setting tab.
45
			if( $sections = array_keys( $this->get_sections() ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
46
				$default_tab = current( $sections );
47
			}
48
			return $default_tab;
49
		}
50
51
		/**
52
		 * Add this page to settings.
53
		 *
54
		 * @since  1.8
55
		 * @param  array $pages Lst of pages.
56
		 * @return array
57
		 */
58
		public function add_settings_page( $pages ) {
59
			$setting = $this->get_settings();
60
			// Bailout: Do not add addons setting tab if it does not contain any setting fields.
61
			if( ! empty( $setting ) ) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
62
				$pages[ $this->id ] = $this->label;
63
			}
64
65
			return $pages;
66
		}
67
68
		/**
69
		 * Get settings array.
70
		 *
71
		 * @since  1.8
72
		 * @return array
73
		 */
74
		public function get_settings() {
75
			$settings = array();
76
77
			/**
78
			 * Filter the addons settings.
79
			 * Backward compatibility: Please do not use this filter. This filter is deprecated in 1.8
80
			 */
81
			$settings = apply_filters( 'give_settings_addons', $settings );
82
83
			/**
84
			 * Filter the settings.
85
			 *
86
			 * @since  1.8
87
			 * @param  array $settings
88
			 */
89
			$settings = apply_filters( 'give_get_settings_' . $this->id, $settings );
90
91
			// Output.
92
			return $settings;
93
		}
94
	}
95
96
endif;
97
98
return new Give_Settings_Addon();
99