Completed
Pull Request — master (#5)
by David
02:09
created

AddAssetCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 12.2 %

Coupling/Cohesion

Components 2
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 2
cbo 7
dl 5
loc 41
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 5 29 3

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
namespace TheCodingMachine\Discovery\Commands;
5
6
7
use Composer\Command\BaseCommand;
8
use Composer\Factory;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use TheCodingMachine\Discovery\Asset;
14
use TheCodingMachine\Discovery\AssetOperation;
15
use TheCodingMachine\Discovery\AssetsBuilder;
16
use TheCodingMachine\Discovery\AssetType;
17
use TheCodingMachine\Discovery\DiscoveryFileLoader;
18
use TheCodingMachine\Discovery\Dumper;
19
20
class AddAssetCommand extends BaseCommand
21
{
22
    protected function configure()
23
    {
24
        $this->setName('discovery:add')
25
             ->setDescription('Add an asset')
26
             ->addArgument('asset-type', InputArgument::REQUIRED, 'The asset type')
27
             ->addArgument('value', InputArgument::REQUIRED, 'The asset to add')
28
             ->addOption('priority', 'p', InputOption::VALUE_REQUIRED, 'The priority', 0.0);
29
    }
30
31
    protected function execute(InputInterface $input, OutputInterface $output)
32
    {
33
        $assetTypeName = $input->getArgument('asset-type');
34
        $assetValue = $input->getArgument('value');
35
36
        $priority = $input->getOption('priority');
37
38
        if (!is_numeric($priority)) {
39
            $output->writeln('<error>The priority must be a numeric value.</error>');
40
            return;
41
        }
42
43
        $loader = new DiscoveryFileLoader();
44
45 View Code Duplication
        if (file_exists('discovery.json')) {
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...
46
            $assetOperationTypes = $loader->loadDiscoveryFile(new \SplFileInfo('discovery.json'), '', '');
47
        } else {
48
            $assetOperationTypes = [];
49
        }
50
51
        $assetOperationTypes[$assetTypeName][] = new AssetOperation(AssetOperation::ADD, new Asset($assetValue, '', '', $priority, null));
52
53
        $loader->saveDiscoveryFile($assetOperationTypes, new \SplFileObject('discovery.json', 'w+'));
54
55
        $dumper = new Dumper($this->getComposer(), $this->getIO());
0 ignored issues
show
Bug introduced by
It seems like $this->getComposer() 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...
56
        $dumper->dumpDiscoveryFiles();
57
58
        $output->writeln('discovery.json file successfully modified.');
59
    }
60
}
61