Completed
Push — master ( 5b2ca7...bfb059 )
by Hong
01:49
created

UniquePriorityQueue::getUnique()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 7
nop 1
1
<?php
2
3
/**
4
 * Phoole (PHP7.2+)
5
 *
6
 * @category  Library
7
 * @package   Phoole\Base
8
 * @copyright Copyright (c) 2019 Hong Zhang
9
 */
10
declare(strict_types=1);
11
12
namespace Phoole\Base\Queue;
13
14
/**
15
 * UniquePriorityQueue
16
 *
17
 * Make sure items in the queue are unique
18
 *
19
 * @package Phoole\Base
20
 */
21
class UniquePriorityQueue extends PriorityQueue
22
{
23
    /**
24
     * {@inheritDoc}
25
     */
26
    public function count()
27
    {
28
        return count($this->getUnique(array_column($this->queue, 'data')));
29
    }
30
31
    /**
32
     * {@inheritDoc}
33
     */
34
    public function getIterator()
35
    {
36
        $this->sortQueue();
37
        return new \ArrayIterator(
38
            $this->getUnique(array_column($this->queue, 'data'))
39
        );
40
    }
41
42
    /**
43
     * Remove duplicated items
44
     *
45
     * @param  array $input
46
     * @return array
47
     */
48
    protected function getUnique(array $input): array
49
    {
50
        $result = [];
51
        foreach ($input as $k => $val) {
52
            if (is_object($val)) {
53
                $key = \spl_object_hash($val);
54
            } elseif (is_scalar($val)) {
55
                $key = (string) $val;
56
            }
57
            if (!isset($result[$key])) {
58
                $result[$key] = $val;
0 ignored issues
show
Bug introduced by
The variable $key 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...
59
            }
60
        }
61
        return \array_values($result);
62
    }
63
}
64