Completed
Push — master ( ae2c73...fead31 )
by Clinton
01:46
created

RedisQ::matchesFilter()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace RedisQ;
4
5
class RedisQ
6
{
7
    public function queueObject($object)
8
    {
9
        global $redis;
10
11
        $wrapped = serialize($object);
12
13
        $allQueues = new RedisTtlSortedSet('redisQ:allQueues');
14
        $objectQueues = new RedisTtlSortedSet('objectQueues');
15
        $queues = $allQueues->getMembers();
16
17
        $multi = $redis->multi();
18
19
        // Store an instance of the object
20
        $objectID = 'redisQ:objectID:'.uniqID().md5($wrapped);
21
        $objectQueues->add(time(), $objectID);
22
        $multi->setex($objectID, 9600, $wrapped);
23
24
        // Add objectID to all queues
25
        foreach ($queues as $queueID) {
26
            $multi->lPush($queueID, $objectID);
27
            $multi->expire($queueID, 9600);
28
        }
29
        $multi->exec();
30
    }
31
32
    public function registerListener($queueID)
33
    {
34
        $allQueues = new RedisTtlSortedSet('redisQ:allQueues');
35
        $allQueues->add(time(), $queueID);
36
    }
37
38
    public function listen($queueID, $timeToWait = 10, $filterValue = null)
39
    {
40
        global $redis;
41
42
        $timeToWait = max(1, min(10, $timeToWait));
43
44
        $rQueueID = "redisQ:queueID:$queueID";
45
        $wQueueID = "$rQueueID:w";
46
        if ($redis->set($wQueueID, true, Array('nx', 'ex'=>15)) === false) {
47
            header('HTTP/1.1 429 Too many requests.');
48
            exit();
49
        }
50
51
        self::registerListener($rQueueID);
52
53
        try {
54
            $time = time();
55
            do {
56
                $object = false;
57
                $pop = $redis->blPop($rQueueID, 1);
58
                if (!isset($pop[1])) {
59
                    if (time() >= ($time + $timeToWait)) return;
60
                } else {
61
                    $objectID = $pop[1];
62
                    $object = $redis->get($objectID);
63
                    $object = unserialize($object);
64
                    $object = self::matchesFilter($filterValue, $object);
65
                }
66
            } while ($object === false);
67
        } finally {
68
            $redis->del($wQueueID);
69
        }
70
71
        return $object;
0 ignored issues
show
Bug introduced by
The variable $object 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...
72
    }
73
74
    protected function matchesFilter($filterValue, $object)
75
    {
76
        if ($filterValue == null) return $object;
77
        return self::recursive_array_search($filterValue, unserialize($object)) ? $object : false;
78
    }
79
80
    protected function recursive_array_search($needle, $haystack) {
81
        foreach($haystack as $key=>$value) {
82
            $current_key = $key;
0 ignored issues
show
Unused Code introduced by
$current_key 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...
83
            if ($needle === $value || (is_array($value) && self::recursive_array_search($needle, $value) !== false)) {
84
                return true;
85
            } else if ($needle === $value) {
86
                return true;
87
            }
88
        }
89
        return false;
90
    }
91
}
92