Passed
Push — master ( 5564fc...4ab603 )
by Mauro
02:05
created

CacheDestroyCommand::execute()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 12
c 1
b 1
f 0
nc 10
nop 2
dl 0
loc 27
rs 8.4444
1
<?php
2
3
namespace SimpleDIC\Console;
4
5
use SimpleDIC\DIC;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Helper\Table;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\SymfonyStyle;
12
13
class CacheDestroyCommand extends Command
14
{
15
    protected function configure()
16
    {
17
        $this
18
            ->setName('dic:cache-destroy')
19
            ->setDescription('Destroy the cache created by DIC.')
20
            ->setHelp('This command try to destroy cache files created by DIC and clear apcu cache.')
21
            ->addArgument('cache_dir', InputArgument::OPTIONAL)
22
        ;
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        $cacheDir = !empty($input->getArgument('cache_dir')) ? $input->getArgument('cache_dir') : __DIR__.'/../../_cache';
28
29
        if (false === is_dir($cacheDir)) {
0 ignored issues
show
Bug introduced by
It seems like $cacheDir can also be of type string[]; however, parameter $filename of is_dir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

29
        if (false === is_dir(/** @scrutinizer ignore-type */ $cacheDir)) {
Loading history...
30
            throw new \InvalidArgumentException($cacheDir . ' is not a valid dir');
0 ignored issues
show
Bug introduced by
Are you sure $cacheDir of type null|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
            throw new \InvalidArgumentException(/** @scrutinizer ignore-type */ $cacheDir . ' is not a valid dir');
Loading history...
31
        }
32
33
        foreach (scandir($cacheDir) as $file) {
0 ignored issues
show
Bug introduced by
It seems like $cacheDir can also be of type string[]; however, parameter $directory of scandir() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        foreach (scandir(/** @scrutinizer ignore-type */ $cacheDir) as $file) {
Loading history...
34
            if (!in_array($file, ['.', '..'])) {
35
36
                // destroy apcu
37
                if (extension_loaded('apc') && ini_get('apc.enabled')) {
38
                    $filePath = $cacheDir . DIRECTORY_SEPARATOR . $file;
39
                    $array = include($filePath);
40
41
                    foreach ($array as $id => $entry) {
42
                        apcu_delete(md5(sha1_file($filePath). DIRECTORY_SEPARATOR .$id));
43
                    }
44
                }
45
46
                // delete file
47
                unlink($filePath);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filePath does not seem to be defined for all execution paths leading up to this point.
Loading history...
48
            }
49
        }
50
51
        $output->writeln('<fg=green>Cache was successfully cleared.</>');
52
    }
53
}
54