ZeroMqTest::emitsToListeners()   B
last analyzed

Complexity

Conditions 7
Paths 16

Size

Total Lines 52
Code Lines 24

Duplication

Lines 30
Ratio 57.69 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 30
loc 52
rs 7.2397
cc 7
eloc 24
nc 16
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AsyncPHP\Remit\Tests;
4
5
use AsyncPHP\Remit\Client\ZeroMqClient;
6
use AsyncPHP\Remit\Location\InMemoryLocation;
7
use AsyncPHP\Remit\Server\ZeroMqServer;
8
9
/**
10
 * @covers AsyncPHP\Remit\Client\ZeroMqClient
11
 * @covers AsyncPHP\Remit\Server\ZeroMqServer
12
 */
13
class ZeroMqTest extends Test
14
{
15
    /**
16
     * @var ZeroMqServer
17
     */
18
    protected $server;
19
    /**
20
     * @var ZeroMqClient
21
     */
22
    protected $client;
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function setUp()
28
    {
29
        parent::setUp();
30
31
        $location = new InMemoryLocation("127.0.0.1", 5555);
32
33
        $this->server = new ZeroMqServer($location);
34
        $this->client = new ZeroMqClient($location);
35
    }
36
37
    /**
38
     * @test
39
     */
40
    public function emitsToListeners()
41
    {
42
        $store = 0;
43
44
        $listener = function ($add) use (&$store) {
45
            $store += $add;
46
        };
47
48
        $this->server->addListener("foo", $listener);
49
50
        $this->client->emit("foo", array(1));
51
52
        $ticks = 0;
53
54 View Code Duplication
        while (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
55
            $this->server->tick();
56
57
            // the listener should have incremented this
58
59
            if ($store === 1) {
60
                break;
61
            }
62
63
            if (++$ticks >= 3) {
64
                $this->fail();
65
            }
66
67
            sleep(1);
68
        }
69
70
        $this->server->removeListener("foo", $listener);
71
72
        $this->client->emit("foo", array(1));
73
74
        $ticks = 0;
75
76 View Code Duplication
        while (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
77
            $this->server->tick();
78
79
            // the listener should not have incremented this, because it's gone!
80
81
            if ($store === 1) {
82
                break;
83
            }
84
85
            if (++$ticks >= 3) {
86
                $this->fail();
87
            }
88
89
            sleep(1);
90
        }
91
    }
92
93
    /**
94
     * @test
95
     */
96
    public function canBeSerialized()
97
    {
98
        $this->assertEquals($this->server, unserialize(serialize($this->server)));
99
        $this->assertEquals($this->client, unserialize(serialize($this->client)));
100
    }
101
}
102