Completed
Push — master ( eef885...465d0c )
by
unknown
30:08 queued 12:49
created

Common_Util   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 68
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A construct() 0 1 1
C get_last_unique_key() 0 53 11
1
<?php
2
/**
3
 * Gestion des méthodes communes
4
 *
5
 * @package Evarisk\Plugin
6
 */
7
8
namespace eoxia;
9
10
if ( ! defined( 'ABSPATH' ) ) {
11
	exit;
12
}
13
14
if ( ! class_exists( '\eoxia\Common_Util' ) ) {
15
	/**
16
	 * Gestion des méthodes communes
17
	 *
18
	 * @author Jimmy Latour <[email protected]>
19
	 * @version 1.1.0.0
20
	 */
21
	class Common_Util extends \eoxia\Singleton_Util {
22
		/**
23
		 * Le constructeur obligatoirement pour utiliser la classe \eoxia\Singleton_Util
24
		 *
25
		 * @return void nothing
26
		 */
27
		protected function construct() {}
28
29
		/**
30
		 * Renvoie la dernière clé unique selon le type de l'élement
31
		 *
32
		 * @param  string $controller Le controller.
33
		 * @return int             		L'identifiant unique
34
		 */
35
		public static function get_last_unique_key( $controller ) {
36
			$element_type = $controller::g()->get_post_type();
37
			$wp_type = $controller::g()->get_identifier_helper();
38
			if ( empty( $wp_type ) || empty( $element_type ) || ! is_string( $wp_type ) || ! is_string( $element_type ) ) {
39
				return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by eoxia\Common_Util::get_last_unique_key of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
40
			}
41
42
			global $wpdb;
43
			switch ( $wp_type ) {
44
				case 'post':
45
					$query = $wpdb->prepare(
46
						"SELECT max( PM.meta_value + 0 )
47
						FROM {$wpdb->postmeta} AS PM
48
							INNER JOIN {$wpdb->posts} AS P ON ( P.ID = PM.post_id )
49
						WHERE PM.meta_key = %s
50
							AND P.post_type = %s", '_wpdigi_unique_key', $element_type );
51
				break;
52
53
				case 'comment':
54
					$query = $wpdb->prepare(
55
						"SELECT max( CM.meta_value + 0 )
56
						FROM {$wpdb->commentmeta} AS CM
57
							INNER JOIN {$wpdb->comments} AS C ON ( C.comment_ID = CM.comment_id )
58
						WHERE CM.meta_key = %s
59
							AND C.comment_type = %s", '_wpdigi_unique_key', $element_type );
60
				break;
61
62
				case 'user':
63
					$query = $wpdb->prepare(
64
						"SELECT max( UM.meta_value + 0 )
65
						FROM {$wpdb->usermeta} AS UM
66
						WHERE UM.meta_key = %s", '_wpdigi_unique_key' );
67
				break;
68
69
				case 'term':
70
					$query = $wpdb->prepare(
71
						"SELECT max( TM.meta_value + 0 )
72
						FROM {$wpdb->term_taxonomy} AS T
73
							INNER JOIN {$wpdb->termmeta} AS TM ON ( T.term_id = TM.term_id )
74
						WHERE TM.meta_key = %s AND T.taxonomy=%s", '_wpdigi_unique_key', $element_type );
75
				break;
76
			}
77
78
			if ( ! empty( $query ) ) {
79
				$last_unique_key = $wpdb->get_var( $query );
80
			}
81
82
			if ( empty( $last_unique_key ) ) {
83
				return 0;
84
			}
85
86
			return $last_unique_key;
87
		}
88
	}
89
} // End if().
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90