Bz2JsonImportCommand   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 2
cbo 5
dl 0
loc 42
ccs 26
cts 28
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 17 1
A executeCommand() 0 20 2
1
<?php
2
3
namespace Queryr\Replicator\Cli\Command;
4
5
use Queryr\Replicator\Cli\Import\PagesImporterCli;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Wikibase\JsonDumpReader\JsonDumpFactory;
11
12
/**
13
 * @licence GNU GPL v2+
14
 * @author Jeroen De Dauw < [email protected] >
15
 */
16
class Bz2JsonImportCommand extends ImportCommandBase {
17
18 2
	protected function configure() {
19 2
		$this->setName( 'import:bz2' );
20 2
		$this->setDescription( 'Imports entities from a bzip2 compressed JSON dump' );
21
22 2
		$this->addArgument(
23 2
			'file',
24 2
			InputArgument::REQUIRED,
25 2
			'Full path of the bz2 JSON dump file'
26
		);
27
28 2
		$this->addOption(
29 2
			'max',
30 2
			'm',
31 2
			InputOption::VALUE_OPTIONAL,
32 2
			'The maximum number of entities to import'
33
		);
34 2
	}
35
36 2
	protected function executeCommand( InputInterface $input, OutputInterface $output ) {
37 2
		$importer = new PagesImporterCli(
38 2
			$input,
39 2
			$output,
40 2
			$this->factory,
0 ignored issues
show
Bug introduced by
It seems like $this->factory can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
41 2
			function() use ( $output ) {
42
				$output->writeln( "\n" );
43
				$output->writeln( "<info>Import process aborted</info>" );
44 2
			}
45
		);
46
47 2
		$dumpReader = ( new JsonDumpFactory() )->newBz2DumpReader( $input->getArgument( 'file' ) );
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('file') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Wikibase\JsonDumpReader\...ory::newBz2DumpReader() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
48 2
		$iterator = $this->factory->newJsonEntityPageIterator( $dumpReader );
49
50 2
		if ( is_numeric( $input->getOption( 'max' ) ) ) {
51 1
			$iterator = new \LimitIterator( $iterator, 0, (int)$input->getOption( 'max' ) );
52
		}
53
54 2
		$importer->runImport( $iterator );
55 2
	}
56
57
}
58