Passed
Pull Request — 1.x (#68)
by Akihito
01:29
created

EnvAuthProvider::setContext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
14
/**
15
 * @implements ProviderInterface<string>
16
 */
17
class EnvAuthProvider implements ProviderInterface, SetContextInterface
18
{
19
    private string $context;
20
21
    /** @var array<string, string>  */
22
    private array $envAuth;
23
24
    /** @param string $context */
25
    public function setContext($context): void
26
    {
27
        $this->context = $context;
28
    }
29
30
    /**
31
     * @param array<string, string> $envAuth
32
     *
33
     * @EnvAuth
34
     */
35
    #[EnvAuth]
36
    public function __construct(array $envAuth)
37
    {
38
        $this->envAuth = $envAuth;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function get(): string
45
    {
46
        $value = (string) getenv($this->envAuth[$this->context]);
47
        assert($value);
48
49
        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...
50
    }
51
}
52