Completed
Pull Request — master (#98)
by Alejandro
06:29
created

CreateTagCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 56
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 56
loc 56
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 6 6 1
A configure() 12 12 1
A execute() 17 17 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
namespace Shlinkio\Shlink\CLI\Command\Tag;
3
4
use Acelaya\ZsmAnnotatedServices\Annotation as DI;
5
use Shlinkio\Shlink\Core\Service\Tag\TagService;
6
use Shlinkio\Shlink\Core\Service\Tag\TagServiceInterface;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Zend\I18n\Translator\Translator;
12
use Zend\I18n\Translator\TranslatorInterface;
13
14 View Code Duplication
class CreateTagCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
15
{
16
    /**
17
     * @var TagServiceInterface
18
     */
19
    private $tagService;
20
    /**
21
     * @var TranslatorInterface
22
     */
23
    private $translator;
24
25
    /**
26
     * CreateTagCommand constructor.
27
     * @param TagServiceInterface $tagService
28
     * @param TranslatorInterface $translator
29
     *
30
     * @DI\Inject({TagService::class, Translator::class})
31
     */
32 2
    public function __construct(TagServiceInterface $tagService, TranslatorInterface $translator)
33
    {
34 2
        $this->tagService = $tagService;
35 2
        $this->translator = $translator;
36 2
        parent::__construct();
37 2
    }
38
39 2
    protected function configure()
40
    {
41
        $this
42 2
            ->setName('tag:create')
43 2
            ->setDescription($this->translator->translate('Creates one or more tags.'))
44 2
            ->addOption(
45 2
                'name',
46 2
                't',
47 2
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
48 2
                $this->translator->translate('The name of the tags to create')
49
            );
50 2
    }
51
52 2
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54 2
        $tagNames = $input->getOption('name');
55 2
        if (empty($tagNames)) {
56 1
            $output->writeln(sprintf(
57 1
                '<comment>%s</comment>',
58 1
                $this->translator->translate('You have to provide at least one tag name')
59
            ));
60 1
            return;
61
        }
62
63 1
        $this->tagService->createTags($tagNames);
64 1
        $output->writeln($this->translator->translate('Created tags') . sprintf(': ["<info>%s</info>"]', implode(
65 1
            '</info>", "<info>',
66 1
            $tagNames
67
        )));
68 1
    }
69
}
70