Passed
Branch master (203e41)
by Nate
01:56
created

HttpRequestAnnotHandler   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 2
dl 0
loc 28
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 15 2
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Internal\AnnotationHandler;
10
11
use Tebru\AnnotationReader\AbstractAnnotation;
12
use Tebru\Retrofit\Annotation\HttpRequest;
13
use Tebru\Retrofit\AnnotationHandler;
14
use Tebru\Retrofit\Converter;
15
use Tebru\Retrofit\ServiceMethodBuilder;
16
17
/**
18
 * Class HttpRequestAnnotHandler
19
 *
20
 * @author Nate Brunette <[email protected]>
21
 */
22
final class HttpRequestAnnotHandler implements AnnotationHandler
23
{
24
    /**
25
     * Sets the request method, uri, and whether or not the request contains a body
26
     *
27
     * @param HttpRequest|AbstractAnnotation $annotation The annotation to handle
28
     * @param ServiceMethodBuilder $serviceMethodBuilder Used to construct a [@see ServiceMethod]
29
     * @param Converter|null $converter Converter used to convert types before sending to service method
30
     * @param int|null $index The position of the parameter or null if annotation does not reference parameter
31
     * @return void
32
     * @throws \LogicException
33
     */
34 16
    public function handle(
35
        AbstractAnnotation $annotation,
36
        ServiceMethodBuilder $serviceMethodBuilder,
37
        ?Converter $converter,
38
        ?int $index
39
    ): void {
40 16
        $uri = $annotation->getValue();
41
42 16
        $serviceMethodBuilder->setMethod($annotation->getType());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Tebru\AnnotationReader\AbstractAnnotation as the method getType() does only exist in the following sub-classes of Tebru\AnnotationReader\AbstractAnnotation: Tebru\Retrofit\Annotation\DELETE, Tebru\Retrofit\Annotation\GET, Tebru\Retrofit\Annotation\HEAD, Tebru\Retrofit\Annotation\OPTIONS, Tebru\Retrofit\Annotation\PATCH, Tebru\Retrofit\Annotation\POST, Tebru\Retrofit\Annotation\PUT, Tebru\Retrofit\Annotation\REQUEST. 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...
43 16
        $serviceMethodBuilder->setPath($uri);
44
45 16
        if (!$annotation->hasBody()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Tebru\AnnotationReader\AbstractAnnotation as the method hasBody() does only exist in the following sub-classes of Tebru\AnnotationReader\AbstractAnnotation: Tebru\Retrofit\Annotation\DELETE, Tebru\Retrofit\Annotation\GET, Tebru\Retrofit\Annotation\HEAD, Tebru\Retrofit\Annotation\OPTIONS, Tebru\Retrofit\Annotation\PATCH, Tebru\Retrofit\Annotation\POST, Tebru\Retrofit\Annotation\PUT, Tebru\Retrofit\Annotation\REQUEST. 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...
46 11
            $serviceMethodBuilder->setHasBody($annotation->hasBody());
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Tebru\AnnotationReader\AbstractAnnotation as the method hasBody() does only exist in the following sub-classes of Tebru\AnnotationReader\AbstractAnnotation: Tebru\Retrofit\Annotation\DELETE, Tebru\Retrofit\Annotation\GET, Tebru\Retrofit\Annotation\HEAD, Tebru\Retrofit\Annotation\OPTIONS, Tebru\Retrofit\Annotation\PATCH, Tebru\Retrofit\Annotation\POST, Tebru\Retrofit\Annotation\PUT, Tebru\Retrofit\Annotation\REQUEST. 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...
47
        }
48 16
    }
49
}
50