Completed
Push — master ( b28f87...a255d6 )
by Olivier
03:32
created

DebugCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 69
Duplicated Lines 28.99 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 30.3%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 3
dl 20
loc 69
ccs 10
cts 33
cp 0.303
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 9 1
A execute() 20 30 4
A getListing() 0 10 2

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
declare(strict_types=1);
4
5
namespace Shapin\Datagen\Bridge\Symfony\Bundle\Command;
6
7
use Shapin\Datagen\Loader;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
14
class DebugCommand extends Command
15
{
16
    private $connection;
17
18 2
    public function __construct(Loader $loader)
19
    {
20 2
        $this->loader = $loader;
0 ignored issues
show
Bug introduced by
The property loader does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
21
22 2
        parent::__construct();
23 2
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    protected function configure()
29
    {
30
        $this
31 2
            ->setName('shapin:datagen:debug')
32 2
            ->setDescription('Display information about fixtures.')
33 2
            ->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'List only fixtures from the given group.')
34 2
            ->addOption('grouped', null, InputOption::VALUE_NONE, 'Display fixtures by groups.')
35
        ;
36 2
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $io = new SymfonyStyle($input, $output);
44
45 View Code Duplication
        if (null !== $group = $input->getOption('group')) {
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
            $fixtures = $this->loader->getFixtures([$group]);
47
48
            $io->title("Fixtures in group \"$group\".");
49
            $io->listing($this->getListing($fixtures));
50
51
            return;
52
        }
53
54 View Code Duplication
        if (false !== $input->getOption('grouped')) {
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...
55
            $io->title('Repartition of fixtures by groups.');
56
57
            $fixturesByGroup = $this->loader->getFixturesByGroups();
58
59
            foreach ($fixturesByGroup as $group => $fixtures) {
60
                $io->section($group);
61
                $io->listing($this->getListing($fixtures));
62
            }
63
64
            return;
65
        }
66
67
        $io->title('All fixtures');
68
69
        $io->listing($this->getListing($this->loader->getFixtures()));
70
    }
71
72
    private function getListing(array $fixtures): array
73
    {
74
        $content = [];
75
        foreach ($fixtures as $fixture) {
76
            $order = str_pad((string) $fixture->getOrder(), 3, ' ', STR_PAD_LEFT);
77
            $content[] = "<comment>$order</comment> - <info>[{$fixture->getProcessor()}]</info> - {$fixture->getName()}";
78
        }
79
80
        return $content;
81
    }
82
}
83