Completed
Push — master ( 495169...b31514 )
by Changwan
10:11
created

ArrayAccessLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 13
cts 14
cp 0.9286
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A middleware() 0 7 2
A execute() 0 11 4
1
<?php
2
namespace Wandu\Router\Loader;
3
4
use Psr\Http\Message\ServerRequestInterface;
5
use Wandu\Router\Contracts\LoaderInterface;
6
use Wandu\Router\Contracts\MiddlewareInterface;
7
use Wandu\Router\Exception\HandlerNotFoundException;
8
9
class ArrayAccessLoader implements LoaderInterface
10
{
11
    /** @var \ArrayAccess */
12
    protected $container;
13
14
    /**
15
     * @param \ArrayAccess|array $container
16
     */
17 4
    public function __construct($container)
18
    {
19 4
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container can also be of type array. However, the property $container is declared as type object<ArrayAccess>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
20 4
    }
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 2
    public function middleware(string $className, ServerRequestInterface $request): MiddlewareInterface
26
    {
27 2
        if (!isset($this->container[$className])) {
28 1
            throw new HandlerNotFoundException($className);
29
        }
30 1
        return $this->container[$className];
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 2
    public function execute(string $className, string $methodName, ServerRequestInterface $request)
37
    {
38 2
        if (!isset($this->container[$className])) {
39 1
            throw new HandlerNotFoundException($className, $methodName);
40
        }
41 1
        $object = $this->container[$className];
42 1
        if (!method_exists($object, $methodName) && !method_exists($object, '__call')) {
43
            throw new HandlerNotFoundException($className, $methodName);
44
        }
45 1
        return call_user_func([$object, $methodName], $request);
46
    }
47
}
48