Passed
Push — 1.x ( 9a36fe...980d56 )
by Akihito
02:01 queued 14s
created

EnvAuthProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 6 1
A __construct() 0 4 1
A setContext() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\AuraSqlModule;
6
7
use Ray\AuraSqlModule\Annotation\EnvAuth;
8
use Ray\Di\ProviderInterface;
9
use Ray\Di\SetContextInterface;
10
11
use function assert;
12
use function getenv;
13
use function is_string;
14
15
/**
16
 * @implements ProviderInterface<string>
17
 */
18
class EnvAuthProvider implements ProviderInterface, SetContextInterface
19
{
20
    private string $context;
21
22
    /** @var array<string, string>  */
23
    private array $envAuth;
24
25
    /** @param string $context */
26
    public function setContext($context): void
27
    {
28
        $this->context = $context;
29
    }
30
31
    /**
32
     * @param array<string, string> $envAuth
33
     *
34
     * @EnvAuth
35
     */
36
    #[EnvAuth]
37
    public function __construct(array $envAuth)
38
    {
39
        $this->envAuth = $envAuth;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function get(): string
46
    {
47
        $value = (string) getenv($this->envAuth[$this->context]);
48
        assert(is_string($value));
49
50
        return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type string which is incompatible with the return type mandated by Ray\Di\ProviderInterface::get() of Ray\Di\T.

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...
51
    }
52
}
53