CoverageListener::afterSuite()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 7
cts 8
cp 0.875
cc 2
nc 2
nop 0
crap 2.0078
1
<?php
2
3
4
namespace Doyo\PhpSpec\CodeCoverage\Listener;
5
6
7
use Doyo\Bridge\CodeCoverage\ProcessorInterface;
8
use Doyo\Bridge\CodeCoverage\TestCase;
9
use Doyo\PhpSpec\CodeCoverage\Event\CoverageEvent;
10
use Doyo\Symfony\Bridge\EventDispatcher\EventDispatcher;
11
use Doyo\Symfony\Bridge\EventDispatcher\EventDispatcherInterface;
12
use PhpSpec\Console\ConsoleIO;
13
use PhpSpec\Event\ExampleEvent;
14
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15
16
class CoverageListener implements EventSubscriberInterface
17
{
18
    /**
19
     * @var ProcessorInterface $processor
20
     */
21
    private $processor;
22
23
    /**
24
     * @var ConsoleIO
25
     */
26
    private $consoleIO;
27
28
    /**
29
     * @var EventDispatcherInterface
30
     */
31
    private $dispatcher;
32
33
    /**
34
     * @var bool
35
     */
36
    private $canCollectCoverage;
37
38
    /**
39
     * CoverageListener constructor.
40
     * @param ProcessorInterface $processor
41
     * @param ConsoleIO $consoleIO
42
     */
43 6
    public function __construct(
44
        EventDispatcher $dispatcher,
45
        ProcessorInterface $processor,
46
        ConsoleIO $consoleIO,
47
        bool $canCollectCoverage
48
    )
49
    {
50 6
        $this->dispatcher = $dispatcher;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dispatcher of type Doyo\Symfony\Bridge\Even...patcher\EventDispatcher is incompatible with the declared type Doyo\Symfony\Bridge\Even...ventDispatcherInterface of property $dispatcher.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
51 6
        $this->processor = $processor;
52 6
        $this->consoleIO = $consoleIO;
53 6
        $this->canCollectCoverage = $canCollectCoverage;
54
    }
55
56 1
    public static function getSubscribedEvents()
57
    {
58
        return [
59 1
            'beforeExample' => ['beforeExample', -10],
60
            'afterExample' => ['afterExample', -10],
61
            'afterSuite' => ['afterSuite', -10]
62
        ];
63
    }
64
65 1
    public function beforeExample(ExampleEvent $suiteEvent)
66
    {
67 1
        if(!$this->canCollectCoverage){
68
            return;
69
        }
70 1
        $example = $suiteEvent->getExample();
71 1
        $processor = $this->processor;
72
73 1
        $name = strtr('%spec%::%example%', [
74 1
            '%spec%'    => $example->getSpecification()->getTitle(),
75 1
            '%example%' => $example->getFunctionReflection()->getName(),
76
        ]);
77 1
        $testCase = new TestCase($name);
78 1
        $processor->setCurrentTestCase($testCase);
79 1
        $processor->start($testCase);
80
    }
81
82 15
    public function afterExample(ExampleEvent $exampleEvent)
83
    {
84 15
        if(!$this->canCollectCoverage){
85
            return;
86
        }
87 15
        $processor = $this->processor;
88 15
        $result = $exampleEvent->getResult();
89 15
        $testCase = $processor->getCurrentTestCase();
90
91
        $map = [
92 15
            ExampleEvent::PASSED => TestCase::RESULT_PASSED,
93 15
            ExampleEvent::SKIPPED => TestCase::RESULT_SKIPPED,
94 15
            ExampleEvent::FAILED => TestCase::RESULT_FAILED,
95 15
            ExampleEvent::BROKEN => TestCase::RESULT_ERROR,
96 15
            ExampleEvent::PENDING => TestCase::RESULT_SKIPPED,
97
        ];
98
99 15
        $result = $map[$result];
100 15
        $testCase->setResult($result);
101 15
        $processor->addTestCase($testCase);
102 15
        $processor->stop();
103
    }
104
105 1
    public function afterSuite()
106
    {
107 1
        if(!$this->canCollectCoverage){
108
            return;
109
        }
110 1
        $processor = $this->processor;
111 1
        $consoleIO = $this->consoleIO;
112 1
        $dispatcher = $this->dispatcher;
113 1
        $event = new CoverageEvent($processor, $consoleIO);
114
115 1
        $dispatcher->dispatch($event, CoverageEvent::REPORT);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with Doyo\PhpSpec\CodeCoverag...t\CoverageEvent::REPORT. ( Ignorable by Annotation )

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

115
        $dispatcher->/** @scrutinizer ignore-call */ 
116
                     dispatch($event, CoverageEvent::REPORT);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
116
    }
117
}