Kint_Parser_TraceTest::testIsTrace()   A
last analyzed

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 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Kint_Parser_TraceTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style introduced by
Kint_Parser_TraceTest does not seem to conform to the naming convention (^[A-Z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
4
{
5
    public function testParse()
6
    {
7
        $p = new Kint_Parser();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
8
        $p->addPlugin(new Kint_Parser_Trace());
9
10
        $bt = defined('DEBUG_BACKTRACE_IGNORE_ARGS')
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $bt. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
11
            ? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
12
            : debug_backtrace();
13
14
        $o = Kint_Object::blank();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
15
16
        $o = $p->parse($bt, $o);
17
18
        $this->assertContains('trace', $o->hints);
19
        $this->assertInstanceOf('Kint_Object_Trace', $o);
20
        $this->assertInstanceOf('Kint_Object_TraceFrame', $o->value->contents[0]);
21
    }
22
23
    public function provideTraces()
24
    {
25
        $bt = debug_backtrace(true);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $bt. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
26
        $bad_bt_1 = $bt;
0 ignored issues
show
Coding Style introduced by
$bad_bt_1 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
        $bad_bt_1[0]['test'] = 'woot';
0 ignored issues
show
Coding Style introduced by
$bad_bt_1 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
28
        $bad_bt_2 = $bt;
0 ignored issues
show
Coding Style introduced by
$bad_bt_2 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Unused Code introduced by
$bad_bt_2 is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29
        $bat_bt_2[0]['function'] = 1234;
0 ignored issues
show
Coding Style introduced by
$bat_bt_2 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style Comprehensibility introduced by
$bat_bt_2 was never initialized. Although not strictly required by PHP, it is generally a good practice to add $bat_bt_2 = 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...
30
31
        return array(
32
            'empty' => array(
33
                'trace' => array(),
34
                'expect' => false,
35
            ),
36
            'backtrace' => array(
37
                'trace' => $bt,
38
                'expect' => true,
39
            ),
40
            'bad backtrace, extra key' => array(
41
                'trace' => $bad_bt_1,
0 ignored issues
show
Coding Style introduced by
$bad_bt_1 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
42
                'expect' => false,
43
            ),
44
            'bad backtrace, wrong type' => array(
45
                'trace' => $bad_bt_1,
0 ignored issues
show
Coding Style introduced by
$bad_bt_1 does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
46
                'expect' => false,
47
            ),
48
            'mythical' => array(
49
                'trace' => array(
50
                    array(
51
                        'function' => 'mythical_internal_function_with_no_args_that_results_in_a_backtrace',
52
                        'file' => __FILE__,
53
                        'line' => 1,
54
                    ),
55
                ),
56
                'expect' => true,
57
            ),
58
        );
59
    }
60
61
    /**
62
     * @dataProvider provideTraces
63
     */
64
    public function testIsTrace(array $trace, $expected)
65
    {
66
        $this->assertEquals($expected, Kint_Parser_Trace::isTrace($trace));
67
    }
68
69
    public function provideFrames()
70
    {
71
        return array(
72
            'function match' => array(
73
                'frame' => array(
74
                    'function' => 'testAWeirdFunctionName',
75
                ),
76
                'matches' => array('testaweirdfunctionname'),
77
                'expected' => true,
78
            ),
79
            'function no match denormalized' => array(
80
                'frame' => array(
81
                    'function' => 'testAWeirdFunctionName',
82
                ),
83
                'matches' => array('testAWeirdFunctionName'),
84
                'expected' => false,
85
            ),
86
            'function no match method' => array(
87
                'frame' => array(
88
                    'function' => 'testAWeirdFunctionName',
89
                ),
90
                'matches' => array(array('test', 'testaweirdfunctionname')),
91
                'expected' => false,
92
            ),
93
            'method no match function' => array(
94
                'frame' => array(
95
                    'function' => 'testAWeirdFunctionName',
96
                    'class' => 'test',
97
                ),
98
                'matches' => array('testAWeirdFunctionName'),
99
                'expected' => false,
100
            ),
101
            'method match' => array(
102
                'frame' => array(
103
                    'function' => 'testAWeirdFunctionName',
104
                    'class' => 'test',
105
                ),
106
                'matches' => array(array('test', 'testaweirdfunctionname')),
107
                'expected' => true,
108
            ),
109
        );
110
    }
111
112
    /**
113
     * @dataProvider provideFrames
114
     */
115
    public function testFrameIsListed(array $frame, array $matches, $expected)
116
    {
117
        $this->assertEquals($expected, Kint_Parser_Trace::frameIsListed($frame, $matches));
118
    }
119
120
    public function testNormalizeAliases()
121
    {
122
        $input = array(
123
            'CamelCaseFunction',
124
            'snake_case_function',
125
            'One_of_the_FunctionsMyColleaguesMADE__',
126
            'stringThatCan\'tBeAfunction',
127
            'another string that can not be a function',
128
            array('clASs', 'meThod'),
129
            array($this, 'meThod'),
130
            array('a', 'b', 'c'),
131
        );
132
133
        $expected = array(
134
            'camelcasefunction',
135
            'snake_case_function',
136
            'one_of_the_functionsmycolleaguesmade__',
137
            array('class', 'method'),
138
        );
139
140
        Kint_Parser_Trace::normalizeAliases($input);
141
142
        $this->assertEquals($expected, $input);
143
    }
144
}
145