WP2D_Helpers::add_debugging()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
cc 5
nc 4
nop 1
1
<?php
2
/**
3
 * Plugin Helpers.
4
 *
5
 * @package WP_To_Diaspora\Helpers
6
 * @since   1.3.0
7
 */
8
9
// Exit if accessed directly.
10
defined( 'ABSPATH' ) || exit;
11
12
/**
13
 * Various helper methods.
14
 */
15
class WP2D_Helpers {
16
17
	/**
18
	 * Debug text that get's accumulated before output.
19
	 *
20
	 * @var string
21
	 */
22
	private static $debugging = '';
23
24
	/**
25
	 * Add a line to the debug output. Include the stack trace to see where it's coming from.
26
	 *
27
	 * @param string $text Text to add.
28
	 *
29
	 * @return bool
30
	 */
31
	public static function add_debugging( $text ) {
32
		// Make sure we're in debug mode.
33
		if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
34
			$d = '';
35
			foreach ( debug_backtrace() as $dbt ) { // phpcs:ignore
36
				extract( $dbt ); // phpcs:ignore
37
				// Only trace back as far as the plugin goes.
38
				if ( strstr( $file, plugin_dir_path( __DIR__ ) ) ) {
39
					$d = sprintf( "%s%s%s [%s:%s]\n", $class, $type, $function, basename( $file ), $line ) . $d;
40
				}
41
			}
42
43
			self::$debugging .= sprintf( "%s\n%s\n", gmdate( 'Y.m.d H:i:s' ), $d . $text );
44
45
			return true;
46
		}
47
48
		return false;
49
	}
50
51
	/**
52
	 * Return the debug output.
53
	 *
54
	 * @return string The debug output.
55
	 */
56
	public static function get_debugging() {
57
		if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
58
			return self::$debugging;
59
		}
60
61
		return false;
62
	}
63
64
	/**
65
	 * Convert a string with comma seperated values to an array.
66
	 *
67
	 * @todo Make $input by value.
68
	 *
69
	 * @param array|string $input The string to be converted.
70
	 *
71
	 * @return array The converted array.
72
	 */
73 View Code Duplication
	public static function str_to_arr( &$input ) {
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...
74
		if ( ! is_array( $input ) ) {
75
			// Explode string > Trim each entry > Remove blanks > Re-index array.
76
			$input = array_values( array_filter( array_map( 'trim', explode( ',', $input ) ) ) );
77
		} else {
78
			// If we're already an array, make sure we return it clean.
79
			self::arr_to_str( $input );
80
			self::str_to_arr( $input );
81
		}
82
83
		return $input;
84
	}
85
86
	/**
87
	 * Convert an array to a string with comma seperated values.
88
	 *
89
	 * @todo Make $input by value.
90
	 *
91
	 * @param array|string $input The array to be converted.
92
	 *
93
	 * @return string The converted string.
94
	 */
95 View Code Duplication
	public static function arr_to_str( &$input ) {
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...
96
		if ( is_array( $input ) ) {
97
			// Trim each entry > Remove blanks > Implode them together.
98
			$input = implode( ',', array_filter( array_map( 'trim', $input ) ) );
99
		} else {
100
			// If we're already a string, make sure we return it clean.
101
			self::str_to_arr( $input );
102
			self::arr_to_str( $input );
103
		}
104
105
		return $input;
106
	}
107
108
	/**
109
	 * Encrypt the passed string with the passed key.
110
	 *
111
	 * @param string $input String to be encrypted.
112
	 * @param string $key   The key used for the encryption.
113
	 *
114
	 * @return string The encrypted string.
115
	 */
116 View Code Duplication
	public static function encrypt( $input, $key = WP2D_ENC_KEY ) {
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...
117
		if ( null === $input || '' === $input ) {
118
			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 WP2D_Helpers::encrypt of type string.

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...
119
		}
120
		global $wpdb;
121
122
		return $wpdb->get_var( $wpdb->prepare( 'SELECT HEX(AES_ENCRYPT(%s,%s))', $input, $key ) );
123
	}
124
125
	/**
126
	 * Decrypt the passed string with the passed key.
127
	 *
128
	 * @param string $input String to be decrypted.
129
	 * @param string $key   The key used for the decryption.
130
	 *
131
	 * @return string The decrypted string.
132
	 */
133 View Code Duplication
	public static function decrypt( $input, $key = WP2D_ENC_KEY ) {
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...
134
		if ( null === $input || '' === $input ) {
135
			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 WP2D_Helpers::decrypt of type string.

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...
136
		}
137
		global $wpdb;
138
139
		return $wpdb->get_var( $wpdb->prepare( 'SELECT AES_DECRYPT(UNHEX(%s),%s)', $input, $key ) );
140
	}
141
142
	/**
143
	 * Set up and return an API connection using the currently saved options..
144
	 *
145
	 * @return WP2D_API The API object.
146
	 */
147
	public static function api_quick_connect() {
148
		$options   = WP2D_Options::instance();
149
		$pod       = (string) $options->get_option( 'pod' );
150
		$is_secure = true;
151
		$username  = (string) $options->get_option( 'username' );
152
		$password  = self::decrypt( (string) $options->get_option( 'password' ) );
153
154
		$api = new WP2D_API( $pod, $is_secure );
155
156
		// This is necessary for correct error handling!
157
		if ( $api->init() ) {
158
			$api->login( $username, $password );
159
		}
160
161
		if ( $api->has_last_error() ) {
162
			self::add_debugging( $api->get_last_error() );
163
		}
164
165
		return $api;
166
	}
167
}
168