Passed
Pull Request — develop (#8)
by ANTHONIUS
04:34
created

RemoteSession::xdebugPatch()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 16
nc 4
nop 0
dl 0
loc 25
ccs 0
cts 0
cp 0
crap 42
rs 9.1111
c 0
b 0
f 0
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\Bridge\CodeCoverage\Session;
15
16
use Doyo\Behat\Coverage\Bridge\CodeCoverage\TestCase;
17
use phpDocumentor\Reflection\Types\Parent_;
18
use Symfony\Component\HttpFoundation\Request;
19
20
class RemoteSession extends Session
21
{
22
    const HEADER_SESSION_KEY   = 'HTTP_DOYO_COVERAGE_SESSION';
23
    const HEADER_TEST_CASE_KEY = 'HTTP_DOYO_COVERAGE_TESTCASE';
24
25 1
    public static function startSession(Request $request = null)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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

25
    public static function startSession(/** @scrutinizer ignore-unused */ Request $request = null)

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...
26
    {
27
        if (!isset($_SERVER[static::HEADER_SESSION_KEY])) {
28
            return;
29
        }
30
31
        $name    = $_SERVER[static::HEADER_SESSION_KEY];
32
        $session = new static($name);
33
        if (isset($_SERVER[static::HEADER_TEST_CASE_KEY])) {
34
            $name     = $_SERVER[static::HEADER_TEST_CASE_KEY];
35
            $testCase = new TestCase($name);
36
            $session->setTestCase($testCase);
37
38
            $session->start();
39 1
            register_shutdown_function([$session,'shutdown']);
40
41 1
            $session->xdebugPatch();
42
        }
43
44 1
        return $session;
45
    }
46
47
    /**
48
     * @codeCoverageIgnore
49
     */
50
    public function xdebugPatch()
51
    {
52
        $options = $this->filterOptions;
53
        $filterKey = 'whitelistedFiles';
54
55
        if(
56
            !extension_loaded('xdebug')
57
            || !function_exists('xdebug_set_filter')
58
            || !isset($options[$filterKey])
59
        ){
60
            return;
61
        }
62
63
        $dirs = [];
64
        foreach($options[$filterKey] as $fileName => $status){
65
            $dir = dirname($fileName);
66
            if(!in_array($dir, $dirs)){
67
                $dirs[] = $dir;
68
            }
69
        }
70
71
        xdebug_set_filter(
72
            XDEBUG_FILTER_CODE_COVERAGE,
1 ignored issue
show
Bug introduced by
The constant Doyo\Behat\Coverage\Brid...UG_FILTER_CODE_COVERAGE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
73
            XDEBUG_PATH_WHITELIST,
1 ignored issue
show
Bug introduced by
The constant Doyo\Behat\Coverage\Brid...n\XDEBUG_PATH_WHITELIST was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
74
            $dirs
75
        );
76
    }
77
78 2
    public function init(array $config)
79
    {
80 2
        if (isset($config['codeCoverageOptions'])) {
81 2
            $this->setCodeCoverageOptions($config['codeCoverageOptions']);
82
        }
83
84 2
        if (isset($config['filterOptions'])) {
85 2
            $this->setFilterOptions($config['filterOptions']);
86
        }
87
88 2
        $this->reset();
89
    }
90
91 1
    public function stop()
92
    {
93 1
        $processor = $this->processor;
94 1
        $aggregate = $this->data;
95
96 1
        $processor->stop();
97 1
        $processor->updateCoverage($aggregate);
98
99 1
        $this->data = $processor->getData();
100
    }
101
}
102