Completed
Push — develop ( c86111...2faf16 )
by ANTHONIUS
21s
created

CoverageController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 82
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A validateEvent() 0 4 2
A execute() 0 4 2
A __construct() 0 3 1
A getSubscribedEvents() 0 11 1
A onBeforeReportProcess() 0 5 1
A configure() 0 3 1
A onAfterReportProcess() 0 14 3
1
<?php
2
3
/*
4
 * This file is part of the doyo/behat-coverage-extension project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Doyo\Behat\Coverage\Controller\Cli;
15
16
use Behat\Testwork\Cli\Controller;
17
use Doyo\Behat\Coverage\Bridge\Symfony\Event;
18
use Doyo\Behat\Coverage\Event\CoverageEvent;
19
use Doyo\Behat\Coverage\Event\ReportEvent;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Console\Style\StyleInterface;
25
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
26
27
/**
28
 * Code Coverage Cli Controller.
29
 *
30
 * @author Anthonius Munthi <[email protected]>
31
 */
32
class CoverageController implements Controller, EventSubscriberInterface
33
{
34
    /**
35
     * @var StyleInterface|null
36
     */
37
    private $style;
38
39
    /**
40
     * @var bool
41
     */
42
    private $coverageEnabled = false;
43
44
    /**
45
     * CoverageController constructor.
46
     *
47
     * @param StyleInterface|null $style
48
     */
49 9
    public function __construct(StyleInterface $style)
50
    {
51 9
        $this->style = $style;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 1
    public function configure(Command $command)
58
    {
59 1
        $command->addOption('coverage', null, InputOption::VALUE_NONE, 'Collecting code coverage');
60
    }
61
62 3
    public static function getSubscribedEvents()
63
    {
64
        return [
65
            ReportEvent::BEFORE_PROCESS => [
66 3
                ['validateEvent', 1000],
67
                ['onBeforeReportProcess', 100],
68
            ],
69 3
            ReportEvent::AFTER_PROCESS    => 'onAfterReportProcess',
70 3
            CoverageEvent::BEFORE_REFRESH => ['validateEvent', 1000],
71 3
            CoverageEvent::BEFORE_START   => ['validateEvent', 1000],
72 3
            CoverageEvent::BEFORE_STOP    => ['validateEvent', 1000],
73
        ];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 1
    public function execute(InputInterface $input, OutputInterface $output)
80
    {
81 1
        if ($input->hasParameterOption(['--coverage'])) {
82 1
            $this->coverageEnabled = true;
83
        }
84
    }
85
86 6
    public function validateEvent(Event $event)
87
    {
88 6
        if (!$this->coverageEnabled) {
89 1
            $event->stopPropagation();
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\EventD...vent::stopPropagation() has been deprecated: since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead ( Ignorable by Annotation )

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

89
            /** @scrutinizer ignore-deprecated */ $event->stopPropagation();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
90
        }
91
    }
92
93 1
    public function onBeforeReportProcess(ReportEvent $event)
94
    {
95 1
        $io = $this->style;
96 1
        $io->section('behat coverage reports process started');
1 ignored issue
show
Bug introduced by
The method section() does not exist on null. ( Ignorable by Annotation )

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

96
        $io->/** @scrutinizer ignore-call */ 
97
             section('behat coverage reports process started');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97 1
        $event->setIO($io);
0 ignored issues
show
Bug introduced by
It seems like $io can also be of type null; however, parameter $io of Doyo\Behat\Coverage\Event\ReportEvent::setIO() does only seem to accept Symfony\Component\Console\Style\StyleInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

97
        $event->setIO(/** @scrutinizer ignore-type */ $io);
Loading history...
98
    }
99
100 2
    public function onAfterReportProcess(ReportEvent $event)
101
    {
102 2
        $exceptions = $event->getExceptions();
103 2
        $io         = $event->getIO();
104 2
        if (0 === \count($exceptions)) {
105 1
            $io->success('behat coverage reports process completed');
106
107 1
            return;
108
        }
109
110 1
        $io->newLine(2);
111 1
        $io->section('behat coverage reports process failed');
112 1
        foreach ($exceptions as $exception) {
113 1
            $io->error($exception->getMessage());
114
        }
115
    }
116
}
117