Test Failed
Pull Request — master (#2551)
by Devin
04:51
created

Give_Gateway_Reports_Table::column_default()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 58
Code Lines 41

Duplication

Lines 30
Ratio 51.72 %

Importance

Changes 0
Metric Value
cc 7
eloc 41
nc 7
nop 2
dl 30
loc 58
rs 7.6045
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Gateways Reports Table Class
4
 *
5
 * @package     Give
6
 * @subpackage  Admin/Reports
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.0
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
// Load WP_List_Table if not loaded
18
if ( ! class_exists( 'WP_List_Table' ) ) {
19
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
20
}
21
22
/**
23
 * Give_Gateway_Reports_Table Class
24
 *
25
 * Renders the Download Reports table
26
 *
27
 * @since 1.0
28
 */
29
class Give_Gateway_Reports_Table extends WP_List_Table {
30
31
	/**
32
	 * @var int Number of items per page
33
	 * @since 1.0
34
	 */
35
	public $per_page = 30;
36
37
38
	/**
39
	 * Get things started
40
	 *
41
	 * @since 1.0
42
	 * @see   WP_List_Table::__construct()
43
	 */
44 View Code Duplication
	public function __construct() {
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...
45
		global $status, $page;
46
47
		// Set parent defaults
48
		parent::__construct( array(
49
			'singular' => give_get_forms_label_singular(),    // Singular name of the listed records
50
			'plural'   => give_get_forms_label_plural(),        // Plural name of the listed records
51
			'ajax'     => false                        // Does this table support ajax?
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
52
		) );
53
54
	}
55
56
	/**
57
	 * This function renders most of the columns in the list table.
58
	 *
59
	 * @access public
60
	 * @since  1.0
61
	 *
62
	 * @param array  $item        Contains all the data of the form
63
	 * @param string $column_name The name of the column
64
	 *
65
	 * @return string Column Name
66
	 */
67
	public function column_default( $item, $column_name ) {
68
		$donation_list_page_url = admin_url( 'edit.php?post_type=give_forms&page=give-payment-history' );
69
70
		switch ( $column_name ) {
71 View Code Duplication
			case 'complete_sales':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72
				$value = $item[ $column_name ] ?
73
					sprintf(
74
						'<a href="%s">%s</a>',
75
						add_query_arg(
76
							array(
77
								'status'  => 'publish',
78
								'gateway' => $item['ID']
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
79
							),
80
							$donation_list_page_url
81
						),
82
						$item[ $column_name ]
83
					) :
84
					$item[ $column_name ];
85
				break;
86
87 View Code Duplication
			case 'pending_sales':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
88
				$value = $item[ $column_name ] ?
89
					sprintf(
90
						'<a href="%s">%s</a>',
91
						add_query_arg(
92
							array(
93
								'status'  => 'pending',
94
								'gateway' => $item['ID']
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
95
							),
96
							$donation_list_page_url
97
						),
98
						$item[ $column_name ]
99
					) :
100
					$item[ $column_name ];
101
				break;
102
103
			case 'total_sales':
104
				$value = $item[ $column_name ] ?
105
					sprintf(
106
						'<a href="%s">%s</a>',
107
						add_query_arg(
108
							array(
109
								'gateway' => $item['ID']
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
110
							),
111
							$donation_list_page_url
112
						),
113
						$item[ $column_name ]
114
					) :
115
					$item[ $column_name ];
116
117
				break;
118
119
			default:
120
				$value = $item[ $column_name ];
121
		}
122
123
		return $value;
124
	}
125
126
	/**
127
	 * Retrieve the table columns
128
	 *
129
	 * @access public
130
	 * @since  1.0
131
	 * @return array $columns Array of all the list table columns
132
	 */
133
	public function get_columns() {
134
		$columns = array(
135
			'label'           => esc_attr__( 'Gateway', 'give' ),
136
			'complete_sales'  => esc_attr__( 'Complete Payments', 'give' ),
137
			'pending_sales'   => esc_attr__( 'Pending / Failed Payments', 'give' ),
138
			'total_sales'     => esc_attr__( 'Total Payments', 'give' ),
139
			'total_donations' => esc_attr__( 'Total Donated', 'give' )
140
		);
141
142
		return $columns;
143
	}
144
145
	/**
146
	 * Get the sortable columns
147
	 *
148
	 * @access public
149
	 * @since  1.8.12
150
	 * @return array Array of all the sortable columns
151
	 */
152
	public function get_sortable_columns() {
153
		return array(
154
			'total_donations' => array( 'total_donations', false )
155
		);
156
	}
157
158
159
	/**
160
	 * Retrieve the current page number
161
	 *
162
	 * @access public
163
	 * @since  1.0
164
	 * @return int Current page number
165
	 */
166
	public function get_paged() {
167
		return isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 1;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
168
	}
169
170
171
	/**
172
	 * Outputs the reporting views
173
	 *
174
	 * @access public
175
	 * @since  1.0
176
	 * @return void
177
	 */
178
	public function bulk_actions( $which = '' ) {
179
180
	}
181
182
	/**
183
	 * Generate the table navigation above or below the table
184
	 *
185
	 * @since  1.0
186
	 * @access protected
187
	 *
188
	 * @param string $which
189
	 */
190 View Code Duplication
	protected function display_tablenav( $which ) {
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...
191
192
		if ( 'top' === $which ) {
193
			wp_nonce_field( 'bulk-' . $this->_args['plural'] );
194
		}
195
		?>
196
		<div class="tablenav gateways-report-tablenav give-clearfix <?php echo esc_attr( $which ); ?>">
197
198
			<?php if ( 'top' === $which ) { ?>
199
				<h2 class="alignleft reports-earnings-title screen-reader-text">
200
					<?php _e( 'Donation Methods Report', 'give' ); ?>
201
				</h2>
202
			<?php } ?>
203
204
			<div class="alignright tablenav-right">
205
				<div class="actions bulkactions">
206
					<?php $this->bulk_actions( $which ); ?>
207
				</div>
208
				<?php
209
				$this->extra_tablenav( $which );
210
				$this->pagination( $which );
211
				?>
212
			</div>
213
214
215
			<br class="clear" />
216
217
		</div>
218
		<?php
219
	}
220
221
	/**
222
	 * Reorder User Defined Array
223
	 *
224
	 * @param $old_value
225
	 * @param $new_value
226
	 *
227
	 * @access public
228
	 * @since  1.8.12
229
	 *
230
	 * @return int
231
	 */
232
	public function give_sort_total_donations( $old_value, $new_value ) {
233
		// If no sort, default to label.
234
		$orderby = ( ! empty( $_REQUEST['orderby'] ) ) ? $_REQUEST['orderby'] : 'label';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
235
236
		//If no order, default to asc.
237
		$order = ( ! empty( $_REQUEST['order'] ) ) ? $_REQUEST['order'] : 'asc';
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
238
239
		//Determine sort order.
240
		$result = strcmp( $old_value[ $orderby ], $new_value[ $orderby ] );
241
242
		return ( $order === 'asc' ) ? $result : -$result;
243
	}
244
245
246
	/**
247
	 * Build all the reports data
248
	 *
249
	 * @access public
250
	 * @since  1.0
251
	 * @return array $reports_data All the data for donor reports
252
	 */
253
	public function reports_data() {
254
255
		$reports_data = array();
256
		$gateways     = give_get_payment_gateways();
257
		$stats        = new Give_Payment_Stats();
258
259
		foreach ( $gateways as $gateway_id => $gateway ) {
260
261
			$complete_count = give_count_sales_by_gateway( $gateway_id, 'publish' );
262
			$pending_count  = give_count_sales_by_gateway( $gateway_id, array( 'pending', 'failed' ) );
0 ignored issues
show
Documentation introduced by
array('pending', 'failed') is of type array<integer,string,{"0":"string","1":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
263
264
			$reports_data[] = array(
265
				'ID'              => $gateway_id,
266
				'label'           => $gateway['admin_label'],
267
				'complete_sales'  => $complete_count,
268
				'pending_sales'   => $pending_count,
269
				'total_sales'     => $complete_count + $pending_count,
270
				'total_donations' => give_currency_filter( give_format_amount( $stats->get_earnings( 0, strtotime('04/13/2015' ), current_time('timestamp' ), $gateway_id ), array( 'sanitize' => false ) ) ),
0 ignored issues
show
Documentation introduced by
strtotime('04/13/2015') is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$gateway_id is of type integer|string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
271
			);
272
		}
273
274
		return $reports_data;
275
	}
276
277
	/**
278
	 * Setup the final data for the table
279
	 *
280
	 * @access public
281
	 * @since  1.0
282
	 * @uses   Give_Gateway_Reports_Table::get_columns()
283
	 * @uses   Give_Gateway_Reports_Table::get_sortable_columns()
284
	 * @uses   Give_Gateway_Reports_Table::reports_data()
285
	 * @return void
286
	 */
287
	public function prepare_items() {
288
		$columns               = $this->get_columns();
289
		$hidden                = array(); // No hidden columns
290
		$sortable              = $this->get_sortable_columns();
291
		$this->_column_headers = array( $columns, $hidden, $sortable );
292
		$this->items           = $this->reports_data();
293
294
		// Sort Array when we are sorting data in array.
295
		usort( $this->items, array( $this, 'give_sort_total_donations' ) );
296
297
	}
298
}
299