Completed
Push — develop ( 9892da...c6a3a9 )
by
unknown
28s queued 10s
created

Navigator_Data   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 131
Duplicated Lines 86.26 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 113
loc 131
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_post_types_as_string() 0 10 2
B post_navigator_get_results() 59 59 1
A entity_navigator_get_results() 54 54 1

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
namespace Wordlift\Widgets\Navigator;
4
/**
5
 * @since 3.27.8
6
 * @author Naveen Muthusamy <[email protected]>
7
 */
8
class Navigator_Data {
9
10
	public static function get_post_types_as_string( $post_types ) {
11
		if ( $post_types === array() ) {
12
			$post_types = get_post_types();
13
		}
14
		$post_types = array_map( function ( $post_type ) {
15
			return "'" . esc_sql( $post_type ) . "'";
16
		}, $post_types );
17
18
		return implode( ',', $post_types );
19
	}
20
21 View Code Duplication
	public static function post_navigator_get_results(
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...
22
		$post_id, $fields = array(
23
		'ID',
24
		'post_title',
25
	), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array()
26
	) {
27
28
		$post_types = self::get_post_types_as_string( $post_types );
29
		global $wpdb;
30
31
		$select = implode( ', ', array_map( function ( $item ) {
32
			return "p.$item AS $item";
33
		}, (array) $fields ) );
34
35
		$order_by = implode( ', ', array_map( function ( $item ) {
36
			return "p.$item";
37
		}, (array) $order_by ) );
38
39
40
		/** @noinspection SqlNoDataSourceInspection */
41
		return $wpdb->get_results(
42
			$wpdb->prepare( <<<EOF
43
SELECT %4\$s, p2.ID as entity_id
44
 FROM {$wpdb->prefix}wl_relation_instances r1
45
    INNER JOIN {$wpdb->prefix}wl_relation_instances r2
46
        ON r2.object_id = r1.object_id
47
            AND r2.subject_id != %1\$d
48
	-- get the ID of the post entity in common between the object and the subject 2. 
49
    INNER JOIN {$wpdb->posts} p2
50
        ON p2.ID = r2.object_id
51
            AND p2.post_status = 'publish'
52
            AND p2.post_type IN ($post_types)
53
    INNER JOIN {$wpdb->posts} p
54
        ON p.ID = r2.subject_id
55
            AND p.post_status = 'publish'
56
            AND p.post_type IN ($post_types)
57
    INNER JOIN {$wpdb->term_relationships} tr
58
     	ON tr.object_id = p.ID
59
    INNER JOIN {$wpdb->term_taxonomy} tt
60
     	ON tt.term_taxonomy_id = tr.term_taxonomy_id
61
      	    AND tt.taxonomy = 'wl_entity_type'
62
    INNER JOIN {$wpdb->terms} t
63
        ON t.term_id = tt.term_id
64
            AND t.slug = 'article'
65
    -- select only posts with featured images.
66
    INNER JOIN {$wpdb->postmeta} m
67
        ON m.post_id = p.ID
68
            AND m.meta_key = '_thumbnail_id'
69
 WHERE r1.subject_id = %1\$d
70
 -- avoid duplicates.
71
 GROUP BY p.ID
72
 ORDER BY %5\$s
73
 LIMIT %2\$d
74
 OFFSET %3\$d
75
EOF
76
				, $post_id, $limit, $offset, $select, $order_by )
77
		);
78
79
	}
80
81
82 View Code Duplication
	public static function entity_navigator_get_results(
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...
83
		$post_id, $fields = array(
84
		'ID',
85
		'post_title',
86
	), $order_by = 'ID DESC', $limit = 10, $offset = 0, $post_types = array()
87
	) {
88
		global $wpdb;
89
90
		$select = implode( ', ', array_map( function ( $item ) {
91
			return "p.$item AS $item";
92
		}, (array) $fields ) );
93
94
		$order_by = implode( ', ', array_map( function ( $item ) {
95
			return "p.$item";
96
		}, (array) $order_by ) );
97
98
		$post_types = self::get_post_types_as_string( $post_types );
99
100
		/** @noinspection SqlNoDataSourceInspection */
101
		return $wpdb->get_results(
102
			$wpdb->prepare( <<<EOF
103
SELECT %4\$s, p2.ID as entity_id
104
 FROM {$wpdb->prefix}wl_relation_instances r1
105
	-- get the ID of the post entity in common between the object and the subject 2. 
106
    INNER JOIN {$wpdb->posts} p2
107
        ON p2.ID = r1.object_id
108
            AND p2.post_status = 'publish'
109
             AND p2.post_type IN ($post_types)
110
    INNER JOIN {$wpdb->posts} p
111
        ON p.ID = r1.subject_id
112
            AND p.post_status = 'publish'
113
             AND p.post_type IN ($post_types)
114
    INNER JOIN {$wpdb->term_relationships} tr
115
     	ON tr.object_id = p.ID
116
    INNER JOIN {$wpdb->term_taxonomy} tt
117
     	ON tt.term_taxonomy_id = tr.term_taxonomy_id
118
      	    AND tt.taxonomy = 'wl_entity_type'
119
    INNER JOIN {$wpdb->terms} t
120
        ON t.term_id = tt.term_id
121
            AND t.slug = 'article'
122
    -- select only posts with featured images.
123
    INNER JOIN {$wpdb->postmeta} m
124
        ON m.post_id = p.ID
125
            AND m.meta_key = '_thumbnail_id'
126
 WHERE r1.object_id = %1\$d
127
 -- avoid duplicates.
128
 GROUP BY p.ID
129
 ORDER BY %5\$s
130
 LIMIT %2\$d
131
 OFFSET %3\$d
132
EOF
133
				, $post_id, $limit, $offset, $select, $order_by )
134
		);
135
	}
136
137
138
}
139
140