Debug::enterScope()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 6
cts 7
cp 0.8571
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2.0116
1
<?php
2
3
namespace Zicht\Tool;
4
5
/**
6
 * Debug helper class
7
 */
8
final class Debug
9
{
10
    /**
11
     * @var array
12
     */
13
    public static $scope = array();
14
15
    public static $scopeChange = array();
16
17
    /**
18
     * @return mixed
19
     */
20
    public static function getScope()
21
    {
22
        return self::$scope[count(self::$scope) - 1];
23
    }
24
25
    /**
26
     * Keeps track of scope
27
     *
28
     * @param string $scope
29
     * @return void
30
     */
31 8
    public static function enterScope($scope)
32
    {
33 8
        if (!is_scalar($scope)) {
34
            throw new \InvalidArgumentException("Only scalars allowed as scope identifiers");
35
        }
36 8
        array_push(self::$scope, $scope);
37 8
        list($call) = debug_backtrace(0);
38 8
        array_push(self::$scopeChange, $call);
39 8
    }
40
41
42
    /**
43
     * @param string $scope
44
     * @throws ScopeException
45
     * @return void
46
     */
47 8
    public static function exitScope($scope)
48
    {
49 8
        $current = array_pop(self::$scope);
50 8
        $call = array_pop(self::$scopeChange);
51 8
        if ($scope !== $current) {
52
            throw new \LogicException("The current scope '$current' was not closed properly, while trying to close '$scope', which was opened at {$call['file']}@{$call['line']}");
53
        }
54 8
    }
55
}
56