Completed
Pull Request — master (#34)
by Clément
20:44 queued 16:37
created

CommandLockHandlerTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 31.75 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 1
cbo 3
dl 20
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testInstance() 0 4 1
A testIsLocked() 0 12 1
A testLock() 10 10 1
A testRelease() 10 10 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
4
namespace Itkg\DelayEventBundle\Tests\Handler;
5
6
use Itkg\DelayEventBundle\DomainManager\LockManagerInterface;
7
use Itkg\DelayEventBundle\Handler\CommandLockHandler;
8
use Phake;
9
use Phake_IMock;
10
11
12
/**
13
 * Class CommandLockHandlerTest
14
 */
15
class CommandLockHandlerTest extends \PHPUnit_Framework_TestCase
16
{
17
    /**
18
     * @var CommandLockHandler
19
     */
20
    private $handler;
21
22
    /**
23
     * @var LockManagerInterface|Phake_IMock
24
     */
25
    private $manager;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
34
        $this->manager = Phake::mock('Itkg\DelayEventBundle\DomainManager\LockManagerInterface');
35
        $this->handler = new CommandLockHandler($this->manager);
0 ignored issues
show
Documentation introduced by
$this->manager is of type object<Phake_IMock>, but the function expects a object<Itkg\DelayEventBu...r\LockManagerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
    }
37
38
    public function testInstance()
39
    {
40
        $this->assertInstanceOf('Itkg\DelayEventBundle\Handler\LockHandlerInterface', $this->handler);
41
    }
42
43
    public function testIsLocked()
44
    {
45
        $channel = 'foo';
46
        $lock = Phake::mock('Itkg\DelayEventBundle\Model\Lock');
47
        Phake::when($this->manager)->getLock($channel)->thenReturn($lock);
0 ignored issues
show
Bug introduced by
It seems like $this->manager can also be of type object<Itkg\DelayEventBu...r\LockManagerInterface>; however, Phake::when() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
48
49
        Phake::when($lock)->isCommandLocked()->thenReturn(false);
50
        $this->assertFalse($this->handler->isLocked($channel));
51
52
        Phake::when($lock)->isCommandLocked()->thenReturn(true);
53
        $this->assertTrue($this->handler->isLocked($channel));
54
    }
55
56 View Code Duplication
    public function testLock()
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...
57
    {
58
        $channel = 'foo';
59
        $lock = Phake::mock('Itkg\DelayEventBundle\Model\Lock');
60
        Phake::when($this->manager)->getLock($channel)->thenReturn($lock);
0 ignored issues
show
Bug introduced by
It seems like $this->manager can also be of type object<Itkg\DelayEventBu...r\LockManagerInterface>; however, Phake::when() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
62
        $this->handler->lock($channel);
63
        Phake::verify($lock)->setCommandLocked(true);
64
        Phake::verify($this->manager)->save($lock);
0 ignored issues
show
Bug introduced by
It seems like $this->manager can also be of type object<Itkg\DelayEventBu...r\LockManagerInterface>; however, Phake::verify() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
65
    }
66
67 View Code Duplication
    public function testRelease()
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...
68
    {
69
        $channel = 'foo';
70
        $lock = Phake::mock('Itkg\DelayEventBundle\Model\Lock');
71
        Phake::when($this->manager)->getLock($channel)->thenReturn($lock);
0 ignored issues
show
Bug introduced by
It seems like $this->manager can also be of type object<Itkg\DelayEventBu...r\LockManagerInterface>; however, Phake::when() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
72
73
        $this->handler->release($channel);
74
        Phake::verify($lock)->setCommandLocked(false);
75
        Phake::verify($this->manager)->save($lock);
0 ignored issues
show
Bug introduced by
It seems like $this->manager can also be of type object<Itkg\DelayEventBu...r\LockManagerInterface>; however, Phake::verify() does only seem to accept object<Phake_IMock>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
76
    }
77
}
78