PriorityChannel   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 21.21 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 3 Features 0
Metric Value
wmc 5
c 4
b 3
f 0
lcom 1
cbo 2
dl 7
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A doSend() 7 8 3

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
namespace PEIP\Channel;
4
5
namespace PEIP\Channel;
6
7
/*
8
 * This file is part of the PEIP package.
9
 * (c) 2009-2016 Timo Michna <timomichna/yahoo.de>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
/*
16
 * PriorityChannel
17
 *
18
 * @author Timo Michna <timomichna/yahoo.de>
19
 * @package PEIP
20
 * @subpackage channel
21
 * @extends \PEIP\Channel\QueueChannel
22
 * @implements \PEIP\INF\Event\Connectable, \PEIP\INF\Channel\Channel, \PEIP\INF\Channel\PollableChannel
23
 */
24
25
use SplPriorityQueue;
26
27
class PriorityChannel extends \PEIP\Channel\QueueChannel
28
{
29
    protected $priorityHeader = 'PRIORITY';
30
31
    /**
32
     * @param $capacity
33
     * @param $priorityHeader
34
     *
35
     * @return
36
     */
37
    public function __construct($capacity = -1, $priorityHeader = null)
38
    {
39
        $this->setCapacity((int) $capacity);
40
        $this->queue = new SplPriorityQueue();
41
        if ($priorityHeader) {
42
            $this->priorityHeader = $priorityHeader;
43
        }
44
    }
45
46
    /**
47
     * @param $message
48
     *
49
     * @return
50
     */
51 View Code Duplication
    protected function doSend(\PEIP\INF\Message\Message $message)
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...
52
    {
53
        if ($this->capacity < 1 || $this->getMessageCount() <= $this->getCapacity()) {
54
            $this->queue->insert($message, $message->getHeader($this->priorityHeader));
0 ignored issues
show
Bug introduced by
The method insert does only exist in SplPriorityQueue, but not in SplQueue.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
        } else {
56
            throw new \Exception('Not implemented yet.');
57
        }
58
    }
59
}
60