WorkerConnection::sendPacket()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
nc 2
nop 1
1
<?php
2
namespace PHPDaemon\IPCManager;
3
4
use PHPDaemon\Core\Daemon;
5
use PHPDaemon\Core\Timer;
6
use PHPDaemon\Network\Connection;
7
8
class WorkerConnection extends Connection
9
{
10
    /** @var null */
11
    protected $timeout = null;
12
    /** @var int */
13
    protected $lowMark = 4; // initial value of the minimal amout of bytes in buffer
14
    /** @var int */
15
    protected $highMark = 0xFFFF; // initial value of the maximum amout of bytes in buffer
16
    /**
17
     * @TODO DESCR
18
     */
19
    const STATE_CONTENT = 1;
20
    /** @var */
21
    protected $packetLength;
22
23
    /**
24
     * @TODO DESCR
25
     */
26
    public function onReady()
27
    {
28
        $this->sendPacket([
29
            'op' => 'start',
30
            'pid' => Daemon::$process->getPid(),
31
            'workerId' => Daemon::$process->getId()
32
        ]);
33
        parent::onReady();
34
    }
35
36
    /**
37
     * @param $p
38
     */
39
    protected function onPacket($p)
40
    {
41
        if ($p['op'] === 'spawnInstance') {
42
            $fullname = $p['appfullname'];
43
            $fullname = str_replace('-', ':', $fullname);
44
            if (mb_orig_strpos($fullname, ':') === false) {
45
                $fullname .= ':';
46
            }
47
            list($app, $name) = explode(':', $fullname, 2);
48
            Daemon::$appResolver->getInstance($app, $name, true, true);
49
        } elseif ($p['op'] === 'importFile') {
50
            if (!Daemon::$config->autoreimport->value) {
51
                Daemon::$process->gracefulRestart();
0 ignored issues
show
Bug introduced by
The method gracefulRestart does only exist in PHPDaemon\Thread\Worker, but not in PHPDaemon\Thread\IPC and PHPDaemon\Thread\Master.

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...
52
                return;
53
            }
54
            $path = $p['path'];
55
            Timer::add(function ($event) use ($path) {
56
                if (Daemon::supported(Daemon::SUPPORT_RUNKIT_IMPORT)) {
57
                    //Daemon::log('--start runkit_import('.$path.')');
58
                    runkit_import($path, RUNKIT_IMPORT_FUNCTIONS | RUNKIT_IMPORT_CLASSES | RUNKIT_IMPORT_OVERRIDE);
59
                    //Daemon::log('--end runkit_import('.$path.')');
60
                } else {
61
                    $this->appInstance->log('Cannot import \'' . $path . '\': runkit_import is not callable.');
0 ignored issues
show
Documentation introduced by
The property appInstance does not exist on object<PHPDaemon\IPCManager\WorkerConnection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
                }
63
64
                $event->finish();
65
            }, 5);
66
        } elseif ($p['op'] === 'call') {
67
            if (mb_orig_strpos($p['appfullname'], ':') === false) {
68
                $p['appfullname'] .= ':';
69
            }
70
            list($app, $name) = explode(':', $p['appfullname'], 2);
71
            if ($app = Daemon::$appResolver->getInstance($app, $name)) {
72
                $app->RPCall($p['method'], $p['args']);
73
            }
74
        }
75
    }
76
77
    /**
78
     * @TODO DESCR
79
     * @param $p
80
     */
81
    public function sendPacket($p)
82
    {
83
        if ($p === null) {
84
            return;
85
        }
86
        $data = \igbinary_serialize($p);
87
        $this->write(pack('N', mb_orig_strlen($data)) . $data);
88
    }
89
90
    /**
91
     * @TODO DESCR
92
     * Called when new data received.
93
     * @return void
94
     */
95 View Code Duplication
    public function onRead()
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...
96
    {
97
        start:
98
        if ($this->state === self::STATE_ROOT) {
99
            if (false === ($r = $this->readExact(4))) {
100
                return; // not ready yet
101
            }
102
            $u = unpack('N', $r);
103
            $this->packetLength = $u[1];
104
            $this->state = self::STATE_CONTENT;
105
        }
106
        if ($this->state === self::STATE_CONTENT) {
107
            if (false === ($packet = $this->readExact($this->packetLength))) {
108
                $this->setWatermark($this->packetLength, $this->packetLength);
109
                return; // not ready yet
110
            }
111
            $this->setWatermark(4, 0xFFFF);
112
            $this->state = self::STATE_ROOT;
113
            $this->onPacket(\igbinary_unserialize($packet));
114
        }
115
        goto start;
116
    }
117
}
118