Completed
Push — master ( 21e79a...88f484 )
by Mathijs
03:02
created

JobConnectionListVirtualHostMapper::mapUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 7
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
crap 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\JobConnectionListVirtualHost;
7
use mcorten87\rabbitmq_api\jobs\JobDefinitionListVirtualHost;
8
use mcorten87\rabbitmq_api\objects\Method;
9
use mcorten87\rabbitmq_api\objects\Url;
10
use mcorten87\rabbitmq_api\services\MqManagementConfig;
11
12 View Code Duplication
class JobConnectionListVirtualHostMapper extends BaseMapper
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
15
    /**
16
     * @return Method
17
     */
18 1
    protected function mapMethod() : Method
19
    {
20 1
        return new Method(Method::GET);
21
    }
22
23
    /**
24
     * @param JobConnectionListVirtualHost $job
25
     * @return Url
26
     */
27 1
    protected function mapUrl(JobBase $job) : Url
28
    {
29 1
        $url = sprintf('vhosts');
30 1
        $url .= sprintf('/%1$s', urlencode((string)$job->getVirtualHost()));
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 getVirtualHost() does only exist in the following sub-classes of mcorten87\rabbitmq_api\jobs\JobBase: mcorten87\rabbitmq_api\j...BetweenQueueAndExchange, mcorten87\rabbitmq_api\jobs\JobBindingListExchange, mcorten87\rabbitmq_api\jobs\JobBindingListQueue, mcorten87\rabbitmq_api\j...bBindingListVirtualHost, mcorten87\rabbitmq_api\j...BindingToExchangeCreate, mcorten87\rabbitmq_api\j...BindingToExchangeDelete, mcorten87\rabbitmq_api\j...JobBindingToQueueCreate, mcorten87\rabbitmq_api\j...JobBindingToQueueDelete, mcorten87\rabbitmq_api\j...nnectionListVirtualHost, mcorten87\rabbitmq_api\j...finitionListVirtualHost, mcorten87\rabbitmq_api\jobs\JobExchangeCreate, mcorten87\rabbitmq_api\jobs\JobExchangeDelete, mcorten87\rabbitmq_api\jobs\JobExchangeList, mcorten87\rabbitmq_api\j...ExchangeListVirtualHost, mcorten87\rabbitmq_api\jobs\JobExchangePublish, mcorten87\rabbitmq_api\jobs\JobPermissionCreate, mcorten87\rabbitmq_api\jobs\JobPermissionDelete, mcorten87\rabbitmq_api\j...rmissionListVirtualHost, mcorten87\rabbitmq_api\jobs\JobQueueCreate, mcorten87\rabbitmq_api\jobs\JobQueueDelete, mcorten87\rabbitmq_api\jobs\JobQueueList, mcorten87\rabbitmq_api\j...JobQueueListVirtualHost, mcorten87\rabbitmq_api\jobs\JobVirtualHostCreate, mcorten87\rabbitmq_api\jobs\JobVirtualHostDelete, mcorten87\rabbitmq_api\jobs\JobVirtualHostList. 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...
31 1
        $url .= '/connections';
32 1
        return new Url($url);
33
    }
34
}
35