Delete   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 43
ccs 14
cts 14
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A delete() 0 16 3
1
<?php
2
3
namespace BestServedCold\LaravelZendSearch\Lucene\Store;
4
5
use BestServedCold\LaravelZendSearch\Lucene\Index;
6
use BestServedCold\LaravelZendSearch\Lucene\Search;
7
8
/**
9
 * Class Delete
10
 *
11
 * @package BestServedCold\LaravelZendSearch\Lucene\Store
12
 */
13
class Delete
14
{
15
    /**
16
     * @var Search
17
     */
18
    private $search;
19
20
    /**
21
     * Delete constructor.
22
     *
23
     * @param Search $search
24
     */
25 21
    public function __construct(Search $search)
26
    {
27 21
        $this->search = $search;
28 21
        $this->search->path(config('search.index.path'));
29 21
    }
30
31
    /**
32
     * Delete
33
     *
34
     * @param  Index          $index
35
     * @param  integer $id
36
     * @param  string|boolean $uid
37
     * @return $this
38
     */
39 1
    public function delete(Index $index, $id, $uid = false)
40
    {
41 1
        $this->search->match((int) $id, 'xref_id');
42
43 1
        if ($uid) {
44 1
            $this->search->where(base64_encode($uid), 'uid');
45 1
        }
46
47 1
        foreach ($this->search->hits(false) as $hit) {
0 ignored issues
show
Bug introduced by
The expression $this->search->hits(false) of type array|object<ZendSearch\Lucene\Search\QueryHit> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
48 1
            $index->get()->delete($hit);
49 1
        }
50
51 1
        $index->get()->commit();
52
        
53 1
        return $this;
54
    }
55
}
56