InvalidatePathCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 62
loc 62
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 8 8 2
A configure() 19 19 1
A execute() 14 14 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
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\HttpCacheBundle\Command;
13
14
use FOS\HttpCacheBundle\CacheManager;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
/**
20
 * A command to trigger cache invalidation by path from the command line.
21
 *
22
 * @author David Buchmann <[email protected]>
23
 */
24 View Code Duplication
class InvalidatePathCommand extends BaseInvalidateCommand
25
{
26
    use PathSanityCheck;
27
28
    protected static $defaultName = 'fos:httpcache:invalidate:path';
29
30
    /**
31
     * If no cache manager is specified explicitly, fos_http_cache.cache_manager
32
     * is automatically loaded.
33
     *
34
     * @param CacheManager|null $cacheManager The cache manager to talk to
35
     */
36 5
    public function __construct(CacheManager $cacheManager = null)
37
    {
38 5
        if (2 <= func_num_args()) {
39
            @trigger_error('Passing a command name in the constructor is deprecated and will be removed in version 3', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
40
            static::$defaultName = func_get_arg(1);
41
        }
42 5
        parent::__construct($cacheManager);
43 5
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 5
    protected function configure()
49
    {
50
        $this
51 5
            ->setDescription('Invalidate cached paths on all configured caching proxies')
52 5
            ->addArgument(
53 5
                'paths',
54 5
                InputArgument::IS_ARRAY | InputArgument::REQUIRED,
55 5
                'URL paths you want to invalidate, you can specify any number of paths'
56
            )
57 5
            ->setHelp(<<<'EOF'
58 5
The <info>%command.name%</info> command invalidates a list of paths on the configured caching proxies.
59
60
Example:
61
62
    <info>php %command.full_name% /some/path /other/path</info>
63
EOF
64
            )
65
        ;
66 5
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 3
    protected function execute(InputInterface $input, OutputInterface $output): int
72
    {
73 3
        $paths = $input->getArgument('paths');
74
75 3
        foreach ($paths as $path) {
0 ignored issues
show
Bug introduced by
The expression $paths of type string|array<integer,string>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
76 3
            if ($this->looksLikeRegularExpression($path)) {
77
                $output->writeln(sprintf('Path %s looks like a regular expression. Invalidation requests operate on exact paths. Use regex invalidation for regular expressions.', $path));
78
            }
79
80 3
            $this->getCacheManager()->invalidatePath($path);
81
        }
82
83 3
        return 0;
84
    }
85
}
86