ExportCacheClearCommand   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 14.77 %

Coupling/Cohesion

Components 2
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 8
dl 13
loc 88
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 6 1
A itemToString() 0 12 3
A execute() 0 20 4
A getTypes() 13 13 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
namespace TreeHouse\IoBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use TreeHouse\IoBundle\Export\FeedExporter;
11
use TreeHouse\IoBundle\Export\FeedType\FeedTypeInterface;
12
13
class ExportCacheClearCommand extends Command
14
{
15
    /**
16
     * @var FeedExporter
17
     */
18
    protected $exporter;
19
20
    /**
21
     * @param FeedExporter $exporter
22
     */
23
    public function __construct(FeedExporter $exporter)
24
    {
25
        parent::__construct();
26
27
        $this->exporter = $exporter;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    protected function configure()
34
    {
35
        $this->setName('io:export:cache:clear');
36
        $this->addArgument('type', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The type(s) to export feeds for. If left empty, feeds for all known types are exported.');
37
        $this->addOption('where', null, InputOption::VALUE_OPTIONAL, 'Limit the cache to a specific set of the query, use <comment>x</comment> as root alias');
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $types = $this->getTypes($input->getArgument('type'));
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('type') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type null or string; however, TreeHouse\IoBundle\Comma...learCommand::getTypes() does only seem to accept array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
46
47
        $output->writeln(sprintf('Clearing cache for types: <info>%s</info>', implode(', ', $input->getArgument('type'))));
48
49
        foreach ($types as $type) {
50
            $builder = $type->getQueryBuilder('x');
51
            if ($input->getOption('where')) {
52
                $builder->andWhere($input->getOption('where'));
53
            }
54
55
            foreach ($builder->getQuery()->iterate() as list($item)) {
56
                $output->writeln(sprintf('Clearing cache for <comment>%s</comment>', $this->itemToString($item)));
57
                $this->exporter->clearCache($item, [$type]);
58
59
                $builder->getEntityManager()->detach($item);
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param array $types
66
     *
67
     * @return FeedTypeInterface[]
68
     */
69 View Code Duplication
    protected function getTypes(array $types)
0 ignored issues
show
Duplication introduced by
This method 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...
70
    {
71
        if (empty($types)) {
72
            return $this->exporter->getTypes();
73
        }
74
75
        $result = [];
76
        foreach ($types as &$type) {
77
            $result[] = $this->exporter->getType($type);
78
        }
79
80
        return $result;
81
    }
82
83
    /**
84
     * @param object $item
85
     *
86
     * @return string
87
     */
88
    private function itemToString($item)
89
    {
90
        if (method_exists($item, '__toString')) {
91
            return (string) $item;
92
        }
93
94
        if (method_exists($item, 'getId')) {
95
            return $item->getId();
96
        }
97
98
        return spl_object_hash($item);
99
    }
100
}
101