ArrayIterator::offsetExists()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Result;
13
14
class ArrayIterator extends AbstractResultsIterator implements \ArrayAccess
15
{
16
    public function offsetExists($offset)
17
    {
18
        return $this->documentExists($offset);
19
    }
20
21
    public function offsetGet($offset)
22
    {
23
        return $this->getDocument($offset);
24
    }
25
26
    public function offsetSet($offset, $value)
27
    {
28
        $this->documents[$offset] = $value;
29
    }
30
31
    public function offsetUnset($offset)
32
    {
33
        unset($this->documents[$offset]);
34
    }
35
36
    protected function convertDocument(array $raw)
37
    {
38
        if (array_key_exists('_source', $raw)) {
39
            $doc = $raw['_source'];
40
        } elseif (array_key_exists('fields', $raw)) {
41
            $doc = array_map('reset', $raw['fields']);
42
        }
43
44
        $doc['_id'] = $raw['_id'];
0 ignored issues
show
Bug introduced by
The variable $doc 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...
45
46
        return $doc;
47
    }
48
}
49