AMQPTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 21.05 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 8
c 2
b 0
f 2
lcom 1
cbo 1
dl 16
loc 76
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 2
A testProcess() 0 18 2
A testSingleConnection() 0 5 1
A testMultiConnection() 0 5 1
A testGetQueue() 16 16 1
A testGetQueueException() 0 13 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
namespace Aimeos\MW\MQueue;
4
5
6
class AMQPTest extends \PHPUnit_Framework_TestCase
7
{
8
	protected function setUp()
9
	{
10
		if( class_exists( '\PhpAmqpLib\Connection\AMQPStreamConnection' ) === false ) {
11
			$this->markTestSkipped( 'Please install the "php-amqplib" library via composer first' );
12
		}
13
	}
14
15
16
	public function testProcess()
17
	{
18
		try
19
		{
20
			$mqueue = new \Aimeos\MW\MQueue\AMQP( array( 'host' => 'localhost' ) );
21
			$queue = $mqueue->getQueue( 'aimeos_unittest' );
22
		}
23
		catch( \Aimeos\MW\MQueue\Exception $e )
0 ignored issues
show
Bug introduced by
The class Aimeos\MW\MQueue\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
24
		{
25
			$this->markTestSkipped( 'No AMQP compliant server available at "localhost"' );
26
		}
27
28
		$queue->add( 'testmsg' );
0 ignored issues
show
Bug introduced by
The variable $queue does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
29
		$msg = $queue->get();
30
		$queue->del( $msg );
31
32
		$this->assertNull( $queue->get() );
33
	}
34
35
36
	public function testSingleConnection()
37
	{
38
		$this->setExpectedException( '\Aimeos\MW\MQueue\Exception' );
39
		new \Aimeos\MW\MQueue\AMQP( array( 'host' => '192.168.255.255', 'connection_timeout' => 0.1 ) );
40
	}
41
42
43
	public function testMultiConnection()
44
	{
45
		$this->setExpectedException( '\Aimeos\MW\MQueue\Exception' );
46
		new \Aimeos\MW\MQueue\AMQP( array( 'host' => array( '192.168.254.255', '192.168.255.255' ), 'connection_timeout' => 0.1 ) );
47
	}
48
49
50 View Code Duplication
	public function testGetQueue()
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...
51
	{
52
		$channel = $this->getMockBuilder( 'PhpAmqpLib\Channel\AMQPChannel' )
53
			->disableOriginalConstructor()
54
			->getMock();
55
56
		$object = $this->getMockBuilder( '\Aimeos\MW\MQueue\AMQP' )
57
			->setMethods( array( 'getChannel', '__destruct' ) )
58
			->disableOriginalConstructor()
59
			->getMock();
60
61
		$object->expects( $this->once() )->method( 'getChannel' )
62
			->will( $this->returnValue( $channel ) );
63
64
		$this->assertInstanceOf( '\Aimeos\MW\MQueue\Queue\Iface', $object->getQueue( 'test' ) );
65
	}
66
67
68
	public function testGetQueueException()
69
	{
70
		$object = $this->getMockBuilder( '\Aimeos\MW\MQueue\AMQP' )
71
			->setMethods( array( 'getChannel', '__destruct' ) )
72
			->disableOriginalConstructor()
73
			->getMock();
74
75
		$object->expects( $this->once() )->method( 'getChannel' )
76
			->will( $this->throwException( new \Exception() ) );
77
78
		$this->setExpectedException( '\Aimeos\MW\MQueue\Exception' );
79
		$object->getQueue( 'test' );
80
	}
81
}
82