Completed
Push — master ( 106c7e...3c0ece )
by Dzmitry
06:55
created

TimeProfilerSubscriber   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 5
c 1
b 1
f 0
lcom 1
cbo 0
dl 0
loc 51
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSubscribedEvents() 0 7 1
A start() 0 4 1
A stop() 0 4 1
A getProfiledTimestamp() 0 7 2
1
<?php
2
namespace ivol\tests;
3
4
use ivol\EventDispatcher\AfterExecuteEvent;
5
use ivol\EventDispatcher\BeforeExecuteEvent;
6
use ivol\ExecutionContext;
7
use ivol\ExecutionWrapper;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
class ExecutionWrapperIntegrationTest extends \PHPUnit_Framework_TestCase
11
{
12
    public function testExecuteSubscriber()
13
    {
14
        $sut = new ExecutionWrapper();
15
        $profiler = new TimeProfilerSubscriber();
16
        $sut->getEventDispatcher()->addSubscriber($profiler);
17
18
        $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...
19
20
        $this->assertTrue($profiler->getProfiledTimestamp() > 0);
21
    }
22
23
}
24
25
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...
26
{
27
    /** @var float  */
28
    private $startTime = -1;
29
    /** @var float  */
30
    private $profiledTime = -1;
31
    
32
    /**
33
     * Returns an array of event names this subscriber wants to listen to.
34
     *
35
     * The array keys are event names and the value can be:
36
     *
37
     *  * The method name to call (priority defaults to 0)
38
     *  * An array composed of the method name to call and the priority
39
     *  * An array of arrays composed of the method names to call and respective
40
     *    priorities, or 0 if unset
41
     *
42
     * For instance:
43
     *
44
     *  * array('eventName' => 'methodName')
45
     *  * array('eventName' => array('methodName', $priority))
46
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
47
     *
48
     * @return array The event names to listen to
49
     */
50
    public static function getSubscribedEvents()
51
    {
52
        return array(
53
            BeforeExecuteEvent::EVENT_NAME => 'start',
54
            AfterExecuteEvent::EVENT_NAME => 'stop'
55
        );
56
    }
57
58
    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...
59
    {
60
        $this->startTime = microtime(true);
61
    }
62
63
    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...
64
    {
65
        $this->profiledTime = microtime(true) - $this->startTime;
66
    }
67
68
    public function getProfiledTimestamp()
69
    {
70
        if ($this->profiledTime < 0) {
71
            throw new RuntimeException('Profiling not started yet');
72
        }
73
        return $this->profiledTime;
74
    }
75
}