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

AddAssetCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
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