Passed
Push — master ( 9f5262...c72dd6 )
by Gaetano
09:22
created

MyLogger::debugMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
require_once __DIR__ . "/_prepend.php";
3
4
/**
5
 * Demoing how to inject a custom logger for use by the library
6
 */
7
8
use PhpXmlRpc\Client;
9
use PhpXmlRpc\Encoder;
10
use PhpXmlRpc\Helper\Charset;
11
use PhpXmlRpc\Helper\Http;
12
use PhpXmlRpc\Helper\XMLParser;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XMLParser. Consider defining an alias.

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...
13
use PhpXmlRpc\Request;
14
15
// Definition of a custom logger implementing the same API as the default one
16
17
class MyLogger
18
{
19
    protected $debugBuffer = '';
20
    protected $errorBuffer = '';
21
22
    // logger API
23
    public function debug($message, $context = array())
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

23
    public function debug($message, /** @scrutinizer ignore-unused */ $context = array())

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

Loading history...
24
    {
25
        $this->debugBuffer .= $message . "\n";
26
    }
27
28
    // logger API
29
    public function error($message, $context = array())
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

29
    public function error($message, /** @scrutinizer ignore-unused */ $context = array())

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

Loading history...
30
    {
31
        $this->errorBuffer .= $message . "\n";
32
    }
33
34
    public function getDebug()
35
    {
36
        return $this->debugBuffer;
37
    }
38
39
    public function getError()
40
    {
41
        return $this->errorBuffer;
42
    }
43
}
44
45
// create the custom logger instance
46
47
$logger = new MyLogger();
48
49
// inject it into all the classes (possibly) involved
50
51
Charset::setLogger($logger);
52
Client::setLogger($logger);
53
Encoder::setLogger($logger);
54
Http::setLogger($logger);
55
Request::setLogger($logger);
56
XMLParser::setLogger($logger);
57
58
// then send a request
59
60
$input = array(
61
    array('name' => 'Dave', 'age' => 24),
62
    array('name' => 'Edd',  'age' => 45),
63
    array('name' => 'Joe',  'age' => 37),
64
    array('name' => 'Fred', 'age' => 27),
65
);
66
67
$encoder = new Encoder();
68
$client = new Client(XMLRPCSERVER);
69
70
// set maximum debug level, to have all the communication details logged
71
$client->setDebug(2);
72
73
// avid compressed responses, as they mess up the output if echoed on the command-line
74
$client->setAcceptedCompression('');
75
76
// send request
77
output("Sending the request. No output debug should appear below...<br>");
78
$request = new Request('examples.sortByAge', array($encoder->encode($input)));
79
$response = $client->send($request);
80
output("Response received.<br>");
81
82
output("The client error info is:<pre>\n" . $logger->getError() . "\n</pre>");
83
output("The client debug info is:<pre>\n" . $logger->getDebug() . "\n</pre>");
84