Completed
Push — non_purge_indexer ( 6bd5a6...31b501 )
by André
13:30
created

ReindexCommand   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 215
Duplicated Lines 3.26 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
dl 7
loc 215
rs 10
c 0
b 0
f 0
wmc 24
lcom 2
cbo 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 10 2
A configure() 0 21 1
B execute() 0 33 5
A executeParallel() 0 14 2
A getStatementContentSince() 0 16 1
A getStatementContentAll() 0 14 1
A fetchIteration() 7 15 4
A getPhpProcess() 0 13 2
B getNumberOfCPUCores() 0 28 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\Command;
10
11
use eZ\Publish\Core\Search\Common\IterativelyIndexer;
12
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
13
use eZ\Publish\Core\Search\Common\Indexer;
14
use Doctrine\DBAL\Driver\Statement;
15
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Process\Process;
20
use Symfony\Component\Process\PhpExecutableFinder;
21
use RuntimeException;
22
use DateTime;
23
use PDO;
24
25
class ReindexCommand extends ContainerAwareCommand
26
{
27
    /**
28
     * @var \eZ\Publish\Core\Search\Common\Indexer
29
     */
30
    private $searchIndexer;
31
32
    /**
33
     * Initialize objects required by {@see execute()}.
34
     *
35
     * @param InputInterface $input
36
     * @param OutputInterface $output
37
     */
38
    public function initialize(InputInterface $input, OutputInterface $output)
39
    {
40
        parent::initialize($input, $output);
41
        $this->searchIndexer = $this->getContainer()->get('ezpublish.spi.search.indexer');
42
        if (!$this->searchIndexer instanceof Indexer) {
43
            throw new RuntimeException(
44
                sprintf('Expected to find Search Engine Indexer but found "%s" instead', get_parent_class($this->searchIndexer))
45
            );
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    protected function configure()
53
    {
54
        $this
55
            ->setName('ezplatform:reindex')
56
            ->setDescription('Recreate or Refresh search engine index')
57
            ->addOption('iteration-count', 'c', InputOption::VALUE_OPTIONAL, 'Number of objects to be indexed in a single iteration, for avoiding using to much memory', 20)
58
            ->addOption('no-commit', null, InputOption::VALUE_NONE, 'Do not commit after each iteration')
59
            ->addOption('no-purge', null, InputOption::VALUE_NONE, 'Do not purge before indexing. BC NOTE: Should this be default as of 2.0?')
60
            ->addOption('since', null, InputOption::VALUE_NONE, 'Index changes since a given time, any format understood by DateTime. Implies "no-purge", can not be combined with "content-ids".')
61
            ->addOption('content-ids', null, InputOption::VALUE_NONE, 'Comma separated list of content id\'s to refresh (deleted or updated/added). Implies "no-purge", can not be combined with "since".')
62
            ->addOption('processes', null, InputOption::VALUE_NONE, 'Number of sub processes to spawn in parallel, by default: (number of cpu cores)-1, disable by setting to "0"', $this->getNumberOfCPUCores() -1)
63
            ->setHelp(
64
                <<<EOT
65
The command <info>%command.name%</info> indexes current configured database in configured search engine index.
66
67
68
TODO: ADD EXAMPLES OF ADVANCE USAGE!
69
70
EOT
71
            );
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77
    protected function execute(InputInterface $input, OutputInterface $output)
78
    {
79
        $noCommit = $input->getOption('no-commit');
80
        $iterationCount = $input->getOption('iteration-count');
81
        if (!is_numeric($iterationCount) || (int) $iterationCount < 1) {
82
            throw new RuntimeException("'--iteration-count' option should be > 0, got '{$iterationCount}'");
83
        }
84
85
86
        if (!$this->searchIndexer instanceof IterativelyIndexer) {
87
            $output->writeln( <<<EOT
88
DEPRECATED:
89
Running indexing against an Indexer that has not been updated to use IterativelyIndexer abstract.
90
91
Options that won't be taken into account:
92
- since
93
- content-ids
94
- processes
95
- no-purge
96
EOT
97
            );
98
            $this->searchIndexer->createSearchIndex($output, (int) $iterationCount, empty($noCommit));
99
100
            return 0;
101
        }
102
103
        // If content-ids are specified we assume we are in a sub process or that it's not needed
104
        if ($contentIds = $input->getOption('content-ids')) {
105
            $this->searchIndexer->updateSearchIndex($contentIds);
106
        }
107
108
        return $this->executeParallel($input, $output, (int) $iterationCount, (bool) $noCommit);
109
    }
110
111
112
    private function executeParallel(InputInterface $input, OutputInterface $output, $iterationCount, $noCommit)
0 ignored issues
show
Unused Code introduced by
The parameter $output is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $iterationCount is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $noCommit is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
113
    {
114
        $since = $input->getOption('since');
115
        $processes = $input->getOption('processes');
0 ignored issues
show
Unused Code introduced by
$processes 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...
116
        $noPurge = $input->getOption('no-purge');
0 ignored issues
show
Unused Code introduced by
$noPurge 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...
117
118
        if ($since) {
119
            $stmt = $this->getStatementContentSince(new DateTime($since));
0 ignored issues
show
Unused Code introduced by
$stmt 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...
120
        } else {
121
            $stmt = $this->getStatementContentAll();
0 ignored issues
show
Unused Code introduced by
$stmt 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...
122
        }
123
124
125
    }
126
127
    /**
128
     * @param DateTime $since
129
     *
130
     * @return \Doctrine\DBAL\Driver\Statement
131
     */
132
    private function getStatementContentSince(DateTime $since)
133
    {
134
        /**
135
         * @var \Doctrine\DBAL\Connection $connection
136
         */
137
        $connection = $this->getContainer()->get('ezpublish.api.storage_engine.legacy.connection');
138
        $q = $connection->createQueryBuilder()
139
            ->select('c.id')
140
            ->from('ezcontentobject', 'c')
141
            ->where('c.status = :status')->andWhere('c.modified >= :since')
142
            ->orderBy('c.modified', true)
143
            ->setParameter('status', ContentInfo::STATUS_PUBLISHED, PDO::PARAM_INT)
144
            ->setParameter('since', $since->getTimestamp(), PDO::PARAM_INT);
145
146
        return $q->execute();
147
    }
148
149
    /**
150
     * @return \Doctrine\DBAL\Driver\Statement
151
     */
152
    private function getStatementContentAll()
153
    {
154
        /**
155
         * @var \Doctrine\DBAL\Connection $connection
156
         */
157
        $connection = $this->getContainer()->get('ezpublish.api.storage_engine.legacy.connection');
158
        $q = $connection->createQueryBuilder()
159
            ->select('c.id')
160
            ->from('ezcontentobject', 'c')
161
            ->where('c.status = :status')
162
            ->setParameter('status', ContentInfo::STATUS_PUBLISHED, PDO::PARAM_INT);
163
164
        return $q->execute();
0 ignored issues
show
Bug Compatibility introduced by
The expression $q->execute(); of type Doctrine\DBAL\Driver\Statement|integer adds the type integer to the return on line 164 which is incompatible with the return type documented by eZ\Bundle\EzPublishCoreB...:getStatementContentAll of type Doctrine\DBAL\Driver\Statement.
Loading history...
165
    }
166
167
    /**
168
     * @param \Doctrine\DBAL\Driver\Statement $stmt
169
     * @param int $iterationCount
170
     *
171
     * @return int[][] Return an array of arrays, each array contains content id's of $iterationCount.
172
     */
173
    private function fetchIteration(Statement $stmt, $iterationCount)
174
    {
175
        do {
176
            $contentIds = [];
177 View Code Duplication
            for ($i = 0; $i < $iterationCount; ++$i) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
                if ($contentId = $stmt->fetch(PDO::FETCH_COLUMN)) {
179
                    $contentIds[] = $contentId;
180
                } else {
181
                    break;
182
                }
183
            }
184
185
            yield $contentIds;
186
        } while ($contentId);
0 ignored issues
show
Bug introduced by
The variable $contentId 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...
187
    }
188
189
    /**
190
     * @param string $consoleDir
191
     *
192
     * @return Process
193
     */
194
    private static function getPhpProcess($consoleDir = 'app')
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
195
    {
196
        $phpFinder = new PhpExecutableFinder();
197
        if (!$phpPath = $phpFinder->find()) {
198
            throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again');
199
        }
200
201
        $cmd = 'ezplatform:reindex';
202
        $php = escapeshellarg($phpFinder);
203
        $console = escapeshellarg($consoleDir.'/console');
204
205
        return new Process($php.' '.$console.' '.$cmd, null, null, null, null);
206
    }
207
208
    /**
209
     * @return int
210
     */
211
    private function getNumberOfCPUCores()
212
    {
213
        $cores = 1;
214
        if (is_file('/proc/cpuinfo')) {
215
            // Linux (and potentially Windows with linux sub systems)
216
            $cpuinfo = file_get_contents('/proc/cpuinfo');
217
            preg_match_all('/^processor/m', $cpuinfo, $matches);
218
            $cores = count($matches[0]);
219
        } else if (DIRECTORY_SEPARATOR === '\\') {
220
            // Windows
221
            if (($process = @popen('wmic cpu get NumberOfCores', 'rb')) !== false) {
222
                fgets($process);
223
                $cores = (int) fgets($process);
224
                pclose($process);
225
            }
226
        } else {
227
            // *nix (Linux, BSD and Mac)
228
            if (($process = @popen('sysctl -a', 'rb')) !== false) {
229
                $output = stream_get_contents($process);
230
                if (preg_match('/hw.ncpu: (\d+)/', $output, $matches)) {
231
                    $cores = (int) $matches[1][0];
232
                }
233
                pclose($process);
234
            }
235
        }
236
237
        return $cores;
238
    }
239
}
240