StandartGitterRoom   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 47
Duplicated Lines 19.15 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 9
loc 47
rs 10
wmc 5
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A listen() 0 7 1
A id() 0 8 2
A driver() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author butschster <[email protected]>
6
 * @date   20.07.2016 15:27
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Interfaces\Gitter;
13
14
use Domains\Room\AbstractRoom;
15
16
class StandartGitterRoom extends AbstractRoom
17
{
18
    /**
19
     * AbstractRoom constructor.
20
     *
21
     * @param string $alias
22
     * @param string|array $groups
23
     * @param array  $middleware
24
     */
25 View Code Duplication
    public function __construct($alias, $groups = '*', array $middleware = [])
0 ignored issues
show
Duplication introduced by
This method 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...
26
    {
27
        parent::__construct();
28
29
        $this->alias = $alias;
30
        $this->groups = (array) $groups;
31
32
        $this->setMiddleware($middleware);
33
    }
34
35
    public function listen()
36
    {
37
        $result = $this->client()->getGitterClient()->http->getRoomByUri($this->alias())->wait();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Domains\Bot\ClientInterface as the method getGitterClient() does only exist in the following implementations of said interface: Interfaces\Gitter\Client.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
38
        $this->id = $result->id;
39
40
        parent::listen();
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function id()
47
    {
48
        if (empty($this->id)) {
49
            return $this->alias();
50
        }
51
52
        return parent::id();
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function driver()
59
    {
60
        return 'gitter';
61
    }
62
}