IPCManager   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 195
Duplicated Lines 8.21 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 16
loc 195
rs 10
c 0
b 0
f 0
wmc 29
lcom 1
cbo 5

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigDefaults() 0 7 1
A init() 0 10 2
A getSocketUrl() 0 4 1
C updatedWorkers() 0 38 13
A onShutdown() 0 7 2
A importFile() 0 9 2
A ensureConnection() 0 4 1
A sendPacket() 0 16 4
A sendBroadcastCall() 8 9 1
A sendSingleCall() 8 9 1
A sendDirectCall() 0 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
namespace PHPDaemon\IPCManager;
3
4
use PHPDaemon\Config;
5
use PHPDaemon\Core\AppInstance;
6
use PHPDaemon\Core\Daemon;
7
use PHPDaemon\Thread;
8
9
class IPCManager extends AppInstance
10
{
11
    /** @var */
12
    public $pool;
13
    /** @var */
14
    public $conn;
15
    /** @var */
16
    public $socketurl;
17
18
    /**
19
     * Setting default config options
20
     * Overriden from AppInstance::getConfigDefaults
21
     * @return array|bool
22
     */
23
    protected function getConfigDefaults()
24
    {
25
        return [
26
            // listen to
27
            'mastersocket' => 'unix:///tmp/phpDaemon-ipc-%x.sock',
28
        ];
29
    }
30
31
    /**
32
     * Constructor.
33
     * @return void
34
     */
35
    public function init()
36
    {
37
        $this->socketurl = sprintf($this->config->mastersocket->value,
38
            crc32(Daemon::$config->pidfile->value . "\x00" . Daemon::$config->user->value . "\x00" . Daemon::$config->group->value));
39
        if (Daemon::$process instanceof Thread\IPC) {
40
            $this->pool = MasterPool::getInstance(['listen' => $this->socketurl]);
0 ignored issues
show
Documentation introduced by
array('listen' => $this->socketurl) is of type array<string,string,{"listen":"string"}>, but the function expects a string.

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...
41
            $this->pool->appInstance = $this;
42
            $this->pool->onReady();
43
        }
44
    }
45
46
    public function getSocketUrl()
47
    {
48
        return $this->socketurl;
49
    }
50
51
    /**
52
     * @TODO DESCR
53
     */
54
    public function updatedWorkers()
55
    {
56
        $perWorker = 1;
57
        $instancesCount = [];
58
        foreach (Daemon::$config as $name => $section) {
0 ignored issues
show
Bug introduced by
The expression \PHPDaemon\Core\Daemon::$config of type object<PHPDaemon\Config\_Object> is not traversable.
Loading history...
59
            if ((!$section instanceof Config\Section) || !isset($section->limitinstances)) {
60
                continue;
61
            }
62
            $instancesCount[$name] = 0;
63
        }
64
        foreach ($this->pool->workers as $worker) {
65
            foreach ($worker->instancesCount as $k => $v) {
66
                if (!isset($instancesCount[$k])) {
67
                    unset($worker->instancesCount[$k]);
68
                    continue;
69
                }
70
                $instancesCount[$k] += $v;
71
            }
72
        }
73
        foreach ($instancesCount as $name => $num) {
74
            $v = Daemon::$config->{$name}->limitinstances->value - $num;
75
            foreach ($this->pool->workers as $worker) {
76
                if ($v <= 0) {
77
                    break;
78
                }
79
                if ((isset($worker->instancesCount[$name])) && ($worker->instancesCount[$name] < $perWorker)) {
80
                    continue;
81
                }
82
                if (!isset($worker->instancesCount[$name])) {
83
                    $worker->instancesCount[$name] = 1;
84
                } else {
85
                    ++$worker->instancesCount[$name];
86
                }
87
                $worker->sendPacket(['op' => 'spawnInstance', 'appfullname' => $name]);
88
                --$v;
89
            }
90
        }
91
    }
92
93
    /**
94
     * Called when application instance is going to shutdown.
95
     * @param bool $graceful
96
     * @return boolean Ready to shutdown?
97
     */
98
    public function onShutdown($graceful = false)
99
    {
100
        if ($this->pool) {
101
            return $this->pool->onShutdown();
102
        }
103
        return true;
104
    }
105
106
    /**
107
     * @TODO DESCR
108
     * @param $workerId
109
     * @param $path
110
     * @return bool
111
     */
112
    public function importFile($workerId, $path)
113
    {
114
        if (!isset($this->pool->workers[$workerId])) {
115
            return false;
116
        }
117
        $worker = $this->pool->workers[$workerId];
118
        $worker->sendPacket(['op' => 'importFile', 'path' => $path]);
119
        return true;
120
    }
121
122
    /**
123
     * @TODO DESCR
124
     */
125
    public function ensureConnection()
126
    {
127
        $this->sendPacket('');
128
    }
129
130
    /**
131
     * @TODO DESCR
132
     * @param $packet
133
     */
134
    public function sendPacket($packet = null)
135
    {
136
        if ($this->conn && $this->conn->isConnected()) {
137
            $this->conn->sendPacket($packet);
138
            return;
139
        }
140
141
        $cb = function ($conn) use ($packet) {
142
            $conn->sendPacket($packet);
143
        };
144
        if (!$this->conn) {
145
            $this->conn = new WorkerConnection(null, null, null);
0 ignored issues
show
Unused Code introduced by
The call to WorkerConnection::__construct() has too many arguments starting with null.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
            $this->conn->connect($this->socketurl);
147
        }
148
        $this->conn->onConnected($cb);
149
    }
150
151
    /**
152
     * @TODO DESCR
153
     * @param $appInstance
154
     * @param $method
155
     * @param array $args
156
     * @param callable $cb
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
157
     */
158 View Code Duplication
    public function sendBroadcastCall($appInstance, $method, $args = [], $cb = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
159
    {
160
        $this->sendPacket([
161
            'op' => 'broadcastCall',
162
            'appfullname' => $appInstance,
163
            'method' => $method,
164
            'args' => $args,
165
        ]);
166
    }
167
168
    /**
169
     * @TODO DESCR
170
     * @param $appInstance
171
     * @param $method
172
     * @param array $args
173
     * @param callable $cb
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
174
     */
175 View Code Duplication
    public function sendSingleCall($appInstance, $method, $args = [], $cb = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
176
    {
177
        $this->sendPacket([
178
            'op' => 'singleCall',
179
            'appfullname' => $appInstance,
180
            'method' => $method,
181
            'args' => $args,
182
        ]);
183
    }
184
185
    /**
186
     * @TODO DESCR
187
     * @param $workerId
188
     * @param $appInstance
189
     * @param $method
190
     * @param array $args
191
     * @param callable $cb
0 ignored issues
show
Documentation introduced by
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
192
     */
193
    public function sendDirectCall($workerId, $appInstance, $method, $args = [], $cb = null)
0 ignored issues
show
Unused Code introduced by
The parameter $cb is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
    {
195
        $this->sendPacket([
196
            'op' => 'directCall',
197
            'appfullname' => $appInstance,
198
            'method' => $method,
199
            'args' => $args,
200
            'workerId' => $workerId,
201
        ]);
202
    }
203
}
204