Test Failed
Pull Request — master (#456)
by Kiran
22:05
created

GeoDir_Privacy_Exporters   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B post_data_exporter() 0 37 4
B get_post_personal_data() 0 28 1
1
<?php
2
/**
3
 * Personal data exporters.
4
 *
5
 * @since 1.2.26
6
 * @package GeoDirectory
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * GeoDir_Privacy_Exporters Class.
13
 */
14
class GeoDir_Privacy_Exporters {
15
	/**
16
	 * Finds and exports data which could be used to identify a person from GeoDirectory data associated with an email address.
17
	 *
18
	 * Posts are exported in blocks of 10 to avoid timeouts.
19
	 *
20
	 * @since 1.2.26
21
	 * @param string $email_address The user email address.
22
	 * @param int    $page  Page.
23
	 * @return array An array of personal data in name value pairs
24
	 */
25
	public static function post_data_exporter( $email_address, $page ) {
26
		$done           = false;
0 ignored issues
show
Unused Code introduced by
$done 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
		$page           = (int) $page;
28
		$user           = get_user_by( 'email', $email_address ); // Check if user has an ID in the DB to load stored personal data.
29
		$data_to_export = array();
30
31
		$post_query    = array(
0 ignored issues
show
Unused Code introduced by
$post_query 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...
32
			'limit'    => 10,
33
			'page'     => $page,
34
			'author'   => array( $email_address ),
35
		);
36
37
		if ( $user instanceof WP_User ) {
0 ignored issues
show
Bug introduced by
The class WP_User does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
38
			$order_query['author'][] = (int) $user->ID;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$order_query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $order_query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
39
		}
40
41
		$posts = array();
42
43
		if ( 0 < count( $posts ) ) {
44
			foreach ( $posts as $post ) {
45
				$data_to_export[] = array(
46
					'group_id'    => 'geodirectory_posts',
47
					'group_label' => __( 'Posts', 'geodirectory' ),
48
					'item_id'     => 'post-' . $post->ID,
49
					'data'        => self::get_post_personal_data( $post ),
50
				);
51
			}
52
			$done = 10 > count( $posts );
53
		} else {
54
			$done = true;
55
		}
56
57
		return array(
58
			'data' => $data_to_export,
59
			'done' => $done,
60
		);
61
	}
62
63
	/**
64
	 * Get personal data (key/value pairs) for an post object.
65
	 *
66
	 * @since 1.6.26
67
	 * @param WP_Post $post Post object.
68
	 * @return array
69
	 */
70
	protected static function get_post_personal_data( $post ) {
71
		$personal_data = array(
72
			array(
73
				'name'  => __( 'Post ID', 'geodirectory' ),
74
				'value' => $post->ID,
75
			),
76
			array(
77
				'name'  => __( 'Post Title', 'geodirectory' ),
78
				'value' => get_the_title( $post->ID ),
79
			),
80
			array(
81
				'name'  => __( 'Post Date', 'geodirectory' ),
82
				'value' => date_i18n( get_option( 'date_format' ) . ', ' . get_option( 'time_format' ), strtotime( $post->post_date ) ),
83
			),
84
			// TODO more fields
85
		);
86
87
		/**
88
		 * Allow extensions to register their own personal data for this post for the export.
89
		 *
90
		 * @since 1.6.26
91
		 * @param array    $personal_data Array of name value pairs to expose in the export.
92
		 * @param WP_Post $post The post object.
93
		 */
94
		$personal_data = apply_filters( 'geodir_privacy_export_post_personal_data', $personal_data, $post );
95
96
		return $personal_data;
97
	}
98
}
99