AuraSqlReplicationDbProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A setContext() 0 3 1
A get() 0 7 3
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