Completed
Push — master ( 7571fa...8f9bf0 )
by Marc
03:08
created

TraceableNetwork::trace()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.096

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 3
cts 13
cp 0.2308
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 1
nop 0
crap 7.096
1
<?php
2
/*
3
 * This file is part of the phpflo/phpflo-flowtrace package.
4
 *
5
 * (c) Marc Aschmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace PhpFlo;
12
13
use PhpFlo\Common\AbstractNetworkAdapter;
14
use PhpFlo\Common\HookableNetworkInterface;
15
use PhpFlo\Common\Network;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PhpFlo\Network.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use PhpFlo\Common\NetworkAdapterinterface;
17
use PhpFlo\Common\NetworkInterface;
18
use PhpFlo\Exception\FlowException;
19
use PhpFlo\Exception\InvalidDefinitionException;
20
use PhpFlo\Exception\InvalidTypeException;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Network for tracing events.
25
 *
26
 * @package PhpFlo
27
 * @author Marc Aschmann <[email protected]>
28
 */
29
class TraceableNetwork extends AbstractNetworkAdapter implements NetworkAdapterinterface
30
{
31
    private $logger;
32
33 2
    public function __construct(NetworkInterface $network, LoggerInterface $logger)
34
    {
35 2
        parent::__construct($network);
36
37 2
        $this->logger = $logger;
38 2
        $this->network->hook('data', 'flowtrace', $this->trace());
39 2
    }
40
41
    /**
42
     * Wrap the creation of the callback
43
     *
44
     * @return \Closure
45
     */
46
    private function trace()
47
    {
48 2
        $trace = function ($data, $socket) {
49
            if (!is_string($data)) {
50
                $data = serialize($data);
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
            }
52
53
            $to = $socket->to();
54
            $message = "-> {$to['port']} {$to['process']}";
55
56
            $from = $socket->from();
57
            if (isset($from['process'])) {
58
                $message = "{$from['process']} {$from['port']} " . $message;
59
            }
60
61
            $this->logger->debug($message);
62 2
        };
63
64 2
        return $trace->bindTo($this);
65
    }
66
}
67