Completed
Push — spike ( a372d1...20d8a1 )
by Akihito
04:14
created

RefreshAnnotatedCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 2
b 1
f 0
cc 1
eloc 5
nc 1
nop 2
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\RepositoryModule\Annotation\AbstractCommand;
10
use BEAR\RepositoryModule\Annotation\Purge;
11
use BEAR\RepositoryModule\Annotation\Refresh;
12
use BEAR\Resource\ResourceInterface;
13
use BEAR\Resource\ResourceObject;
14
use BEAR\Resource\Uri;
15
use Ray\Aop\MethodInvocation;
16
17
class RefreshAnnotatedCommand implements CommandInterface
18
{
19
    /**
20
     * @var QueryRepositoryInterface
21
     */
22
    private $repository;
23
24
    /**
25
     * @var ResourceInterface
26
     */
27
    private $resource;
28
29 11
    public function __construct(
30
        QueryRepositoryInterface $repository,
31
        ResourceInterface $resource
32
    ) {
33 11
        $this->repository = $repository;
34 11
        $this->resource = $resource;
35 11
    }
36
37 7
    public function command(MethodInvocation $invocation, ResourceObject $ro)
38
    {
39 7
        $annotations = $invocation->getMethod()->getAnnotations();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class ReflectionMethod as the method getAnnotations() does only exist in the following sub-classes of ReflectionMethod: Ray\Aop\ReflectionMethod. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
40 7
        foreach ($annotations as $annotation) {
41 2
            $this->request($ro, $annotation);
42
        }
43 7
    }
44
45 2
    private function getUri(ResourceObject $resourceObject, AbstractCommand $annotation) : string
46
    {
47 2
        $body = \is_array($resourceObject->body) ? $resourceObject->body : [];
48 2
        $query = $body + $resourceObject->uri->query;
49 2
        $uri = uri_template($annotation->uri, $query);
50
51 2
        return $uri;
52
    }
53
54 2
    private function request(ResourceObject $resourceObject, $annotation)
55
    {
56 2
        if (! $annotation instanceof AbstractCommand) {
0 ignored issues
show
Bug introduced by
The class BEAR\RepositoryModule\Annotation\AbstractCommand does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
57 2
            return;
58
        }
59 2
        $uri = new Uri($this->getUri($resourceObject, $annotation));
60 2
        if ($annotation instanceof Purge) {
0 ignored issues
show
Bug introduced by
The class BEAR\RepositoryModule\Annotation\Purge does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
61 2
            $this->repository->purge($uri);
62
        }
63 2
        if ($annotation instanceof Refresh) {
0 ignored issues
show
Bug introduced by
The class BEAR\RepositoryModule\Annotation\Refresh does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
64 2
            $this->repository->purge($uri);
65 2
            $ro = $this->resource->get->uri($uri)->eager->request();
0 ignored issues
show
Bug introduced by
Accessing get on the interface BEAR\Resource\ResourceInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
66 2
            $this->repository->put($ro);
67
        }
68 2
    }
69
}
70