Completed
Push — feature/test-commands ( b61593...109eae )
by Oguzhan
04:47
created

SearchSpotifyArtistCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 2
cbo 6
dl 52
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 10 10 1
B execute() 28 28 4
A setSpotifyService() 4 4 1

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
 * This file is part of the Pbxg33k\MusicInfo package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * (c) 2017 Oguzhan uysal. All rights reserved
10
 ******************************************************************************/
11
namespace Pbxg33k\MusicInfo\Command;
12
13
use Pbxg33k\MusicInfo\Model\Artist;
14
use Pbxg33k\MusicInfo\Service\Spotify\Service;
15
use Symfony\Component\Console\Command\Command;
16
use Symfony\Component\Console\Helper\Table;
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22 View Code Duplication
class SearchSpotifyArtistCommand 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...
23
{
24
    /**
25
     * @var Service
26
     */
27
    protected $spotifyService;
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('music-info:search:spotify:artist')
33
            ->setDescription('Search an artist on Spotify')
34
            ->setHelp("This command will search and return an artist using the Spotify API")
35
            ->addOption('save', 'p', InputOption::VALUE_OPTIONAL, false)
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
36
            ->addArgument('query', InputArgument::REQUIRED, 'Artist name')
37
            ;
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $artist = $input->getArgument('query');
43
        if(!$artist) {
44
            throw new \InvalidArgumentException('Artist missing');
45
        }
46
47
        $result = $this->spotifyService->artist()->getByName($artist);
48
49
        $table = new Table($output);
50
        $table
51
            ->setHeaders(['ID','Artist']);
52
53
        if(!$result) {
54
            $output->writeln('No results');
55
        } else {
56
            /** @var Artist $artist */
57
            foreach($result as $artist) {
58
                $table
59
                    ->addRow([
60
                        $artist->getId(),
61
                        $artist->getName()
62
                    ]);
63
            }
64
65
            $table->render();
66
        }
67
    }
68
69
    public function setSpotifyService(Service $service)
70
    {
71
        $this->spotifyService = $service;
72
    }
73
}