Completed
Push — master ( 42ba23...5fbbab )
by Julien
03:55
created

functions-dummy.php ➔ wpbo_input_dummy()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 53
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 25
c 1
b 0
f 0
nc 14
nop 1
dl 0
loc 53
rs 7.1199

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
 * Generate random date.
4
 *
5
 * @see  http://stackoverflow.com/a/14186825
6
 * @param  [type] $min_date [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(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...
7
 * @param  [type] $max_date [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(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...
8
 * @return [type]           [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(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...
9
 */
10
function wpbo_rand_date( $min_date, $max_date ) {
11
    /* Gets 2 dates as string, earlier and later date.
12
       Returns date in between them.
13
    */
14
15
    $rand_epoch = rand( $min_date, $max_date );
16
17
    return date( 'Y-m-d H:i:s', $rand_epoch );
18
}
19
20
function wpbo_input_dummy( $lines = 5 ) {
21
22
	if( isset( $_GET['wpbo_dummy'] ) && is_numeric( $_GET['wpbo_dummy'] ) )
23
		$lines = $_GET['wpbo_dummy'];
24
25
	/* Max conversion rate */
26
	$ratio = 10;
0 ignored issues
show
Unused Code introduced by
$ratio 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...
27
28
	/* Max conversion added based on max ratio */
29
	$max = ( 20 * $lines ) / 100;
30
31
	$args = array(
32
		'post_type'      => 'wpbo-popup',
33
		'post_status'    => 'publish',
34
		'posts_per_page' => -1,
35
		
36
	);
37
38
	$popups = new WP_Query( $args );
39
40
	if( count( $popups->posts ) >= 1 ) {
41
42
		foreach( $popups->posts as $popup ) {
43
44
			/* Count ratio */
45
			$limit = 0;
46
47
			$types = array(
48
				'impression',
49
				'conversion'
50
			);
51
			
52
			for( $count = 0; $count <= $lines; $count++ ) {
53
54
				if( $limit >= $max )
55
					$types[1] = 'impression';
56
57
				$dtype = rand( 0, 1 );
58
				$dtype = $types[$dtype];
59
				$time  = wpbo_rand_date( strtotime( 'first day of January last year', time() ), strtotime( 'last day of December this year', time() ) );
60
61
				if( $dtype === 'conversion' )
62
					++$limit;
63
64
				wpbo_db_insert_data( array( 'time' => $time, 'data_type' => $dtype, 'popup_id' => $popup->ID, ), false );
65
66
			}
67
68
		}
69
70
	}
71
72
}
73
74
function wpbo_add_dummy() {
75
	add_action( 'init', 'wpbo_input_dummy' );
76
}
77
78
if ( isset( $_GET['wpbo_dummy'] ) ) {
79
	wpbo_add_dummy();
80
}