ExampleICMPRequest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 21 1
A run() 0 18 1
1
<?php
2
namespace PHPDaemon\Examples;
3
4
use PHPDaemon\HTTPRequest\Generic;
5
6
class ExampleICMPRequest extends Generic
7
{
8
/**
9
 * Constructor.
10
 * @return void
11
 */
12
public function init()
13
{
14
    $req = $this;
15
16
    $job = $this->job = new \PHPDaemon\Core\ComplexJob(function () use ($req) { // called when job is done
0 ignored issues
show
Documentation introduced by
The property job does not exist on object<PHPDaemon\Examples\ExampleICMPRequest>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
17
18
        $req->wakeup(); // wake up the request immediately
19
20
    });
21
22
    $job('pingjob', function ($name, $job) use ($req) { // registering job named 'pingjob'
23
24
        \PHPDaemon\Clients\ICMP\Pool::getInstance()->sendPing('8.8.8.8', function ($latency) use ($name, $job) {
25
            $job->setResult($name, $latency);
26
        });
27
    });
28
29
    $job(); // let the fun begin
30
31
    $this->sleep(5, true); // setting timeout
32
}
33
34
/**
35
 * Called when request iterated.
36
 * @return integer Status.
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
37
 */
38
public function run()
39
{
40
$this->header('Content-Type: text/html');
41
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
42
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
43
<html xmlns="http://www.w3.org/1999/xhtml">
44
<head>
45
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
46
    <title>Ping 8.8.8.8</title>
47
</head>
48
<body>
49
<h1>Latency to 8.8.8.8:</h1>
50
<?php echo round($this->job->getResult('pingjob'), 4) * 1000;
0 ignored issues
show
Documentation introduced by
The property job does not exist on object<PHPDaemon\Examples\ExampleICMPRequest>. 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...
51
?> ms.
52
</body>
53
</html><?php
54
55
}
56
}
57