GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 5793f9...e93048 )
by Oleg
04:32
created

OptimizationProblem::getOptimizationId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Route4Me;
4
5
use Route4Me\Address;
6
use Route4Me\Common;
7
use Route4Me\RouteParameters;
8
use Route4Me\OptimizationProblemParams;
9
use Route4Me\Route;
10
use Route4Me\Route4Me;
11
use GuzzleHttp\Client;
12
13
class OptimizationProblem extends Common
14
{
15
    static public $apiUrl = '/api.v4/optimization_problem.php';
16
	static public $apiUrl_addr = '/api.v4/address.php';
17
    static public $apiHybridUrl = '/api.v4/hybrid_date_optimization.php';
18
    static public $apiHybridDepotUrl = '/api/change_hybrid_optimization_depot.php';
19
20
    public $optimization_problem_id;
21
    public $user_errors = array();
22
    public $state;
23
    public $parameters;
24
    public $sent_to_background;
25
    public $addresses = array();
26
    public $routes = array();
27
    public $links = array();
28
29
    function __construct()
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...
30
    {
31
        $this->parameters = new RouteParameters;
32
    }
33
34
    public static function fromArray(array $params)
35
    {
36
        $problem = new OptimizationProblem;
37
        $problem->optimization_problem_id = Common::getValue($params, 'optimization_problem_id');
38
        $problem->user_errors = Common::getValue($params, 'user_errors', array());
39
        $problem->state = Common::getValue($params, 'state', array());
40
        $problem->sent_to_background = Common::getValue($params, 'sent_to_background', array());
41
        $problem->links = Common::getValue($params, 'links', array());
42
43
        if (isset($params['parameters'])) {
44
            $problem->parameters = RouteParameters::fromArray($params['parameters']);
45
        }
46
47 View Code Duplication
        if (isset($params['addresses'])) {
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...
48
            $addresses = array();
49
            foreach ($params['addresses'] as $address) {
50
                $addresses[] = Address::fromArray($address);
51
            }
52
            $problem->addresses = $addresses;
53
        }
54
55 View Code Duplication
        if (isset($params['routes'])) {
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...
56
            $routes = array();
57
            foreach ($params['routes'] as $route) {
58
                $routes[] = Route::fromArray($address);
0 ignored issues
show
Bug introduced by
The variable $address seems to be defined by a foreach iteration on line 49. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
59
            }
60
            $problem->routes = $routes;
61
        }
62
63
        return $problem;
64
    }
65
66
    public static function optimize(OptimizationProblemParams $params)
67
    {
68
        $optimize = Route4Me::makeRequst(array(
69
            'url'    => self::$apiUrl,
70
            'method' => 'POST',
71
            'query'  => array(
72
                'directions'             => isset($params->directions) ? $params->directions: null, 
73
                'format'                 => isset($params->format) ? $params->format: null,
74
                'route_path_output'      => isset($params->route_path_output) ? $params->route_path_output: null,
75
                'optimized_callback_url' => isset($params->optimized_callback_url) ? $params->optimized_callback_url: null
76
            ),
77
            'body'   => array(
78
                'addresses'  => $params->getAddressesArray(),
79
                'parameters' => $params->getParametersArray()
80
            )
81
        ));
82
83
        return OptimizationProblem::fromArray($optimize);
84
    }
85
86
    public static function get($params)
87
    {
88
        $optimize = Route4Me::makeRequst(array(
89
            'url'    => self::$apiUrl,
90
            'method' => 'GET',
91
            'query'  => array(
92
                'state' => isset($params['state']) ? $params['state'] : null,
93
                'limit' => isset($params['limit']) ? $params['limit'] : null,
94
                'offset' => isset($params['offset']) ? $params['offset'] : null,
95
                'optimization_problem_id' => isset($params['optimization_problem_id']) 
96
                    ? $params['optimization_problem_id'] : null,
97
                'wait_for_final_state' => isset($params['wait_for_final_state']) 
98
                    ? $params['wait_for_final_state'] : null,
99
            )
100
        ));
101
102
        if (isset($optimize['optimizations'])) {
103
            $problems = array();
104
            foreach($optimize['optimizations'] as $problem) {
105
                $problems[] = OptimizationProblem::fromArray($problem);
106
            }
107
            return $problems;
108
        } else {
109
            return OptimizationProblem::fromArray($optimize);
110
        }
111
    }
112
113
    public static function reoptimize($params)
114
    {
115
        $param = new OptimizationProblemParams;
116
        $param->optimization_problem_id = isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null;
117
        $param->reoptimize = 1;
118
119
        return self::update((array)$param);
120
    }
121
122
    public static function update($params)
123
    {
124
		$optimize = Route4Me::makeRequst(array(
125
            'url'    => self::$apiUrl,
126
            'method' => 'PUT',
127
            'query'  => array(
128
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
129
                'addresses' => isset($params['addresses']) ? $params['addresses'] : null,
130
                'reoptimize' => isset($params['reoptimize']) ? $params['reoptimize'] : null,
131
            )
132
        ));
133
		
134
		return $optimize;
135
    }
136
137
    public function getOptimizationId()
138
    {
139
        return $this->optimization_problem_id;
140
    }
141
142
    public function getRoutes()
143
    {
144
        return $this->routes;
145
    }
146
	
147
	public function getRandomOptimizationId($offset,$limit)
0 ignored issues
show
Unused Code introduced by
The parameter $offset 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 $limit 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...
148
	{
149
		$query['limit'] = isset($params['limit']) ? $params['limit'] : 30;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$query was never initialized. Although not strictly required by PHP, it is generally a good practice to add $query = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Bug introduced by
The variable $params seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
150
        $query['offset'] = isset($params['offset']) ? $params['offset'] : 0;
151
			
152
		$json = Route4Me::makeRequst(array(
153
            'url'    => self::$apiUrl,
154
            'method' => 'GET',
155
            'query'  => $query
156
        ));
157
		
158
		$optimizations = array();
159
            foreach($json as $optimization) {
160
				if (gettype($optimization)!="array") continue;
161
				foreach ($optimization as $otp1) {
162
					$optimizations[] = $otp1;
163
				}
164
            }
165
			
166
			$num=rand(0,sizeof($optimizations)-1);
167
			//echo "num=$num.<br>".sizeof($optimizations)."<br>";
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
168
			$rOptimization=$optimizations[$num];
169
			return $rOptimization;
170
	}
171
	
172
	public function getAddresses($opt_id)
173
	{
174
		if ($opt_id==null) return null;
175
		
176
		$params = array( "optimization_problem_id" => $opt_id );
177
		
178
		$optimization = (array)$this->get($params);
179
		
180
		$addresses = $optimization["addresses"];
181
		
182
		return $addresses;
183
		
184
	}
185
	
186
	public function getRandomAddressFromOptimization($opt_id)
187
	{
188
		$addresses = (array)$this->getAddresses($opt_id);
189
		
190
		if ($addresses == null) {
191
			echo "There are no addresses in this optimization!.. Try again.";
192
			return null;
193
		}
194
		
195
		$num=rand(0,sizeof($addresses)-1);
196
		$rAddress=$addresses[$num];
197
		
198
		return $rAddress;
199
	}
200
	
201 View Code Duplication
	public function removeAddress($params)
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...
202
	{
203
		$response = Route4Me::makeRequst(array(
204
            'url'    => self::$apiUrl_addr,
205
            'method' => 'DELETE',
206
            'query'  => array(
207
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
208
                'route_destination_id' => isset($params['route_destination_id']) ? $params['route_destination_id'] : null,
209
            )
210
        ));
211
		
212
		return $response;
213
	}
214
	
215 View Code Duplication
	public function removeOptimization($params)
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...
216
	{
217
		$response = Route4Me::makeRequst(array(
218
            'url'    => self::$apiUrl,
219
            'method' => 'DELETE',
220
            'query'  => array(
221
                'redirect' => isset($params['redirect']) ? $params['redirect'] : null,
222
            ),
223
            'body'  => array(
224
				'optimization_problem_ids' => isset($params['optimization_problem_ids']) ? $params['optimization_problem_ids'] : null,
225
			)
226
        ));
227
		
228
		return $response;
229
	}
230
    
231 View Code Duplication
    public function getHybridOptimization($params)
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...
232
    {
233
        $optimize = Route4Me::makeRequst(array(
234
            'url'    => self::$apiHybridUrl,
235
            'method' => 'GET',
236
            'query'  => array(
237
                'target_date_string' => isset($params['target_date_string']) ? $params['target_date_string'] : null,
238
                'timezone_offset_minutes' => isset($params['timezone_offset_minutes']) ? $params['timezone_offset_minutes'] : null
239
            )
240
        ));
241
242
        return $optimize;
243
    }
244
    
245 View Code Duplication
    Public function addDepotsToHybrid($params)
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...
246
    {
247
        $depots = Route4Me::makeRequst(array( 
248
            'url'    => self::$apiHybridDepotUrl,
249
            'method' => 'POST',
250
            'query'  => array(
251
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
252
                ),
253
            'body'  => array(
254
                'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null,
255
                'delete_old_depots' => isset($params['delete_old_depots']) ? $params['delete_old_depots'] : null,
256
                'new_depots' => isset($params['new_depots']) ? $params['new_depots'] : null,
257
            )
258
        ));
259
        
260
        return $depots;
261
    }
262
}
263