Passed
Push — remove_compat_layer ( 1de0f9...27dc5c )
by Doug
12:36 queued 03:29
created

EventSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * Event subscriber.
6
 *
7
 * @copyright 2013 Anthon Pang
8
 *
9
 * @license BSD-2-Clause
10
 */
11
12
namespace DVDoug\Behat\CodeCoverage\Subscriber;
13
14
use Behat\Behat\EventDispatcher\Event\ExampleTested;
15
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
16
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
17
use DVDoug\Behat\CodeCoverage\Service\ReportService;
18
use SebastianBergmann\CodeCoverage\CodeCoverage;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
/**
22
 * Event subscriber.
23
 *
24
 * @author Anthon Pang <[email protected]>
25
 */
26
class EventSubscriber implements EventSubscriberInterface
27
{
28
    /**
29
     * @var CodeCoverage
30
     */
31
    private $coverage;
32
33
    /**
34
     * @var ReportService
35
     */
36
    private $reportService;
37
38
    public function __construct(ReportService $reportService, CodeCoverage $coverage = null)
39
    {
40
        $this->reportService = $reportService;
41
        $this->coverage = $coverage;
42
    }
43
44
    public static function getSubscribedEvents(): array
45
    {
46
        return [
47
            ScenarioTested::BEFORE => 'beforeScenario',
48
            ExampleTested::BEFORE => 'beforeScenario',
49
            ScenarioTested::AFTER => 'afterScenario',
50
            ExampleTested::AFTER => 'afterScenario',
51
            ExerciseCompleted::AFTER => 'afterExercise',
52
        ];
53
    }
54
55
    /**
56
     * Before Scenario/Outline Example hook.
57
     */
58 4
    public function beforeScenario(ScenarioTested $event): void
59
    {
60
        if (!$this->coverage) {
61
            return;
62
        }
63
64
        $node = $event->getScenario();
65
        $id = $event->getFeature()->getFile() . ':' . $node->getLine();
66
67
        $this->coverage->start($id);
68 4
    }
69
70
    /**
71
     * After Scenario/Outline Example hook.
72
     */
73 4
    public function afterScenario(ScenarioTested $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

73
    public function afterScenario(/** @scrutinizer ignore-unused */ ScenarioTested $event): void

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

Loading history...
74
    {
75 4
        if (!$this->coverage) {
76
            return;
77
        }
78
79 4
        $this->coverage->stop();
80
    }
81
82
    /**
83
     * After Exercise hook.
84
     */
85
    public function afterExercise(ExerciseCompleted $event): void
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

85
    public function afterExercise(/** @scrutinizer ignore-unused */ ExerciseCompleted $event): void

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

Loading history...
86
    {
87
        if (!$this->coverage) {
88
            return;
89
        }
90
91
        $this->reportService->generateReport($this->coverage);
92
    }
93
}
94