Passed
Branch master (203e41)
by Nate
02:00
created

PartAnnotHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 4
crap 1
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\Part;
13
use Tebru\Retrofit\AnnotationHandler;
14
use Tebru\Retrofit\Converter;
15
use Tebru\Retrofit\Internal\ParameterHandler\PartParamHandler;
16
use Tebru\Retrofit\RequestBodyConverter;
17
use Tebru\Retrofit\ServiceMethodBuilder;
18
19
/**
20
 * Class PartAnnotHandler
21
 *
22
 * @author Nate Brunette <[email protected]>
23
 */
24
final class PartAnnotHandler implements AnnotationHandler
25
{
26
    /**
27
     * Handle an annotation, mutating the [@see ServiceMethodBuilder] based on the value
28
     *
29
     * @param Part|AbstractAnnotation $annotation The annotation to handle
30
     * @param ServiceMethodBuilder $serviceMethodBuilder Used to construct a [@see ServiceMethod]
31
     * @param Converter|RequestBodyConverter $converter Converter used to convert types before sending to service method
32
     * @param int|null $index The position of the parameter or null if annotation does not reference parameter
33
     * @return void
34
     */
35 2
    public function handle(
36
        AbstractAnnotation $annotation,
37
        ServiceMethodBuilder $serviceMethodBuilder,
38
        ?Converter $converter,
39
        ?int $index
40
    ): void {
41 2
        $serviceMethodBuilder->setIsMultipart();
42 2
        $serviceMethodBuilder->addParameterHandler(
43 2
            $index,
44 2
            new PartParamHandler($converter, $annotation->getValue(), $annotation->getEncoding())
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 getEncoding() does only exist in the following sub-classes of Tebru\AnnotationReader\AbstractAnnotation: Tebru\Retrofit\Annotation\Part, Tebru\Retrofit\Annotation\PartMap. 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...
Compatibility introduced by
$converter of type object<Tebru\Retrofit\Converter> is not a sub-type of object<Tebru\Retrofit\RequestBodyConverter>. It seems like you assume a child interface of the interface Tebru\Retrofit\Converter 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...
45
        );
46 2
    }
47
}
48