Passed
Pull Request — master (#42)
by Kevin
02:15
created

ShellVerbosityResetter::resetEnv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task\Runner;
4
5
/**
6
 * @internal
7
 *
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class ShellVerbosityResetter
11
{
12
    private $var;
13
    private $env;
14
    private $server;
15
16
    public function __construct()
17
    {
18
        $this->var = \getenv('SHELL_VERBOSITY');
19
        $this->env = $_ENV['SHELL_VERBOSITY'] ?? false;
20
        $this->server = $_SERVER['SHELL_VERBOSITY'] ?? false;
21
    }
22
23
    public function reset(): void
24
    {
25
        $this->resetVar();
26
        $this->resetEnv();
27
        $this->resetServer();
28
    }
29
30
    private function resetVar(): void
31
    {
32
        if (!\function_exists('putenv')) {
33
            return;
34
        }
35
36
        if (false === $this->var) {
37
            // unset as it wasn't set to begin with
38
            @\putenv('SHELL_VERBOSITY');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for putenv(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

38
            /** @scrutinizer ignore-unhandled */ @\putenv('SHELL_VERBOSITY');

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
40
            return;
41
        }
42
43
        @\putenv("SHELL_VERBOSITY={$this->var}");
44
    }
45
46
    private function resetEnv(): void
47
    {
48
        if (false === $this->env) {
49
            // unset as it wasn't set to begin with
50
            unset($_ENV['SHELL_VERBOSITY']);
51
52
            return;
53
        }
54
55
        $_ENV['SHELL_VERBOSITY'] = $this->env;
56
    }
57
58
    private function resetServer(): void
59
    {
60
        if (false === $this->server) {
61
            // unset as it wasn't set to begin with
62
            unset($_SERVER['SHELL_VERBOSITY']);
63
64
            return;
65
        }
66
67
        $_SERVER['SHELL_VERBOSITY'] = $this->server;
68
    }
69
}
70