Passed
Push — remove_compat_layer ( 27dc5c...3c8467 )
by Doug
03:22
created

EventSubscriber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
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 24
    public function __construct(ReportService $reportService, CodeCoverage $coverage = null)
39
    {
40 24
        $this->reportService = $reportService;
41 24
        $this->coverage = $coverage;
42 24
    }
43
44 4
    public static function getSubscribedEvents(): array
45
    {
46
        return [
47 4
            ScenarioTested::BEFORE => 'beforeScenario',
48 4
            ExampleTested::BEFORE => 'beforeScenario',
49 4
            ScenarioTested::AFTER => 'afterScenario',
50 4
            ExampleTested::AFTER => 'afterScenario',
51 4
            ExerciseCompleted::AFTER => 'afterExercise',
52
        ];
53
    }
54
55
    /**
56
     * Before Scenario/Outline Example hook.
57
     */
58 8
    public function beforeScenario(ScenarioTested $event): void
59
    {
60 8
        if (!$this->coverage) {
61 4
            return;
62
        }
63
64 4
        $node = $event->getScenario();
65 4
        $id = $event->getFeature()->getFile() . ':' . $node->getLine();
66
67 4
        $this->coverage->start($id);
68 4
    }
69
70
    /**
71
     * After Scenario/Outline Example hook.
72
     */
73 8
    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 8
        if (!$this->coverage) {
76 4
            return;
77
        }
78
79 4
        $this->coverage->stop();
80 4
    }
81
82
    /**
83
     * After Exercise hook.
84
     */
85 8
    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 8
        if (!$this->coverage) {
88 4
            return;
89
        }
90
91 4
        $this->reportService->generateReport($this->coverage);
92 4
    }
93
}
94