Issues (4)

src/Subscriber/EventSubscriber.php (2 issues)

Severity
1
<?php
2
/**
3
 * Behat Code Coverage
4
 */
5
declare(strict_types=1);
6
7
namespace DVDoug\Behat\CodeCoverage\Subscriber;
8
9
use Behat\Behat\EventDispatcher\Event\ExampleTested;
10
use Behat\Behat\EventDispatcher\Event\ScenarioTested;
11
use Behat\Testwork\EventDispatcher\Event\ExerciseCompleted;
12
use DVDoug\Behat\CodeCoverage\Service\ReportService;
13
use SebastianBergmann\CodeCoverage\CodeCoverage;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
class EventSubscriber implements EventSubscriberInterface
17
{
18
    private ?CodeCoverage $coverage;
19
20
    private ReportService $reportService;
21
22 222
    public function __construct(ReportService $reportService, ?CodeCoverage $coverage)
23
    {
24 222
        $this->reportService = $reportService;
25 222
        $this->coverage = $coverage;
26
    }
27
28 37
    public static function getSubscribedEvents(): array
29
    {
30 37
        return [
31 37
            ScenarioTested::BEFORE => 'beforeScenario',
32 37
            ExampleTested::BEFORE => 'beforeScenario',
33 37
            ScenarioTested::AFTER => 'afterScenario',
34 37
            ExampleTested::AFTER => 'afterScenario',
35 37
            ExerciseCompleted::AFTER => 'afterExercise',
36 37
        ];
37
    }
38
39
    /**
40
     * Before Scenario/Outline Example hook.
41
     */
42 74
    public function beforeScenario(ScenarioTested $event): void
43
    {
44 74
        if (!$this->coverage) {
45 37
            return;
46
        }
47
48 37
        $node = $event->getScenario();
49 37
        $id = $event->getFeature()->getFile() . ':' . $node->getLine();
50
51 37
        $this->coverage->start($id);
52
    }
53
54
    /**
55
     * After Scenario/Outline Example hook.
56
     */
57 109
    public function afterScenario(ScenarioTested $event): void
0 ignored issues
show
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

57
    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...
58
    {
59 109
        if (!$this->coverage) {
60 37
            return;
61
        }
62
63 72
        $this->coverage->stop();
64
    }
65
66
    /**
67
     * After Exercise hook.
68
     */
69 74
    public function afterExercise(ExerciseCompleted $event): void
0 ignored issues
show
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

69
    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...
70
    {
71 74
        if (!$this->coverage) {
72 37
            return;
73
        }
74
75 37
        $this->reportService->generateReport($this->coverage);
76
    }
77
}
78