Term   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 209
Duplicated Lines 20.57 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 43
loc 209
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
B create_objects() 25 25 4
B create_test_object() 0 34 2
A delete_all() 0 18 3
C delete() 18 69 7

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
namespace DummyPress\Types;
3
use DummyPress\Abstracts as Abs;
4
use DummyPress\TestContent as TestContent;
5
use DummyPress\Delete as Delete;
6
7
8
/**
9
 * Class to build test data for terms.
10
 *
11
 * @package    WordPress
12
 * @subpackage Evans
13
 * @author     Mike Selander
14
 */
15
class Term extends Abs\Type {
16
17
	/**
18
	 * type
19
	 * Defines type slug for use elsewhere in the plugin
20
	 *
21
	 * @var string
22
	 * @access protected
23
	 */
24
	protected $type = 'term';
25
26
	/**
27
	 * Create test data posts.
28
	 *
29
	 * This is where the magic begins. We accept a cpt id (slug) and potntially
30
	 * a number of posts to create. We then fetch the supports & metaboxes
31
	 * for that cpt and feed them into a function to create each post individually.
32
	 *
33
	 * @access private
34
	 *
35
	 * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object
36
	 *
37
	 * @param string $slug a custom post type ID.
38
	 * @param int $num Optional. Number of posts to create.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $num not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
39
	 */
40 View Code Duplication
	public function create_objects( $slug, $connection, $num = '' ) {
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...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
41
42
		// If we're missing a custom post type id - don't do anything
43
		if ( empty( $slug ) ) {
44
			return;
45
		}
46
47
		// Set our connection status for the rest of the methods
48
		$this->connected = $connection;
49
50
		// If we forgot to put in a quantity, make one for us
51
		if ( empty( $num ) ) {
52
			$num = rand( 5, 30 );
53
		}
54
55
		// Create test terms
56
		for( $i = 0; $i < $num; $i++ ) {
0 ignored issues
show
Unused Code introduced by
$i++; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
57
58
			$return = $this->create_test_object( $slug );
59
60
			return $return;
61
62
		}
63
64
	}
65
66
67
	/**
68
	 * Creates the individual test data object.
69
	 *
70
	 * Create individual posts for testing with. Gathers basic information such
71
	 * as title, content, thumbnail, etc. and inserts them with the post. Also
72
	 * adds metaboxes if applicable .
73
	 *
74
	 * @access private
75
	 *
76
	 * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content
77
	 *
78
	 * @param string $slug a custom post type ID.
79
	 */
80
	private function create_test_object( $slug ) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
81
82
		// Get a random title
83
		$title = apply_filters( "tc_{$slug}_term_title", TestContent::title() );
84
85
		$return = wp_insert_term(
86
			$title,
87
			$slug,
88
			apply_filters( "tc_{$slug}_term_arguments", array(
89
					'description'=> TestContent::title(),
90
					'slug' => sanitize_title( $title ),
91
				)
92
			)
93
		);
94
95
		// Then, set a test content flag on the new post for later deletion
96
		add_term_meta( $return['term_id'], 'dummypress_test_data', '__test__', true );
97
98
		// Check if we have errors and return them or created message
99
		if ( is_wp_error( $return ) ) {
100
			error_log( $return->get_error_message() );
101
			return $return;
102
		} else {
103
			return array(
104
				'action'	=> 'created',
105
				'object'	=> 'term',
106
				'oid'		=> $return['term_id'],
107
				'type'		=> $slug,
108
				'link_edit'	=> admin_url( '/edit-tags.php?action=edit&taxonomy='.$slug.'&tag_ID='.$return['term_id'] ),
109
				'link_view'	=> get_term_link( $return['term_id'] )
110
			);
111
		}
112
113
	}
114
115
116
117
	/**
118
	 * Delete all test data, regardless of type, within terms.
119
	 *
120
	 * @see Delete
121
	 */
122
	public function delete_all() {
123
124
		$delete =  new Delete;
125
126
		// Make sure that the current user is logged in & has full permissions.
127
		if ( ! $delete->user_can_delete() ) {
128
			return;
129
		}
130
131
		// Loop through all taxonomies and remove any data
132
		$taxonomies = get_taxonomies();
133
		foreach ( $taxonomies as $tax ) :
134
135
			$this->delete( $tax );
136
137
		endforeach;
138
139
	}
140
141
142
	/**
143
	 * Delete test data terms.
144
	 *
145
	 * This function will search for all terms of a particular taxonomy ($slug)
146
	 * and delete them all using a particular term_meta flag that we set when creating
147
	 * the posts. Validates the user first.
148
	 *
149
	 * @see WP_Query, wp_delete_post
150
	 *
151
	 * @param string $slug a custom post type ID.
152
	 */
153
	public function delete( $slug ) {
154
155
		$delete =  new Delete;
156
157
		// Make sure that the current user is logged in & has full permissions.
158
		if ( ! $delete->user_can_delete() ) {
159
			return;
160
		}
161
162
		// Check that $cptslg has a string.
163
		if ( empty( $slug ) ) {
164
			return;
165
		}
166
167
		// Query for our terms
168
		$args = array(
169
		    'hide_empty' => false,
170
		    'meta_query' => array(
171
				'relation'		=> 'OR',
172
		        array(
173
		           'key'       => 'dummypress_test_data',
174
		           'value'     => '__test__',
175
		           'compare'   => '='
176
			   ),
177
			   array(
178
				  'key'       => 'evans_test_content',
179
				  'value'     => '__test__',
180
				  'compare'   => '='
181
			  ),
182
		    )
183
		);
184
185
		$terms = get_terms( $slug, $args );
186
187
		if ( ! empty( $terms ) ) {
188
189
			$events = array();
190
191 View Code Duplication
			foreach ( $terms as $term ) {
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...
192
193
				// Double check our set user meta value
194
				if ( '__test__' != get_term_meta( $term->term_id, 'dummypress_test_data', true ) && '__test__' != get_term_meta( $term->term_id, 'evans_test_content', true ) ) {
195
					continue;
196
				}
197
198
				$events[] = array(
199
					'action'	=> 'deleted',
200
					'oid'		=> $term->term_id,
201
					'type'		=> $slug,
202
					'link'		=> ''
203
				);
204
205
				// Delete our term
206
				wp_delete_term( $term->term_id, $slug );
207
208
			}
209
210
			$taxonomy = get_taxonomy( $slug );
211
212
			$events[] = array(
213
				'action'	=> 'general',
214
				'message'	=> __( 'Deleted', 'dummybot' ) . ' ' . $taxonomy->labels->name
215
			);
216
217
			return $events;
218
219
		}
220
221
	}
222
223
}
224