Test Failed
Push — issues/1227 ( 92c1d9 )
by Ravinder
06:04
created

Give_DB_Donor_Meta   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 20.49 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 25
loc 122
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A get_columns() 0 8 1
A create_table() 18 18 1
A bc_200_params() 7 14 3
A is_valid_post_type() 0 3 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
 * Donor Meta DB class
4
 *
5
 * @package     Give
6
 * @subpackage  Classes/DB Donor Meta
7
 * @copyright   Copyright (c) 2016, WordImpress
8
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
9
 * @since       1.6
10
 */
11
12
// Exit if accessed directly.
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * Class Give_DB_Donor_Meta
19
 *
20
 * This class is for interacting with the donor meta database table.
21
 *
22
 * @since 1.6
23
 */
24
class Give_DB_Donor_Meta extends Give_DB_Meta {
25
26
	/**
27
	 * Meta type
28
	 *
29
	 * @since  2.0
30
	 * @access public
31
	 *
32
	 * @var string
33
	 */
34
	public $meta_type = 'donor';
35
36
	/**
37
	 * Meta supports.
38
	 *
39
	 * @since  2.0
40
	 * @access protected
41
	 * @var array
42
	 */
43
	protected $supports = array();
44
45
	/**
46
	 * Give_DB_Donor_Meta constructor.
47
	 *
48
	 * @access  public
49
	 * @since   1.6
50
	 */
51
	public function __construct() {
52
		/* @var WPDB $wpdb */
53
		global $wpdb;
54
55
		$wpdb->donormeta   = $this->table_name = $wpdb->prefix . 'give_donormeta';
56
		$this->primary_key = 'meta_id';
57
		$this->version     = '1.0';
58
59
		parent::__construct();
60
61
		$this->bc_200_params();
62
		$this->register_table();
63
	}
64
65
	/**
66
	 * Get table columns and data types.
67
	 *
68
	 * @access  public
69
	 * @since   1.6
70
	 *
71
	 * @return  array  Columns and formats.
72
	 */
73
	public function get_columns() {
74
		return array(
75
			'meta_id'     => '%d',
76
			'donor_id' => '%d',
77
			'meta_key'    => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
78
			'meta_value'  => '%s',
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
79
		);
80
	}
81
82
	/**
83
	 * Create the table
84
	 *
85
	 * @access public
86
	 * @since  1.6
87
	 *
88
	 * @return void
89
	 */
90 View Code Duplication
	public function create_table() {
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...
91
92
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
93
94
		$sql = "CREATE TABLE {$this->table_name} (
95
			meta_id bigint(20) NOT NULL AUTO_INCREMENT,
96
			donor_id bigint(20) NOT NULL,
97
			meta_key varchar(255) DEFAULT NULL,
98
			meta_value longtext,
99
			PRIMARY KEY  (meta_id),
100
			KEY donor_id (donor_id),
101
			KEY meta_key (meta_key)
102
			) CHARACTER SET utf8 COLLATE utf8_general_ci;";
103
104
		dbDelta( $sql );
105
106
		update_option( $this->table_name . '_db_version', $this->version );
107
	}
108
109
	/**
110
	 * Add backward compatibility for old table name
111
	 *
112
	 * @since  2.0
113
	 * @access private
114
	 * @global wpdb $wpdb
115
	 */
116
	private function bc_200_params() {
117
		/* @var wpdb $wpdb */
118
		global $wpdb;
119
120 View Code Duplication
		if (
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...
121
			! give_has_upgrade_completed( 'v20_rename_donor_tables' ) &&
122
			$wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s","{$wpdb->prefix}give_customermeta" ) )
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
Coding Style Comprehensibility introduced by
The string literal SHOW TABLES LIKE %s does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
123
		) {
124
			$wpdb->donormeta = $this->table_name = "{$wpdb->prefix}give_customermeta";
125
			$this->meta_type = 'customer';
126
		}
127
128
		$wpdb->customermeta = $wpdb->donormeta;
129
	}
130
131
	/**
132
	 * Check if current id is valid
133
	 *
134
	 * @since  2.0
135
	 * @access protected
136
	 *
137
	 * @param $ID
138
	 *
139
	 * @return bool
140
	 */
141
	protected function is_valid_post_type( $ID ) {
142
		return $ID && true;
143
	}
144
145
}
146