Passed
Push — 0.7.0 ( a316e5...a89e02 )
by Alexander
03:01
created

ApacheAdapter::isSupported()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php 
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file LICENSE.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2021 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /LICENSE
21
 */
22
23
namespace Syscodes\Dotenv\Repository\Adapters;
24
25
use Syscodes\Contracts\Dotenv\Adapter;
26
27
/**
28
 * Read, write and delete an environment variable for 
29
 * subprocess_env of Apache.
30
 * 
31
 * @author Alexander Campo <[email protected]>
32
 */
33
class ApacheAdapter implements Adapter
34
{
35
    /**
36
     * Determines if the adapter is supported.
37
     * 
38
     * @return bool
39
     */
40
    public function isSupported()
41
    {
42
        return function_exists('apache_getenv' && function_exists('apache_setenv'));
0 ignored issues
show
Bug introduced by
'apache_getenv' && funct...exists('apache_setenv') of type boolean is incompatible with the type string expected by parameter $function of function_exists(). ( Ignorable by Annotation )

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

42
        return function_exists(/** @scrutinizer ignore-type */ 'apache_getenv' && function_exists('apache_setenv'));
Loading history...
43
    }
44
45
    /**
46
     * Check if a variable exists.
47
     * 
48
     * @param  string  $name
49
     * 
50
     * @return bool
51
     */
52
    public function has(string $name)
53
    {
54
        return false === apache_getenv($name);
55
    }
56
57
    /**
58
     * Read an environment variable.
59
     * 
60
     * @param  string  $name
61
     * 
62
     * @return mixed
63
     */
64
    public function read(string $name)
65
    {
66
        if ($this->has($name)) {
67
            return apache_getenv($name);
0 ignored issues
show
Bug Best Practice introduced by
The expression return apache_getenv($name) returns the type string which is incompatible with the return type mandated by Syscodes\Contracts\Dotenv\Adapter::read() of boolean.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
68
        }
69
70
        return null;
71
    }
72
73
     /**
74
     * Write to an environment variable.
75
     * 
76
     * @param  string  $name
77
     * @param  string  $value
78
     * 
79
     * @return bool
80
     */
81
    public function write(string $name, string $value)
82
    {
83
        return apache_setenv($name, $value);
84
    }
85
86
    /**
87
     * Delete an environment variable.
88
     * 
89
     * @param  string  $name
90
     * 
91
     * @return bool
92
     */
93
    public function delete(string $name)
94
    {
95
        return apache_setenv($name, '');
96
    }
97
}