Evaluate   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setEvalProvider() 0 3 1
A getEvalProvider() 0 3 1
A dispatchCode() 0 22 5
1
<?php
2
3
namespace PhpConsole\Dispatcher;
4
use PhpConsole\Connector;
5
use PhpConsole\Dispatcher;
6
use PhpConsole\Dumper;
7
use PhpConsole\EvalProvider;
8
use PhpConsole\EvalResultMessage;
9
10
/**
11
 * Executes client code and sends result data to connector as client expected messages
12
 *
13
 * @package PhpConsole
14
 * @version 3.1
15
 * @link http://consle.com
16
 * @author Sergey Barbushin http://linkedin.com/in/barbushin
17
 * @copyright © Sergey Barbushin, 2011-2013. All rights reserved.
18
 * @license http://www.opensource.org/licenses/BSD-3-Clause "The BSD 3-Clause License"
19
 */
20
class Evaluate extends Dispatcher {
21
22
	/** @var EvalProvider */
23
	protected $evalProvider;
24
25
	/**
26
	 * @param Connector $connector
27
	 * @param EvalProvider $evalProvider
28
	 * @param Dumper $dumper
29
	 */
30 10
	public function __construct(Connector $connector, EvalProvider $evalProvider, Dumper $dumper) {
31 10
		$this->evalProvider = $evalProvider;
32 10
		parent::__construct($connector, $dumper);
33 10
	}
34
35
	/**
36
	 * Override eval provider
37
	 * @param EvalProvider $evalProvider
38
	 */
39 1
	public function setEvalProvider(EvalProvider $evalProvider) {
40 1
		$this->evalProvider = $evalProvider;
41 1
	}
42
43
	/**
44
	 * Get eval provider
45
	 * @return EvalProvider
46
	 */
47 2
	public function getEvalProvider() {
48 2
		return $this->evalProvider;
49
	}
50
51
	/**
52
	 * Execute PHP code and send result message in connector
53
	 * @param $code
54
	 */
55 6
	public function dispatchCode($code) {
56 6
		if($this->isActive()) {
57 5
			$previousLastError = error_get_last();
58 5
			$oldDisplayErrors = ini_set('display_errors', false);
59 5
			$result = $this->evalProvider->evaluate($code);
60 5
			ini_set('display_errors', $oldDisplayErrors);
61
62 5
			$message = new EvalResultMessage();
63 5
			$message->return = $this->dumper->dump($result->return);
64 5
			$message->output = $this->dumper->dump($result->output);
65 5
			$message->time = round($result->time, 6);
66
67 5
			$newLastError = error_get_last();
68 5
			if($newLastError && $newLastError != $previousLastError) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $newLastError 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...
69
				$this->connector->getErrorsDispatcher()->dispatchError($newLastError ['type'], $newLastError ['message'], $newLastError ['file'], $newLastError ['line'], 999);
70
			}
71 5
			if($result->exception) {
72 2
				$this->connector->getErrorsDispatcher()->dispatchException($result->exception);
73
			}
74 5
			$this->sendMessage($message);
75
		}
76 6
	}
77
}
78