Passed
Push — develop ( a4c97f...02f249 )
by ANTHONIUS
04:04
created

CoverageListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 37
c 0
b 0
f 0
dl 0
loc 85
rs 10
ccs 34
cts 34
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getSubscribedEvents() 0 6 1
A afterSuite() 0 8 1
A afterExample() 0 18 1
A beforeExample() 0 13 1
1
<?php
2
3
4
namespace Doyo\PhpSpec\CodeCoverage\Listener;
5
6
7
use Doyo\PhpSpec\CodeCoverage\ProcessorInterface;
8
use Doyo\Behat\Coverage\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 PhpSpec\Event\SuiteEvent;
15
use PhpSpec\Loader\Node\ExampleNode;
16
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17
18
class CoverageListener implements EventSubscriberInterface
19
{
20
    /**
21
     * @var ProcessorInterface $processor
22
     */
23
    private $processor;
24
25
    /**
26
     * @var ConsoleIO
27
     */
28
    private $consoleIO;
29
30
    /**
31
     * @var EventDispatcherInterface
32
     */
33
    private $dispatcher;
34
35
    /**
36
     * CoverageListener constructor.
37
     * @param ProcessorInterface $processor
38
     * @param ConsoleIO $consoleIO
39
     */
40 5
    public function __construct(
41
        EventDispatcher $dispatcher,
42
        ProcessorInterface $processor,
43
        ConsoleIO $consoleIO
44
    )
45
    {
46 5
        $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...
47 5
        $this->processor = $processor;
48 5
        $this->consoleIO = $consoleIO;
49
    }
50
51 1
    public static function getSubscribedEvents()
52
    {
53
        return [
54 1
            'beforeExample' => ['beforeExample', -10],
55
            'afterExample' => ['afterExample', -10],
56
            'afterSuite' => ['afterSuite', -10]
57
        ];
58
    }
59
60 1
    public function beforeExample(ExampleEvent $suiteEvent)
61
    {
62 1
        $example = $suiteEvent->getExample();
63 1
        $processor = $this->processor;
64
65 1
        $name = strtr('%spec%::%example%', [
66 1
            '%spec%'    => $example->getSpecification()->getTitle(),
67 1
            '%example%' => $example->getFunctionReflection()->getName(),
68
        ]);
69 1
        $testCase = new TestCase($name);
70
71 1
        $processor->setCurrentTestCase($testCase);
72 1
        $processor->start($testCase);
73
    }
74
75 9
    public function afterExample(ExampleEvent $exampleEvent)
76
    {
77 9
        $processor = $this->processor;
78 9
        $result = $exampleEvent->getResult();
79 9
        $testCase = $processor->getCurrentTestCase();
80
81
        $map = [
82 9
            ExampleEvent::PASSED => TestCase::RESULT_PASSED,
83 9
            ExampleEvent::SKIPPED => TestCase::RESULT_SKIPPED,
84 9
            ExampleEvent::FAILED => TestCase::RESULT_FAILED,
85 9
            ExampleEvent::BROKEN => TestCase::RESULT_ERROR,
86 9
            ExampleEvent::PENDING => TestCase::RESULT_SKIPPED,
87
        ];
88
89 9
        $result = $map[$result];
90 9
        $testCase->setResult($result);
91 9
        $processor->addTestCase($testCase);
92 9
        $processor->stop();
93
    }
94
95 1
    public function afterSuite()
96
    {
97 1
        $processor = $this->processor;
98 1
        $consoleIO = $this->consoleIO;
99 1
        $dispatcher = $this->dispatcher;
100 1
        $event = new CoverageEvent($processor, $consoleIO);
101
102 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

102
        $dispatcher->/** @scrutinizer ignore-call */ 
103
                     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...
103
    }
104
}