WalkscoreAPI   A
last analyzed

Complexity

Total Complexity 25

Size/Duplication

Total Lines 129
Duplicated Lines 11.63 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 15
loc 129
rs 10
c 0
b 0
f 0
wmc 25
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 9 9 2
A get_walkscore() 3 7 4
B get_transit_score() 3 7 5
A get_transit_stop_search() 0 2 1
A get_transit_network_search() 0 2 1
A get_transit_stop_details() 0 2 1
A get_transit_route_details() 0 2 1
A get_transit_supported_cities() 0 2 1
C response_code_msg() 0 29 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 24 and the first side effect is on line 18.

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
 * WP Walkscore API
4
 *
5
 * @package WP-Walkscore-API
6
 */
7
/*
8
* Plugin Name: WP WalkScore API
9
* Plugin URI: https://github.com/wp-api-libraries/wp-walkscore-api
10
* Description: Perform API requests to Walkscore in WordPress.
11
* Author: WP API Libraries
12
* Version: 1.0.0
13
* Author URI: https://wp-api-libraries.com
14
* GitHub Plugin URI: https://github.com/wp-api-libraries/wp-walkscore-api
15
* GitHub Branch: master
16
*/
17
/* Exit if accessed directly. */
18
if ( ! defined( 'ABSPATH' ) ) { exit; }
19
/* Check if class exists. */
20
if ( ! class_exists( 'WalkscoreAPI' ) ) {
21
	/**
22
	 * Walkscore API Class.
23
	 */
24
	class WalkscoreAPI {
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...
25
		/**
26
		 * API Key.
27
		 *
28
		 * @var string
29
		 */
30
		static private $wsapikey;
31
		/**
32
		 * Return format. XML or JSON.
33
		 *
34
		 * @var [string
35
		 */
36
		static private $output;
37
		/**
38
		 * URL to the API.
39
		 *
40
		 * @var string
41
		 */
42
		private $base_uri = 'http://api.walkscore.com';
43
		/**
44
		 * __construct function.
45
		 *
46
		 * @access public
47
		 * @param mixed $wsapikey API Key.
48
		 * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
49
		 */
50
		public function __construct( $wsapikey, $output = 'json' ) {
51
			static::$wsapikey = $wsapikey;
0 ignored issues
show
Bug introduced by
Since $wsapikey is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $wsapikey to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
52
			static::$output = $output;
0 ignored issues
show
Bug introduced by
Since $output is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $output to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
53
		}
54
		/**
55
		 * Fetch the request from the API.
56
		 *
57
		 * @access private
58
		 * @param mixed $request Request URL.
59
		 * @return $body Body.
0 ignored issues
show
Documentation introduced by
The doc-type $body could not be parsed: Unknown type name "$body" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
60
		 */
61 View Code Duplication
		private function fetch( $request ) {
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...
62
			$response = wp_remote_get( $request );
63
			$code = wp_remote_retrieve_response_code( $response );
64
			if ( 200 !== $code ) {
65
				return new WP_Error( 'response-error', sprintf( __( 'Server response code: %d', 'text-domain' ), $code ) );
66
			}
67
			$body = wp_remote_retrieve_body( $response );
68
			return json_decode( $body );
69
		}
70
		/**
71
		 * Get Walkscore (https://www.walkscore.com/professional/api.php)
72
		 *
73
		 * @access public
74
		 * @param mixed $wsapikey Your Walk Score API Key. Contact us to get one. Required.
0 ignored issues
show
Bug introduced by
There is no parameter named $wsapikey. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
		 * @param mixed $latitude The latitude of the requested location. Required.
76
		 * @param mixed $longitude The longitude of the requested location. Required.
77
		 * @param mixed $address The URL encoded address. Required.
78
		 * @param mixed $format Return results in XML or JSON (defaults to XML).
0 ignored issues
show
Bug introduced by
There is no parameter named $format. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
79
		 * @return void
80
		 */
81
		function get_walkscore( $latitude, $longitude, $address ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
82 View Code Duplication
			if ( empty( $latitude ) || empty( $longitude ) || empty( $address ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
				return new WP_Error( 'required-fields', __( 'Required fields are empty.', 'text-domain' ) );
84
			}
85
			$request = $this->base_uri . '/score?format=' . static::$output . '&wsapikey=' . static::$wsapikey . '&lat=' . $latitude . '&lon=' . $longitude . '&address=' . $address;
0 ignored issues
show
Bug introduced by
Since $output is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $output to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug introduced by
Since $wsapikey is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $wsapikey to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
86
			return $this->fetch( $request );
87
		}
88
		/**
89
		 * get_transit_score function.
90
		 *
91
		 * @access public
92
		 * @param mixed $latitude
93
		 * @param mixed $longitude
94
		 * @param mixed $city
95
		 * @param mixed $state
96
		 * @param mixed $country
97
		 * @param mixed $research
98
		 * @return void
99
		 */
100
		function get_transit_score( $latitude, $longitude, $city, $state, $country, $research ) {
0 ignored issues
show
Unused Code introduced by
The parameter $country is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $research is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101 View Code Duplication
			if ( empty( $latitude ) || empty( $longitude ) || empty( $city ) || empty( $state ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
102
				return new WP_Error( 'required-fields', __( 'Required fields are empty.', 'text-domain' ) );
103
			}
104
			$request = $this->base_uri . '/transit/score/?wsapikey=' . static::$wsapikey . '&lat=' . $latitude . '&lon=' . $longitude . '&city=' . $city . '&state=' . $state . '&format=' . $output;
0 ignored issues
show
Bug introduced by
Since $wsapikey is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $wsapikey to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
Bug introduced by
The variable $output does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
105
			return $this->fetch( $request );
106
		}
107
		function get_transit_stop_search() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
108
		}
109
		function get_transit_network_search() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
110
		}
111
		function get_transit_stop_details() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
112
		}
113
		function get_transit_route_details() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
114
		}
115
		function get_transit_supported_cities() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
		}
117
		/**
118
		 * Response code message
119
		 *
120
		 * @param  [String] $code : Response code to get message from.
0 ignored issues
show
Documentation introduced by
The doc-type [String] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
121
		 * @return [String]       : Message corresponding to response code sent in.
0 ignored issues
show
Documentation introduced by
The doc-type [String] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
122
		 */
123
		public function response_code_msg( $code = '' ) {
124
			switch ( $code ) {
125
			case 1:
126
				$msg = __( 'Walk Score successfully returned.', 'text-domain' );
127
				break;
128
			case 2:
129
				$msg = __( 'Score is being calculated and is not currently available.', 'text-domain' );
130
				break;
131
			case 30:
132
				$msg = __( 'Invalid latitude/longitude.', 'text-domain' );
133
				break;
134
			case 31:
135
				$msg = __( 'Walk Score API internal error.', 'text-domain' );
136
				break;
137
			case 40:
138
				$msg = __( 'Your WSAPIKEY is invalid.', 'text-domain' );
139
				break;
140
			case 41:
141
				$msg = __( 'Your daily API quota has been exceeded.', 'text-domain' );
142
				break;
143
			case 42:
144
				$msg = __( 'Your IP address has been blocked.', 'text-domain' );
145
				break;
146
			default:
147
				$msg = __( 'Sorry, response code is unknown.', 'text-domain' );
148
				break;
149
			}
150
			return $msg;
151
		}
152
	}
153
}
154