Completed
Pull Request — master (#151)
by
unknown
08:45
created
src/PhpConsole/Connector.php 1 patch
Spacing   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
 	 * @return static
65 65
 	 */
66 66
 	public static function getInstance() {
67
-		if(!self::$instance) {
67
+		if (!self::$instance) {
68 68
 			self::$instance = new static();
69 69
 		}
70 70
 		return self::$instance;
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
 	 * @throws \Exception
78 78
 	 */
79 79
 	public static function setPostponeStorage(Storage $storage) {
80
-		if(self::$instance) {
80
+		if (self::$instance) {
81 81
 			throw new \Exception(__METHOD__ . ' can be called only before ' . __CLASS__ . '::getInstance()');
82 82
 		}
83 83
 		self::$postponeStorage = $storage;
@@ -87,7 +87,7 @@  discard block
 block discarded – undo
87 87
 	 * @return Storage
88 88
 	 */
89 89
 	private function getPostponeStorage() {
90
-		if(!self::$postponeStorage) {
90
+		if (!self::$postponeStorage) {
91 91
 			self::$postponeStorage = new Storage\Session();
92 92
 		}
93 93
 		return self::$postponeStorage;
@@ -95,7 +95,7 @@  discard block
 block discarded – undo
95 95
 
96 96
 	protected function __construct() {
97 97
 		$this->initConnection();
98
-		$this->setServerEncoding(ini_get('mbstring.internal_encoding') ? : self::CLIENT_ENCODING);
98
+		$this->setServerEncoding(ini_get('mbstring.internal_encoding') ?: self::CLIENT_ENCODING);
99 99
 	}
100 100
 
101 101
 	private final function __clone() {
@@ -114,14 +114,14 @@  discard block
 block discarded – undo
114 114
 	 * @throws \Exception
115 115
 	 */
116 116
 	private function initConnection() {
117
-		if($this->isCliMode()) {
117
+		if ($this->isCliMode()) {
118 118
 			return;
119 119
 		}
120 120
 
121 121
 		$this->initServerCookie();
122 122
 		$this->client = $this->initClient();
123 123
 
124
-		if($this->client) {
124
+		if ($this->client) {
125 125
 			ob_start();
126 126
 			$this->isActiveClient = true;
127 127
 			$this->registerFlushOnShutDown();
@@ -140,14 +140,14 @@  discard block
 block discarded – undo
140 140
 	 * @throws \Exception
141 141
 	 */
142 142
 	private function initClient() {
143
-		if(isset($_COOKIE[self::CLIENT_INFO_COOKIE])) {
143
+		if (isset($_COOKIE[self::CLIENT_INFO_COOKIE])) {
144 144
 			$clientData = @json_decode(base64_decode($_COOKIE[self::CLIENT_INFO_COOKIE], true), true);
145
-			if(!$clientData) {
145
+			if (!$clientData) {
146 146
 				throw new \Exception('Wrong format of response cookie data: ' . $_COOKIE[self::CLIENT_INFO_COOKIE]);
147 147
 			}
148 148
 
149 149
 			$client = new Client($clientData);
150
-			if(isset($clientData['auth'])) {
150
+			if (isset($clientData['auth'])) {
151 151
 				$client->auth = new ClientAuth($clientData['auth']);
152 152
 			}
153 153
 			return $client;
@@ -159,9 +159,9 @@  discard block
 block discarded – undo
159 159
 	 * @throws \Exception
160 160
 	 */
161 161
 	private function initServerCookie() {
162
-		if(!isset($_COOKIE[self::SERVER_COOKIE]) || $_COOKIE[self::SERVER_COOKIE] != self::SERVER_PROTOCOL) {
162
+		if (!isset($_COOKIE[self::SERVER_COOKIE]) || $_COOKIE[self::SERVER_COOKIE] != self::SERVER_PROTOCOL) {
163 163
 			$isSuccess = setcookie(self::SERVER_COOKIE, self::SERVER_PROTOCOL, null, '/');
164
-			if(!$isSuccess) {
164
+			if (!$isSuccess) {
165 165
 				throw new \Exception('Unable to set PHP Console server cookie');
166 166
 			}
167 167
 		}
@@ -195,11 +195,11 @@  discard block
 block discarded – undo
195 195
 	 * @param array $ipMasks Use *(star character) for "any numbers" placeholder array('192.168.*.*', '10.2.12*.*', '127.0.0.1', '2001:0:5ef5:79fb:*:*:*:*')
196 196
 	 */
197 197
 	public function setAllowedIpMasks(array $ipMasks) {
198
-		if($this->isActiveClient()) {
199
-			if(isset($_SERVER['REMOTE_ADDR'])) {
198
+		if ($this->isActiveClient()) {
199
+			if (isset($_SERVER['REMOTE_ADDR'])) {
200 200
 				$ip = $_SERVER['REMOTE_ADDR'];
201
-				foreach($ipMasks as $ipMask) {
202
-					if(preg_match('~^' . str_replace(array('.', '*'), array('\.', '\w+'), $ipMask) . '$~i', $ip)) {
201
+				foreach ($ipMasks as $ipMask) {
202
+					if (preg_match('~^' . str_replace(array('.', '*'), array('\.', '\w+'), $ipMask) . '$~i', $ip)) {
203 203
 						return;
204 204
 					}
205 205
 				}
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
 	 * @return Dumper
213 213
 	 */
214 214
 	public function getDumper() {
215
-		if(!$this->dumper) {
215
+		if (!$this->dumper) {
216 216
 			$this->dumper = new Dumper();
217 217
 		}
218 218
 		return $this->dumper;
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
 	 * @return Dispatcher\Errors
232 232
 	 */
233 233
 	public function getErrorsDispatcher() {
234
-		if(!$this->errorsDispatcher) {
234
+		if (!$this->errorsDispatcher) {
235 235
 			$this->errorsDispatcher = new Dispatcher\Errors($this, $this->getDumper());
236 236
 		}
237 237
 		return $this->errorsDispatcher;
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
 	 * @return Dispatcher\Debug
251 251
 	 */
252 252
 	public function getDebugDispatcher() {
253
-		if(!$this->debugDispatcher) {
253
+		if (!$this->debugDispatcher) {
254 254
 			$this->debugDispatcher = new Dispatcher\Debug($this, $this->getDumper());
255 255
 		}
256 256
 		return $this->debugDispatcher;
@@ -269,7 +269,7 @@  discard block
 block discarded – undo
269 269
 	 * @return Dispatcher\Evaluate
270 270
 	 */
271 271
 	public function getEvalDispatcher() {
272
-		if(!$this->evalDispatcher) {
272
+		if (!$this->evalDispatcher) {
273 273
 			$this->evalDispatcher = new Dispatcher\Evaluate($this, new EvalProvider(), $this->getDumper());
274 274
 		}
275 275
 		return $this->evalDispatcher;
@@ -285,32 +285,32 @@  discard block
 block discarded – undo
285 285
 	 * @throws \Exception
286 286
 	 */
287 287
 	public function startEvalRequestsListener($exitOnEval = true, $flushDebugMessages = true) {
288
-		if(!$this->auth) {
288
+		if (!$this->auth) {
289 289
 			throw new \Exception('Eval dispatcher is allowed only in password protected mode. See PhpConsole\Connector::getInstance()->setPassword(...)');
290 290
 		}
291
-		if($this->isEvalListenerStarted) {
291
+		if ($this->isEvalListenerStarted) {
292 292
 			throw new \Exception('Eval requests listener already started');
293 293
 		}
294 294
 		$this->isEvalListenerStarted = true;
295 295
 
296
-		if($this->isActiveClient() && $this->isAuthorized() && isset($_POST[Connector::POST_VAR_NAME]['eval'])) {
296
+		if ($this->isActiveClient() && $this->isAuthorized() && isset($_POST[Connector::POST_VAR_NAME]['eval'])) {
297 297
 			$request = $_POST[Connector::POST_VAR_NAME]['eval'];
298
-			if(!isset($request['data']) || !isset($request['signature'])) {
298
+			if (!isset($request['data']) || !isset($request['signature'])) {
299 299
 				throw new \Exception('Wrong PHP Console eval request');
300 300
 			}
301
-			if($this->auth->getSignature($request['data']) !== $request['signature']) {
301
+			if ($this->auth->getSignature($request['data']) !== $request['signature']) {
302 302
 				throw new \Exception('Wrong PHP Console eval request signature');
303 303
 			}
304
-			if($flushDebugMessages) {
305
-				foreach($this->messages as $i => $message) {
306
-					if($message instanceof DebugMessage) {
304
+			if ($flushDebugMessages) {
305
+				foreach ($this->messages as $i => $message) {
306
+					if ($message instanceof DebugMessage) {
307 307
 						unset($this->messages[$i]);
308 308
 					}
309 309
 				}
310 310
 			}
311 311
 			$this->convertEncoding($request['data'], $this->serverEncoding, self::CLIENT_ENCODING);
312 312
 			$this->getEvalDispatcher()->dispatchCode($request['data']);
313
-			if($exitOnEval) {
313
+			if ($exitOnEval) {
314 314
 				exit;
315 315
 			}
316 316
 		}
@@ -323,7 +323,7 @@  discard block
 block discarded – undo
323 323
 	 */
324 324
 	public function setSourcesBasePath($sourcesBasePath) {
325 325
 		$sourcesBasePath = realpath($sourcesBasePath);
326
-		if(!$sourcesBasePath) {
326
+		if (!$sourcesBasePath) {
327 327
 			throw new \Exception('Path "' . $sourcesBasePath . '" not found');
328 328
 		}
329 329
 		$this->sourcesBasePath = $sourcesBasePath;
@@ -338,12 +338,12 @@  discard block
 block discarded – undo
338 338
 	 * @throws \Exception
339 339
 	 */
340 340
 	public function setPassword($password, $publicKeyByIp = true) {
341
-		if($this->auth) {
341
+		if ($this->auth) {
342 342
 			throw new \Exception('Password already defined');
343 343
 		}
344 344
 		$this->convertEncoding($password, self::CLIENT_ENCODING, $this->serverEncoding);
345 345
 		$this->auth = new Auth($password, $publicKeyByIp);
346
-		if($this->client) {
346
+		if ($this->client) {
347 347
 			$this->isAuthorized = $this->client->auth && $this->auth->isValidAuth($this->client->auth);
348 348
 		}
349 349
 	}
@@ -386,18 +386,18 @@  discard block
 block discarded – undo
386 386
 	 * @throws \Exception
387 387
 	 */
388 388
 	protected function convertEncoding(&$string, $toEncoding, $fromEncoding) {
389
-		if($string && is_string($string) && $toEncoding != $fromEncoding) {
389
+		if ($string && is_string($string) && $toEncoding != $fromEncoding) {
390 390
 			static $isMbString;
391
-			if($isMbString === null) {
391
+			if ($isMbString === null) {
392 392
 				$isMbString = extension_loaded('mbstring');
393 393
 			}
394
-			if($isMbString) {
395
-				$string = @mb_convert_encoding($string, $toEncoding, $fromEncoding) ? : $string;
394
+			if ($isMbString) {
395
+				$string = @mb_convert_encoding($string, $toEncoding, $fromEncoding) ?: $string;
396 396
 			}
397 397
 			else {
398
-				$string = @iconv($fromEncoding, $toEncoding . '//IGNORE', $string) ? : $string;
398
+				$string = @iconv($fromEncoding, $toEncoding . '//IGNORE', $string) ?: $string;
399 399
 			}
400
-			if(!$string && $toEncoding == 'UTF-8') {
400
+			if (!$string && $toEncoding == 'UTF-8') {
401 401
 				$string = utf8_encode($string);
402 402
 			}
403 403
 		}
@@ -409,7 +409,7 @@  discard block
 block discarded – undo
409 409
 	 * @throws \Exception
410 410
 	 */
411 411
 	public function setHeadersLimit($bytes) {
412
-		if($bytes < static::PHP_HEADERS_SIZE) {
412
+		if ($bytes < static::PHP_HEADERS_SIZE) {
413 413
 			throw new \Exception('Headers limit cannot be less then ' . __CLASS__ . '::PHP_HEADERS_SIZE');
414 414
 		}
415 415
 		$bytes -= static::PHP_HEADERS_SIZE;
@@ -421,7 +421,7 @@  discard block
 block discarded – undo
421 421
 	 * @param $encoding
422 422
 	 */
423 423
 	public function setServerEncoding($encoding) {
424
-		if($encoding == 'utf8' || $encoding == 'utf-8') {
424
+		if ($encoding == 'utf8' || $encoding == 'utf-8') {
425 425
 			$encoding = 'UTF-8'; // otherwise mb_convert_encoding() sometime fails with error(thanks to @alexborisov)
426 426
 		}
427 427
 		$this->serverEncoding = $encoding;
@@ -432,7 +432,7 @@  discard block
 block discarded – undo
432 432
 	 * @param Message $message
433 433
 	 */
434 434
 	public function sendMessage(Message $message) {
435
-		if($this->isActiveClient()) {
435
+		if ($this->isActiveClient()) {
436 436
 			$this->messages[] = $message;
437 437
 		}
438 438
 	}
@@ -450,7 +450,7 @@  discard block
 block discarded – undo
450 450
 	 */
451 451
 	public function onShutDown() {
452 452
 		$this->registeredShutDowns--;
453
-		if(!$this->registeredShutDowns) {
453
+		if (!$this->registeredShutDowns) {
454 454
 			$this->proceedResponsePackage();
455 455
 		}
456 456
 	}
@@ -475,19 +475,19 @@  discard block
 block discarded – undo
475 475
 	 * @throws \Exception
476 476
 	 */
477 477
 	private function proceedResponsePackage() {
478
-		if($this->isActiveClient()) {
478
+		if ($this->isActiveClient()) {
479 479
 			$response = new Response();
480 480
 			$response->isSslOnlyMode = $this->isSslOnlyMode;
481 481
 
482
-			if(isset($_POST[self::POST_VAR_NAME]['getBackData'])) {
482
+			if (isset($_POST[self::POST_VAR_NAME]['getBackData'])) {
483 483
 				$response->getBackData = $_POST[self::POST_VAR_NAME]['getBackData'];
484 484
 			}
485 485
 
486
-			if(!$this->isSslOnlyMode || $this->isSsl()) {
487
-				if($this->auth) {
486
+			if (!$this->isSslOnlyMode || $this->isSsl()) {
487
+				if ($this->auth) {
488 488
 					$response->auth = $this->auth->getServerAuthStatus($this->client->auth);
489 489
 				}
490
-				if(!$this->auth || $this->isAuthorized()) {
490
+				if (!$this->auth || $this->isAuthorized()) {
491 491
 					$response->isLocal = isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] == '127.0.0.1';
492 492
 					$response->docRoot = isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : null;
493 493
 					$response->sourcesBasePath = $this->sourcesBasePath;
@@ -498,7 +498,7 @@  discard block
 block discarded – undo
498 498
 
499 499
 			$responseData = $this->serializeResponse($response);
500 500
 
501
-			if(strlen($responseData) > $this->headersLimit || !$this->setHeaderData($responseData, self::HEADER_NAME, false)) {
501
+			if (strlen($responseData) > $this->headersLimit || !$this->setHeaderData($responseData, self::HEADER_NAME, false)) {
502 502
 				$this->getPostponeStorage()->push($this->postponeResponseId, $responseData);
503 503
 			}
504 504
 		}
@@ -515,8 +515,8 @@  discard block
 block discarded – undo
515 515
 	}
516 516
 
517 517
 	private function setHeaderData($responseData, $headerName, $throwException = true) {
518
-		if(headers_sent($file, $line)) {
519
-			if($throwException) {
518
+		if (headers_sent($file, $line)) {
519
+			if ($throwException) {
520 520
 				throw new \Exception('Unable to process response data, headers already sent in ' . $file . ':' . $line . '. Try to use ob_start() and don\'t use flush().');
521 521
 			}
522 522
 			return false;
@@ -526,14 +526,14 @@  discard block
 block discarded – undo
526 526
 	}
527 527
 
528 528
 	protected function objectToArray(&$var) {
529
-		if(is_object($var)) {
529
+		if (is_object($var)) {
530 530
 			$var = get_object_vars($var);
531 531
 			array_walk_recursive($var, array($this, 'objectToArray'));
532 532
 		}
533 533
 	}
534 534
 
535 535
 	protected function serializeResponse(DataObject $response) {
536
-		if($this->serverEncoding != self::CLIENT_ENCODING) {
536
+		if ($this->serverEncoding != self::CLIENT_ENCODING) {
537 537
 			$this->objectToArray($response);
538 538
 			$this->convertArrayEncoding($response, self::CLIENT_ENCODING, $this->serverEncoding);
539 539
 		}
@@ -544,7 +544,7 @@  discard block
 block discarded – undo
544 544
 	 * Check if there is postponed response request and dispatch it
545 545
 	 */
546 546
 	private function listenGetPostponedResponse() {
547
-		if(isset($_POST[self::POST_VAR_NAME]['getPostponedResponse'])) {
547
+		if (isset($_POST[self::POST_VAR_NAME]['getPostponedResponse'])) {
548 548
 			header('Content-Type: application/json; charset=' . self::CLIENT_ENCODING);
549 549
 			echo $this->getPostponeStorage()->pop($_POST[self::POST_VAR_NAME]['getPostponedResponse']);
550 550
 			$this->disable();
@@ -556,7 +556,7 @@  discard block
 block discarded – undo
556 556
 abstract class DataObject {
557 557
 
558 558
 	public function __construct(array $properties = array()) {
559
-		foreach($properties as $property => $value) {
559
+		foreach ($properties as $property => $value) {
560 560
 			$this->$property = $value;
561 561
 		}
562 562
 	}
Please login to merge, or discard this patch.