ShellVerbosityResetter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
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 7
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
    /** @var false|string */
13
    private $var;
14
15
    /** @var false|string */
16
    private $env;
17
18
    /** @var false|string */
19
    private $server;
20
21
    public function __construct()
22
    {
23
        $var = \getenv('SHELL_VERBOSITY');
24
25
        $this->var = \is_string($var) ? $var : false;
0 ignored issues
show
introduced by
The condition is_string($var) is always true.
Loading history...
26
        $this->env = $_ENV['SHELL_VERBOSITY'] ?? false;
27
        $this->server = $_SERVER['SHELL_VERBOSITY'] ?? false;
28
    }
29
30
    public function reset(): void
31
    {
32
        $this->resetVar();
33
        $this->resetEnv();
34
        $this->resetServer();
35
    }
36
37
    private function resetVar(): void
38
    {
39
        if (!\function_exists('putenv')) {
40
            return;
41
        }
42
43
        if (false === $this->var) {
44
            // unset as it wasn't set to begin with
45
            @\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

45
            /** @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...
46
47
            return;
48
        }
49
50
        @\putenv("SHELL_VERBOSITY={$this->var}");
51
    }
52
53
    private function resetEnv(): void
54
    {
55
        if (false === $this->env) {
56
            // unset as it wasn't set to begin with
57
            unset($_ENV['SHELL_VERBOSITY']);
58
59
            return;
60
        }
61
62
        $_ENV['SHELL_VERBOSITY'] = $this->env;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->env can also be of type true. However, the property $env is declared as type false|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
    }
64
65
    private function resetServer(): void
66
    {
67
        if (false === $this->server) {
68
            // unset as it wasn't set to begin with
69
            unset($_SERVER['SHELL_VERBOSITY']);
70
71
            return;
72
        }
73
74
        $_SERVER['SHELL_VERBOSITY'] = $this->server;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->server can also be of type true. However, the property $server is declared as type false|string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
75
    }
76
}
77