Completed
Push — master ( 1716ca...a4bcbb )
by David
09:41 queued 07:17
created

Wordlift_Debug_Service::dump_uri()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is included only if WordPress is set in DEBUG mode and provides
4
 * debugging features.
5
 *
6
 * @since 3.7.2
7
 */
8
9
/**
10
 * Define the Wordlift_Debug_Service class.
11
 *
12
 * @since 3.7.2
13
 */
14
class Wordlift_Debug_Service {
15
16
	/**
17
	 * The {@link Wordlift_Entity_Service} instance.
18
	 *
19
	 * @since  3.7.2
20
	 * @access private
21
	 * @var Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
22
	 */
23
	private $entity_service;
24
25
	/**
26
	 * Wordlift_Debug_Service constructor.
27
	 *
28
	 * @since 3.7.2
29
	 *
30
	 * @param Wordlift_Entity_Service $entity_service The {@link Wordlift_Entity_Service} instance.
31
	 */
32
	public function __construct( $entity_service ) {
33
34
		$this->entity_service = $entity_service;
35
36
		add_action( 'wp_ajax_wl_dump_uri', array( $this, 'dump_uri' ) );
37
38
	}
39
40
	public function dump_uri() {
0 ignored issues
show
Coding Style introduced by
dump_uri uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
42
		if ( ! isset( $_GET['id'] ) ) {
43
			wp_send_json_error( 'id not set' );
44
		}
45
46
		$post_id = $_GET['id'];
47
48
		$post = get_post( $post_id );
49
50
		$uri       = $this->entity_service->get_uri( $post_id );
51
		$encoding  = mb_detect_encoding( $uri );
0 ignored issues
show
Unused Code introduced by
$encoding 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...
52
		$build_uri = $this->entity_service->build_uri( $post->post_title, $post->post_type );
53
54
55
		var_dump( $uri );
0 ignored issues
show
Security Debugging Code introduced by
var_dump($uri); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
56
57
		wp_send_json_success( array(
58
			'uri'               => $uri,
59
			'post_title'        => sprintf( '%s (%s)', $post->post_title, mb_detect_encoding( $post->post_title ) ),
60
			'post_title_ascii'  => mb_convert_encoding( $post->post_title, 'ASCII' ),
61
			'build_uri'         => $build_uri,
62
			'build_uri_convert' => mb_convert_encoding( $build_uri, 'ASCII' ),
63
		) );
64
65
	}
66
67
}