AuraSqlReplicationDbProvider::setContext()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
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 Aura\Sql\ConnectionLocatorInterface;
8
use Aura\Sql\ExtendedPdoInterface;
9
use Ray\Di\InjectorInterface;
10
use Ray\Di\ProviderInterface;
11
use Ray\Di\SetContextInterface;
12
13
use function assert;
14
15
/**
16
 * @implements ProviderInterface<ExtendedPdoInterface>
17
 */
18
class AuraSqlReplicationDbProvider implements ProviderInterface, SetContextInterface
19
{
20
    private InjectorInterface $injector;
21
    private string $context = '';
22
23
    public function __construct(InjectorInterface $injector)
24
    {
25
        $this->injector = $injector;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     *
31
     * @param string $context
32
     */
33
    public function setContext($context): void
34
    {
35
        $this->context = $context;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function get(): ExtendedPdoInterface
42
    {
43
        $connectionLocator = $this->injector->getInstance(ConnectionLocatorInterface::class, $this->context);
44
        assert($connectionLocator instanceof ConnectionLocatorInterface);
45
        $isGetRequest = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET';
46
47
        return $isGetRequest ? $connectionLocator->getRead() : $connectionLocator->getWrite();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $isGetRequest ? $...tionLocator->getWrite() returns the type Aura\Sql\ExtendedPdoInterface 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...
48
    }
49
}
50