Completed
Push — 1.x ( fc62ce...abf8ac )
by Akihito
02:10
created

RefreshSameCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of the BEAR.QueryRepository package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\QueryRepository;
8
9
use BEAR\QueryRepository\Exception\UnmatchedQuery;
10
use BEAR\Resource\ResourceObject;
11
use Ray\Aop\MethodInvocation;
12
13
class RefreshSameCommand implements CommandInterface
14
{
15
    /**
16
     * @var QueryRepositoryInterface
17
     */
18
    private $repository;
19
20 11
    public function __construct(QueryRepositoryInterface $repository)
21
    {
22 11
        $this->repository = $repository;
23 11
    }
24
25
    /**
26
     * @param MethodInvocation $invocation
27
     * @param ResourceObject   $resourceObject
28
     */
29 8
    public function command(MethodInvocation $invocation, ResourceObject $resourceObject)
30
    {
31 8
        $method = $invocation->getMethod()->getName();
0 ignored issues
show
Bug introduced by
Consider using $invocation->getMethod()->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
32 8
        if ($method === 'onGet' || $method === 'onPost') {
33
            return;
34
        }
35 8
        unset($invocation);
36 8
        $onGet = [$resourceObject, 'onGet'];
37 8
        $getQuery = $this->getQuery($resourceObject, $onGet);
0 ignored issues
show
Unused Code introduced by
The call to RefreshSameCommand::getQuery() has too many arguments starting with $onGet.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
38 7
        $delUri = clone $resourceObject->uri;
39 7
        $delUri->query = $getQuery;
40
41
        // delete data in repository
42 7
        $this->repository->purge($delUri);
0 ignored issues
show
Compatibility introduced by
$delUri of type object<BEAR\Resource\AbstractUri> is not a sub-type of object<BEAR\Resource\Uri>. It seems like you assume a child class of the class BEAR\Resource\AbstractUri to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
43
44
        // GET for re-generate (in interceptor)
45 7
        $resourceObject->uri->query = $getQuery;
46 7
        call_user_func_array($onGet, $getQuery);
47 7
    }
48
49
    /**
50
     * @param ResourceObject $resourceObject
51
     *
52
     * @return array
53
     */
54 8
    private function getQuery(ResourceObject $resourceObject)
55
    {
56 8
        $refParameters = (new \ReflectionMethod($resourceObject, 'onGet'))->getParameters();
57 8
        $getQuery = [];
58 8
        $query = $resourceObject->uri->query;
59 8
        foreach ($refParameters as $parameter) {
60 8
            if (! isset($query[$parameter->name])) {
61 1
                throw new UnmatchedQuery($resourceObject->uri->method . ' ' . (string) $resourceObject->uri);
62
            }
63 8
            $getQuery[$parameter->name] = $query[$parameter->name];
64 8
        }
65
66 7
        return $getQuery;
67
    }
68
}
69