WP2D_Helpers::api_quick_connect()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 15 and the first side effect is on line 10.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
	public static function add_debugging( $text ) {
30
		// Make sure we're in debug mode.
31
		if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
32
			$d = '';
33
			foreach ( debug_backtrace() as $dbt ) {
34
				extract( $dbt );
35
				// Only trace back as far as the plugin goes.
36
				if ( strstr( $file, plugin_dir_path( dirname( __FILE__ ) ) ) ) {
37
					$d = sprintf( "%s%s%s [%s:%s]\n", $class, $type, $function, basename( $file ), $line ) . $d;
38
				}
39
			}
40
41
			self::$_debugging .= sprintf( "%s\n%s\n", date( 'Y.m.d H:i:s' ), $d . $text );
42
43
			return true;
44
		}
45
		return false;
46
	}
47
48
	/**
49
	 * Return the debug output.
50
	 *
51
	 * @return string The debug output.
52
	 */
53
	public static function get_debugging() {
54
		if ( defined( 'WP2D_DEBUGGING' ) && true === WP2D_DEBUGGING ) {
55
			return self::$_debugging;
56
		}
57
		return false;
58
	}
59
60
	/**
61
	 * Convert a string with comma seperated values to an array.
62
	 *
63
	 * @todo Make $input by value.
64
	 *
65
	 * @param array|string $input The string to be converted.
66
	 * @return array The converted array.
67
	 */
68 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...
69
		if ( ! is_array( $input ) ) {
70
			// Explode string > Trim each entry > Remove blanks > Re-index array.
71
			$input = array_values( array_filter( array_map( 'trim', explode( ',', $input ) ) ) );
72
		} else {
73
			// If we're already an array, make sure we return it clean.
74
			self::arr_to_str( $input );
75
			self::str_to_arr( $input );
76
		}
77
		return $input;
78
	}
79
80
	/**
81
	 * Convert an array to a string with comma seperated values.
82
	 *
83
	 * @todo Make $input by value.
84
	 *
85
	 * @param array|string $input The array to be converted.
86
	 * @return string The converted string.
87
	 */
88 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...
89
		if ( is_array( $input ) ) {
90
			// Trim each entry > Remove blanks > Implode them together.
91
			$input = implode( ',', array_filter( array_map( 'trim', $input ) ) );
92
		} else {
93
			// If we're already a string, make sure we return it clean.
94
			self::str_to_arr( $input );
95
			self::arr_to_str( $input );
96
		}
97
		return $input;
98
	}
99
100
	/**
101
	 * Encrypt the passed string with the passed key.
102
	 *
103
	 * @param string $input String to be encrypted.
104
	 * @param string $key   The key used for the encryption.
105
	 * @return string The encrypted string.
106
	 */
107 View Code Duplication
	public static function encrypt( $input, $key = AUTH_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...
108
		if ( is_null( $input ) || '' === $input ) {
109
			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...
110
		}
111
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
112
		return $wpdb->get_var( $wpdb->prepare( 'SELECT HEX(AES_ENCRYPT(%s,%s))', $input, $key ) );
113
	}
114
115
	/**
116
	 * Decrypt the passed string with the passed key.
117
	 *
118
	 * @param string $input String to be decrypted.
119
	 * @param string $key   The key used for the decryption.
120
	 * @return string The decrypted string.
121
	 */
122 View Code Duplication
	public static function decrypt( $input, $key = AUTH_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...
123
		if ( is_null( $input ) || '' === $input ) {
124
			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...
125
		}
126
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
127
		return $wpdb->get_var( $wpdb->prepare( 'SELECT AES_DECRYPT(UNHEX(%s),%s)', $input, $key ) );
128
	}
129
130
	/**
131
	 * Set up and return an API connection using the currently saved options..
132
	 *
133
	 * @return WP2D_API The API object.
134
	 */
135
	public static function api_quick_connect() {
136
		$options   = WP2D_Options::instance();
137
		$pod       = (string) $options->get_option( 'pod' );
138
		$is_secure = true;
139
		$username  = (string) $options->get_option( 'username' );
140
		$password  = WP2D_Helpers::decrypt( (string) $options->get_option( 'password' ) );
141
142
		$api = new WP2D_API( $pod, $is_secure );
143
144
		// This is necessary for correct error handling!
145
		if ( $api->init() ) {
146
			$api->login( $username, $password );
147
		}
148
149
		if ( $api->has_last_error() ) {
150
			self::add_debugging( $api->get_last_error() );
151
		}
152
153
		return $api;
154
	}
155
}
156