Reindex   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 71
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 49 3
1
<?php
2
3
namespace Colligator\Console\Commands;
4
5
use Colligator\Document;
6
use Colligator\Search\DocumentsIndex;
7
use Illuminate\Console\Command;
8
9
class Reindex extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'colligator:reindex';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Re-build ElasticSearch index.';
24
25
    /**
26
     * Execute the console command.
27
     *
28
     * @return mixed
29
     */
30
    public function handle(DocumentsIndex $docIndex)
31
    {
32
        $t0 = microtime(true);
33
34
        \Log::info('[ReindexJob] Starting job.');
35
36
        $this->info('');
37
        $this->info(' Rebuilding the Elasticsearch index will take some time.');
38
        $this->info('');
39
40
        //$docIndex->dropVersion();
41
        $oldVersion = $docIndex->getCurrentVersion();
42
        $newVersion = $oldVersion + 1;
43
        $this->comment(' Old version: ' . $oldVersion . ', new version: ' . $newVersion);
44
45
        if ($docIndex->versionExists($newVersion)) {
46
            $this->comment(' New version already existed, probably from a crashed job. Removing.');
47
            $docIndex->dropVersion($newVersion);
48
        }
49
50
        $this->comment(' Creating new index');
51
        $docIndex->createVersion($newVersion);
52
53
        $this->comment(sprintf(' [%03d] Building entity usage cache', microtime(true) - $t0));
54
        $docIndex->buildCompleteUsageCache();
55
56
        $this->comment(sprintf(' [%03d] Filling new index...', microtime(true) - $t0));
57
58
        $docCount = Document::count();
59
        $this->output->progressStart($docCount);
60
61
        Document::with('subjects', 'genres', 'cover')->chunk(1000, function ($docs) use ($docIndex, $newVersion) {
0 ignored issues
show
Bug introduced by
The method chunk does only exist in Illuminate\Database\Eloquent\Builder, but not in Illuminate\Database\Eloquent\Model.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
62
            foreach ($docs as $doc) {
63
                $docIndex->index($doc, $newVersion);
64
                $this->output->progressAdvance();
65
            }
66
        });
67
        $this->output->progressFinish();
68
69
        $this->comment(sprintf(' [%03d] Swapping indices', microtime(true) - $t0));
70
        $docIndex->activateVersion($newVersion);
71
72
        $this->comment(sprintf(' [%03d] Dropping old index', microtime(true) - $t0));
73
        $docIndex->dropVersion($oldVersion);
74
75
        $dt = microtime(true) - $t0;
76
        $this->info(' Completed in ' . round($dt) . ' seconds.');
77
        \Log::info('[ReindexJob] Completed in ' . round($dt) . ' seconds.');
78
    }
79
}
80