Failed Conditions
Push — master ( 1d9c07...86291e )
by Atanas
02:02
created

DebugDataProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 18
dl 0
loc 61
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A route() 0 22 4
A toScalar() 0 8 3
A __construct() 0 2 1
1
<?php
2
3
namespace WPEmerge\Exceptions\Whoops;
4
5
use Pimple\Container;
6
7
/**
8
 * Provide debug data for usage with \Whoops\Handler\PrettyPageHandler.
9
 *
10
 * @codeCoverageIgnore
11
 */
12
class DebugDataProvider {
13
	/**
14
	 * Container.
15
	 *
16
	 * @var Container
17
	 */
18
	protected $container = null;
19
20
	/**
21
	 * Constructor.
22
	 *
23
	 * @param Container $container
24
	 */
25
	public function __construct( $container ) {
26
		$this->container = $container;
27
	}
28
29
	/**
30
	 * Convert a value to a scalar representation.
31
	 *
32
	 * @param  mixed $value
33
	 * @return mixed
34
	 */
35
	public function toScalar( $value ) {
36
		$type = gettype( $value );
37
38
		if ( ! is_scalar( $value ) ) {
39
			$value = '(' . $type . ')' . ( $type === 'object' ? ' ' . get_class( $value ) : '' );
40
		}
41
42
		return $value;
43
	}
44
45
	/**
46
	 * Return pritable data about the current route.
47
	 *
48
	 * @param \Whoops\Exception\Inspector $inspector
49
	 * @return array<string, mixed>
50
	 */
51
	public function route( $inspector ) {
52
		/** @var \WPEmerge\Routing\RouteInterface|null $route */
53
		$route = $this->container[ WPEMERGE_ROUTING_ROUTER_KEY ]->getCurrentRoute();
54
55
		if ( ! $route ) {
56
			return [];
57
		}
58
59
		$attributes = [];
60
61
		foreach ( $route->getAttributes() as $attribute => $value ) {
62
			// Only convert the first level of an array to scalar for simplicity.
63
			if ( is_array( $value ) ) {
64
				$value = '[' . implode( ', ', array_map( [$this, 'toScalar'], $value ) ) . ']';
65
			} else {
66
				$value = $this->toScalar( $value );
67
			}
68
69
			$attributes[ $attribute ] = $value;
70
		}
71
72
		return $attributes;
73
	}
74
}
75