Completed
Branch master (bae7d3)
by Dzmitry
05:41 queued 02:34
created

TimeProfilerSubscriber::getProfiledTimestamp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
use ivol\EventDispatcher\AfterExecuteEvent;
3
use ivol\EventDispatcher\BeforeExecuteEvent;
4
use ivol\ExecParams;
5
use ivol\ExecutionWrapper;
6
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
7
8
class ExecutionWrapperIntegrationTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    public function testExecuteSubscriber()
11
    {
12
        $sut = new ExecutionWrapper();
13
        $profiler = new TimeProfilerSubscriber();
14
        $sut->getEventDispatcher()->addSubscriber($profiler);
15
16
        $result = $sut->exec('echo %s', array("'123'"));
0 ignored issues
show
Unused Code introduced by
$result 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...
17
18
        $this->assertTrue($profiler->getProfiledTimestamp() > 0);
19
    }
20
21
}
22
23
class TimeProfilerSubscriber implements EventSubscriberInterface
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
24
{
25
    /** @var float  */
26
    private $startTime = -1;
27
    /** @var float  */
28
    private $profiledTime = -1;
29
    
30
    /**
31
     * Returns an array of event names this subscriber wants to listen to.
32
     *
33
     * The array keys are event names and the value can be:
34
     *
35
     *  * The method name to call (priority defaults to 0)
36
     *  * An array composed of the method name to call and the priority
37
     *  * An array of arrays composed of the method names to call and respective
38
     *    priorities, or 0 if unset
39
     *
40
     * For instance:
41
     *
42
     *  * array('eventName' => 'methodName')
43
     *  * array('eventName' => array('methodName', $priority))
44
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
45
     *
46
     * @return array The event names to listen to
47
     */
48
    public static function getSubscribedEvents()
49
    {
50
        return array(
51
            BeforeExecuteEvent::EVENT_NAME => 'start',
52
            AfterExecuteEvent::EVENT_NAME => 'stop'
53
        );
54
    }
55
56
    public function start(BeforeExecuteEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58
        $this->startTime = microtime(true);
59
    }
60
61
    public function stop(AfterExecuteEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
    {
63
        $this->profiledTime = microtime(true) - $this->startTime;
64
    }
65
66
    public function getProfiledTimestamp()
67
    {
68
        if ($this->profiledTime < 0) {
69
            throw new RuntimeException('Profiling not started yet');
70
        }
71
        return $this->profiledTime;
72
    }
73
}