Passed
Push — master ( 1931dd...40edde )
by Nirjhar
02:32
created

PLUGIN_CUSTOM_ROUTE   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 67
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register_routes() 0 12 1
A callback() 0 10 2
A permission() 0 3 1
A prepare_item_for_response() 0 5 1
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit;
3
4
/**
5
 * Extending REST API framework of WordPress
6
 *
7
 * @author     Nirjhar Lo
8
 * @package    wp-plugin-framework
9
 */
10
if ( ! class_exists( 'PLUGIN_CUSTOM_ROUTE' ) ) {
11
12
	class PLUGIN_CUSTOM_ROUTE extends WP_REST_Controller {
0 ignored issues
show
Bug introduced by
The type WP_REST_Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
15
		/**
16
		 * REST API routes
17
		 *
18
		 * @return Object
19
		 */
20
		public function register_routes() {
21
22
			$version = '1';
23
    		$namespace = 'vendor/v' . $version;
24
    		$base = 'route';
25
26
			//Available options for methods are CREATABLE, READABLE, EDITABLE, DELETABLE
27
			register_rest_route( $namespace, '/' . $base, array(
0 ignored issues
show
Bug introduced by
The function register_rest_route was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
			/** @scrutinizer ignore-call */ 
28
   register_rest_route( $namespace, '/' . $base, array(
Loading history...
28
        		'methods'             => WP_REST_Server::READABLE,
0 ignored issues
show
Bug introduced by
The type WP_REST_Server was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
        		'callback'            => array( $this, 'callback' ),
30
        		'permission_callback' => array( $this, 'permission' ),
31
        		'args'                => array('sample', 'list', 'of', 'args')
32
      		));
33
		}
34
35
36
		/**
37
		 * The request handler
38
		 *
39
		 * @return Object
40
		 */
41
		public function callback() {
42
43
    		$params = $request->get_params();
0 ignored issues
show
Unused Code introduced by
The assignment to $params is dead and can be removed.
Loading history...
Comprehensibility Best Practice introduced by
The variable $request seems to be never defined.
Loading history...
44
    		$items = array();
45
    		$data = $this->prepare_item_for_response( $items, $request );
46
47
    		if ( $data ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
48
      			return new WP_REST_Response( $data, 200 );
0 ignored issues
show
Bug introduced by
The type WP_REST_Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
    		} else {
50
				return new WP_Error( 'status_code', __( 'message', 'text-domain' ) );
0 ignored issues
show
Bug introduced by
The type WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
				return new WP_Error( 'status_code', /** @scrutinizer ignore-call */ __( 'message', 'text-domain' ) );
Loading history...
51
			}
52
		}
53
54
55
		/**
56
		 * Prevent unauthorized access here
57
		 *
58
		 * @return Bool
59
		 */
60
		public function permission() {
61
62
			return current_user_can( 'manage_options' );
0 ignored issues
show
Bug introduced by
The function current_user_can was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
			return /** @scrutinizer ignore-call */ current_user_can( 'manage_options' );
Loading history...
63
		}
64
65
66
		/**
67
		 * Processing of request takes place here
68
		 *
69
		 * @param Array
70
		 * @param Object
71
		 *
72
		 * @return Array
73
		 */
74
		public function prepare_item_for_response($items, $request) {
75
76
			//Process the data in any way you like
77
			$data = compact($items, $request);
78
			return $data;
79
		}
80
	}
81
}
82