Completed
Pull Request — master (#111)
by Luca
02:07
created

ImportCoordinates::validate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 0
dl 0
loc 20
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace lloc\Msls\ContentImport;
4
5
use lloc\Msls\MslsBlogCollection;
6
7
class ImportCoordinates {
8
9
	const IMPORTERS_GLOBAL_KEY = 'msls_importers';
10
11
	/**
12
	 * @var int
13
	 */
14
	public $source_blog_id;
15
	/**
16
	 * @var int
17
	 */
18
	public $source_post_id;
19
	/**
20
	 * @var int
21
	 */
22
	public $dest_blog_id;
23
	/**
24
	 * @var int
25
	 */
26
	public $dest_post_id;
27
28
	/**
29
	 * @var \WP_Post
30
	 */
31
	public $source_post;
32
33
	/**
34
	 * @var string
35
	 */
36
	public $source_lang;
37
38
	/**
39
	 * @var string
40
	 */
41
	public $dest_lang;
42
43
	/**
44
	 * @var array An array keeping track of which importer (slug) should be used for
45
	 *            a specific import type, shape [ <import-type> => <slug> ]
46
	 */
47
	public $importers = [];
48
49
	/**
50
	 * Validates the coordinates.
51
	 *
52
	 * @return bool
53
	 */
54
	public function validate() {
55
		if ( ! get_blog_post( $this->source_blog_id, $this->source_post_id ) ) {
56
			return false;
57
		}
58
		if ( ! get_blog_post( $this->dest_blog_id, $this->dest_post_id ) ) {
59
			return false;
60
		}
61
		if ( ! $this->source_post instanceof \WP_Post ) {
0 ignored issues
show
Bug introduced by
The class WP_Post does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
62
			return false;
63
		}
64
65
		if ( $this->source_lang !== MslsBlogCollection::get_blog_language( $this->source_blog_id ) ) {
66
			return false;
67
		}
68
		if ( $this->dest_lang !== MslsBlogCollection::get_blog_language( $this->dest_blog_id ) ) {
69
			return false;
70
		}
71
72
		return true;
73
	}
74
75
	/**
76
	 * Returns the importer (slug) for a specific type of imports.
77
	 *
78
	 * @param string $importer_type
79
	 *
80
	 * @return string|null The import slug if set or `null` if not set.
81
	 */
82
	public function get_importer_for( $importer_type ) {
83
		return isset( $this->importers[ $importer_type ] )
84
			? $this->importers[ $importer_type ]
85
			: null;
86
	}
87
88
	/**
89
	 * Parses the importers from request superglobals.
90
	 */
91
	public function parse_importers_from_request() {
0 ignored issues
show
Coding Style introduced by
parse_importers_from_request uses the super-global variable $_REQUEST 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...
Coding Style introduced by
parse_importers_from_request uses the super-global variable $_POST 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...
Coding Style introduced by
parse_importers_from_request 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...
92
		// bitmask
93
		$source = isset( $_REQUEST[ self::IMPORTERS_GLOBAL_KEY ] ) * 100
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
94
		          + isset( $_POST[ self::IMPORTERS_GLOBAL_KEY ] ) * 10
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
95
		          + isset( $_GET[ self::IMPORTERS_GLOBAL_KEY ] );
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
96
97
		switch ( $source ) {
98
			case 100:
99
			case 101;
100
			case 111:
101
			case 110:
102
				$source = $_REQUEST;
0 ignored issues
show
introduced by
Detected access of super global var $_REQUEST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_REQUEST
Loading history...
103
				break;
104
			case 010:
105
			case 011:
106
			case 000:
107
			default:
108
				$source = $_POST;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
109
				break;
110
			case 001:
0 ignored issues
show
Unused Code introduced by
case 1: $source = $_GET; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
111
				$source = $_GET;
0 ignored issues
show
introduced by
Detected access of super global var $_GET, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_GET
Loading history...
112
				break;
113
		}
114
115
		$importers = isset( $source[ self::IMPORTERS_GLOBAL_KEY ] ) && is_array( $source[ self::IMPORTERS_GLOBAL_KEY ] )
116
			? $source[ self::IMPORTERS_GLOBAL_KEY ]
117
			: [];
118
119
		foreach ( $importers as $importer_type => $slug ) {
120
			$this->set_importer_for( $importer_type, $slug );
121
		}
122
	}
123
124
	/**
125
	 * Sets the slug of the importer that should be used for a type of import.
126
	 *
127
	 * @param string $importer_type
128
	 * @param string $slug
129
	 */
130
	public function set_importer_for( $importer_type, $slug ) {
131
		$this->importers[ $importer_type ] = $slug;
132
	}
133
}
134