Test Failed
Push — master ( 0a75e1...2c5ac2 )
by Ravinder
08:47
created

Give_Donation_Form_Grid_Block   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 186
Duplicated Lines 4.84 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A get_instance() 9 9 2
A init() 0 3 1
B register_block() 0 65 2
A render_block() 0 22 2
A blank_slate() 0 26 2

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 Donation Grid Block Class
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/Blocks
7
 * @copyright   Copyright (c) 2019, GiveWP
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       2.0.2
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Give_Donation_Form_Grid_Block Class.
19
 *
20
 * This class handles donation forms block.
21
 *
22
 * @since 2.0.2
23
 */
24
class Give_Donation_Form_Grid_Block {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
25
	/**
26
	 * Instance.
27
	 *
28
	 * @since
29
	 * @access private
30
	 * @var Give_Donation_Form_Grid_Block
31
	 */
32
	static private $instance;
33
34
	/**
35
	 * Singleton pattern.
36
	 *
37
	 * @since
38
	 * @access private
39
	 */
40
	private function __construct() {
41
	}
42
43
44
	/**
45
	 * Get instance.
46
	 *
47
	 * @since
48
	 * @access public
49
	 * @return Give_Donation_Form_Grid_Block
50
	 */
51 View Code Duplication
	public static function get_instance() {
0 ignored issues
show
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...
52
		if ( null === static::$instance ) {
53
			self::$instance = new static();
54
55
			self::$instance->init();
56
		}
57
58
		return self::$instance;
59
	}
60
61
	/**
62
	 * Class Constructor
63
	 *
64
	 * Set up the Give Donation Grid Block class.
65
	 *
66
	 * @since  2.0.2
67
	 * @access private
68
	 */
69
	private function init() {
70
		add_action( 'init', array( $this, 'register_block' ), 999 );
71
	}
72
73
	/**
74
	 * Register block
75
	 *
76
	 *
77
	 * @access public
78
	 */
79
	public function register_block() {
80
		// Bailout.
81
		if ( ! function_exists( 'register_block_type' ) ) {
82
			return;
83
		}
84
85
		// Register block.
86
		register_block_type( 'give/donation-form-grid', array(
87
			'render_callback' => array( $this, 'render_block' ),
88
			'attributes'      => array(
89
				'formsPerPage'      => array(
90
					'type'    => 'string',
91
					'default' => '12',
92
				),
93
				'formIDs'           => array(
94
					'type'    => 'string',
95
					'default' => '',
96
				),
97
				'excludedFormIDs'   => array(
98
					'type'    => 'string',
99
					'default' => '',
100
				),
101
				'orderBy'           => array(
102
					'type'    => 'string',
103
					'default' => 'date',
104
				),
105
				'order'             => array(
106
					'type'    => 'string',
107
					'default' => 'DESC',
108
				),
109
				'categories'        => array(
110
					'type'    => 'string',
111
					'default' => '',
112
				),
113
				'tags'              => array(
114
					'type'    => 'string',
115
					'default' => '',
116
				),
117
				'columns'           => array(
118
					'type'    => 'string',
119
					'default' => 'best-fit',
120
				),
121
				'showTitle'         => array(
122
					'type'    => 'boolean',
123
					'default' => true,
124
				),
125
				'showExcerpt'       => array(
126
					'type'    => 'boolean',
127
					'default' => true,
128
				),
129
				'showGoal'          => array(
130
					'type'    => 'boolean',
131
					'default' => true,
132
				),
133
				'showFeaturedImage' => array(
134
					'type'    => 'boolean',
135
					'default' => true,
136
				),
137
				'displayType'       => array(
138
					'type'    => 'string',
139
					'default' => 'redirect',
140
				),
141
			),
142
		) );
143
	}
144
145
	/**
146
	 * Block render callback
147
	 *
148
	 * @param array $attributes Block parameters.
149
	 *
150
	 * @access public
151
	 * @return string;
0 ignored issues
show
Documentation introduced by
The doc-type string; could not be parsed: Expected "|" or "end of type", but got ";" at position 6. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
152
	 */
153
	public function render_block( $attributes ) {
154
		$parameters = array(
155
			'forms_per_page'      => absint( $attributes['formsPerPage'] ),
156
			'ids'                 => $attributes['formIDs'],
157
			'exclude'             => $attributes['excludedFormIDs'],
158
			'orderby'             => $attributes['orderBy'],
159
			'order'               => $attributes['order'],
160
			'cats'                => $attributes['categories'],
161
			'tags'                => $attributes['tags'],
162
			'columns'             => $attributes['columns'],
163
			'show_title'          => $attributes['showTitle'],
164
			'show_goal'           => $attributes['showGoal'],
165
			'show_excerpt'        => $attributes['showExcerpt'],
166
			'show_featured_image' => $attributes['showFeaturedImage'],
167
			'display_type'        => $attributes['displayType'],
168
		);
169
170
		$html = give_form_grid_shortcode( $parameters );
171
		$html = ! empty( $html ) ? $html : $this->blank_slate();
172
173
		return $html;
174
	}
175
176
	/**
177
	 * Return formatted notice when shortcode return empty string
178
	 *
179
	 * @since 2.4.0
180
	 *
181
	 * @return string
182
	 */
183
	private function blank_slate(){
184
		if( ! defined( 'REST_REQUEST' ) ) {
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...
185
			return '';
186
		}
187
188
		ob_start();
189
190
		$content = array(
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
191
			'image_url' => GIVE_PLUGIN_URL . 'assets/dist/images/give-icon-full-circle.svg',
192
			'image_alt' => __( 'Give Icon', 'give' ),
193
			'heading'   => __( 'No donation forms found.', 'give' ),
194
			'message'   => __( 'The first step towards accepting online donations is to create a form.', 'give' ),
195
			'cta_text'  => __( 'Create Donation Form', 'give' ),
196
			'cta_link'  => admin_url( 'post-new.php?post_type=give_forms' ),
197
			'help'      => sprintf(
198
				/* translators: 1: Opening anchor tag. 2: Closing anchor tag. */
199
				__( 'Need help? Get started with %1$sGive 101%2$s.', 'give' ),
200
				'<a href="http://docs.givewp.com/give101/" target="_blank">',
201
				'</a>'
202
			),
203
		);
204
205
		include_once GIVE_PLUGIN_DIR . 'includes/admin/views/blank-slate.php';
206
207
		return ob_get_clean();
208
	}
209
}
210
211
Give_Donation_Form_Grid_Block::get_instance();
212