Test Failed
Push — master ( b0170f...710cb0 )
by Mathijs
05:30
created

JobClusterNameUpdateMapper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 34
ccs 0
cts 9
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mapMethod() 0 3 1
A mapUrl() 0 4 1
A mapConfig() 0 10 1
1
<?php
2
declare(strict_types=1);
3
namespace mcorten87\rabbitmq_api\mappers;
4
5
use mcorten87\rabbitmq_api\jobs\JobBase;
6
use mcorten87\rabbitmq_api\jobs\JobBindingListAll;
7
use mcorten87\rabbitmq_api\jobs\JobClusterNameList;
8
use mcorten87\rabbitmq_api\jobs\JobClusterNameUpdate;
9
use mcorten87\rabbitmq_api\objects\Method;
10
use mcorten87\rabbitmq_api\objects\Url;
11
use mcorten87\rabbitmq_api\services\MqManagementConfig;
12
13
class JobClusterNameUpdateMapper  extends BaseMapper
0 ignored issues
show
Coding Style introduced by
Expected 1 space after class name; 2 found
Loading history...
Coding Style introduced by
Expected 1 space before extends keyword; 2 found
Loading history...
14
{
15
16
    /**
17
     * @return Method
18
     */
19
    protected function mapMethod() : Method {
20
        return new Method(Method::PUT);
21
    }
22
23
    /**
24
     * @param JobClusterNameUpdate $job
25
     * @return Url
26
     */
27
    protected function mapUrl(JobBase $job) : Url {
28
        $url = 'cluster-name';
29
        return new Url($url);
30
    }
31
32
    /**
33
     * @param JobClusterNameUpdate $job
34
     * @return array
35
     */
36
    protected function mapConfig(JobBase $job): array
37
    {
38
        $body = [
39
            'name' => $job->getName(),
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class mcorten87\rabbitmq_api\jobs\JobBase as the method getName() does only exist in the following sub-classes of mcorten87\rabbitmq_api\jobs\JobBase: mcorten87\rabbitmq_api\jobs\JobClusterNameUpdate. 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
        ];
41
42
        return array_merge(parent::mapConfig($job), [
43
            'json'      =>  $body,
44
        ]);
45
    }
46
}
47