Completed
Push — master ( f62d1a...5fc0c8 )
by Gaetano
9s
created

discuss.php ➔ getComments()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 30
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 20
nc 6
nop 1
dl 0
loc 30
rs 8.439
c 2
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
include_once __DIR__ . "/../../vendor/autoload.php";
4
5
use PhpXmlRpc\Value;
6
7
$addComment_sig = array(array(Value::$xmlrpcInt, Value::$xmlrpcString, Value::$xmlrpcString, Value::$xmlrpcString));
8
9
$addComment_doc = 'Adds a comment to an item. The first parameter
10
is the item ID, the second the name of the commenter, and the third
11
is the comment itself. Returns the number of comments against that
12
ID.';
13
14
function addComment($req)
15
{
16
    $err = "";
17
    // since validation has already been carried out for us,
18
    // we know we got exactly 3 string values
19
    $encoder = new PhpXmlRpc\Encoder();
20
    $n = $encoder->decode($req);
21
    $msgID = $n[0];
22
    $name = $n[1];
23
    $comment = $n[2];
24
25
    $dbh = dba_open("/tmp/comments.db", "c", "db2");
26
    if ($dbh) {
27
        $countID = "${msgID}_count";
28
        if (dba_exists($countID, $dbh)) {
29
            $count = dba_fetch($countID, $dbh);
30
        } else {
31
            $count = 0;
32
        }
33
        // add the new comment in
34
        dba_insert($msgID . "_comment_${count}", $comment, $dbh);
35
        dba_insert($msgID . "_name_${count}", $name, $dbh);
36
        $count++;
37
        dba_replace($countID, $count, $dbh);
38
        dba_close($dbh);
39
    } else {
40
        $err = "Unable to open comments database.";
41
    }
42
    // if we generated an error, create an error return response
43 View Code Duplication
    if ($err) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
44
        return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
45
    } else {
46
        // otherwise, we create the right response
47
        return new PhpXmlRpc\Response(new PhpXmlRpc\Value($count, "int"));
0 ignored issues
show
Bug introduced by
The variable $count does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
48
    }
49
}
50
51
$getComments_sig = array(array(Value::$xmlrpcArray, Value::$xmlrpcString));
52
53
$getComments_doc = 'Returns an array of comments for a given ID, which
54
is the sole argument. Each array item is a struct containing name
55
and comment text.';
56
57
function getComments($req)
58
{
59
    $err = "";
60
    $ra = array();
61
    $encoder = new PhpXmlRpc\Encoder();
62
    $msgID = $encoder->decode($req->getParam(0));
63
    $dbh = dba_open("/tmp/comments.db", "r", "db2");
64
    if ($dbh) {
65
        $countID = "${msgID}_count";
66
        if (dba_exists($countID, $dbh)) {
67
            $count = dba_fetch($countID, $dbh);
68
            for ($i = 0; $i < $count; $i++) {
69
                $name = dba_fetch("${msgID}_name_${i}", $dbh);
70
                $comment = dba_fetch("${msgID}_comment_${i}", $dbh);
71
                // push a new struct onto the return array
72
                $ra[] = array(
73
                    "name" => $name,
74
                    "comment" => $comment,
75
                );
76
            }
77
        }
78
    }
79
    // if we generated an error, create an error return response
80
    if ($err) {
81
        return new PhpXmlRpc\Response(0, PhpXmlRpc\PhpXmlRpc::$xmlrpcerruser, $err);
82
    } else {
83
        // otherwise, we create the right response
84
        return new PhpXmlRpc\Response($encoder->encode($ra));
85
    }
86
}
87
88
$srv = new PhpXmlRpc\Server(array(
89
    "discuss.addComment" => array(
90
        "function" => "addComment",
91
        "signature" => $addComment_sig,
92
        "docstring" => $addComment_doc,
93
    ),
94
    "discuss.getComments" => array(
95
        "function" => "getComments",
96
        "signature" => $getComments_sig,
97
        "docstring" => $getComments_doc,
98
    ),
99
));
100