Completed
Push — master ( 584ae4...7571fa )
by Marc
03:06
created

TraceableNetwork::trace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.064

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 3
cts 5
cp 0.6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1.064
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
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
49
if src.process
50
    return "#{src.process} #{src.port.toUpperCase()} -> #{tgt.port.toUpperCase()} #{tgt.node}"
51
  else
52
    return "-> #{tgt.port.toUpperCase()} #{tgt.node}"
53
         */
54
55 2
        $trace = function ($data, $socket) {
0 ignored issues
show
Unused Code introduced by
The parameter $socket 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...
56
            serialize($data);
57
58
            $this->logger->debug();
0 ignored issues
show
Bug introduced by
The call to debug() misses a required argument $message.

This check looks for function calls that miss required arguments.

Loading history...
59 2
        };
60
61 2
        return $trace->bindTo($this);
62
    }
63
}
64