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
/** @implements ProviderInterface<ExtendedPdoInterface> */
16
class AuraSqlReplicationDbProvider implements ProviderInterface, SetContextInterface
17
{
18
    private InjectorInterface $injector;
19
    private string $context = '';
20
21
    public function __construct(InjectorInterface $injector)
22
    {
23
        $this->injector = $injector;
24
    }
25
26
    /**
27
     * {@inheritDoc}
28
     *
29
     * @param string $context
30
     */
31
    public function setContext($context): void
32
    {
33
        $this->context = $context;
34
    }
35
36
    /**
37
     * {@inheritDoc}
38
     */
39
    public function get(): ExtendedPdoInterface
40
    {
41
        $connectionLocator = $this->injector->getInstance(ConnectionLocatorInterface::class, $this->context);
42
        assert($connectionLocator instanceof ConnectionLocatorInterface);
43
        $isGetRequest = isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] === 'GET';
44
45
        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...
46
    }
47
}
48