Completed
Push — 6.0 ( 30a204...9a1522 )
by liu
06:39
created

Clear::execute()   A

Complexity

Conditions 6
Paths 7

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 7
nop 2
dl 0
loc 20
ccs 0
cts 12
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
namespace think\console\command;
12
13
use think\console\Command;
14
use think\console\Input;
15
use think\console\input\Option;
16
use think\console\Output;
17
18
class Clear extends Command
19
{
20
    protected function configure()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configure()
Loading history...
21
    {
22
        // 指令配置
23
        $this
24
            ->setName('clear')
25
            ->addOption('path', 'd', Option::VALUE_OPTIONAL, 'path to clear', null)
26
            ->addOption('cache', 'c', Option::VALUE_NONE, 'clear cache file')
27
            ->addOption('log', 'l', Option::VALUE_NONE, 'clear log file')
28
            ->addOption('dir', 'r', Option::VALUE_NONE, 'clear empty dir')
29
            ->addOption('route', 'u', Option::VALUE_NONE, 'clear route cache')
30
            ->setDescription('Clear runtime file');
31
    }
32
33
    protected function execute(Input $input, Output $output)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function execute()
Loading history...
34
    {
35
        if ($input->getOption('route')) {
36
            $this->app->cache->clear('route_cache');
0 ignored issues
show
Unused Code introduced by
The call to think\Cache::clear() has too many arguments starting with 'route_cache'. ( Ignorable by Annotation )

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

36
            $this->app->cache->/** @scrutinizer ignore-call */ 
37
                               clear('route_cache');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
37
        } else {
38
            $runtimePath = $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR;
39
40
            if ($input->getOption('cache')) {
41
                $path = $runtimePath . 'cache';
42
            } elseif ($input->getOption('log')) {
43
                $path = $runtimePath . 'log';
44
            } else {
45
                $path = $input->getOption('path') ?: $runtimePath;
46
            }
47
48
            $rmdir = $input->getOption('dir') ? true : false;
49
            $this->clear(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR, $rmdir);
50
        }
51
52
        $output->writeln("<info>Clear Successed</info>");
53
    }
54
55
    protected function clear(string $path, bool $rmdir): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function clear()
Loading history...
56
    {
57
        $files = is_dir($path) ? scandir($path) : [];
58
59
        foreach ($files as $file) {
60
            if ('.' != $file && '..' != $file && is_dir($path . $file)) {
61
                array_map('unlink', glob($path . $file . DIRECTORY_SEPARATOR . '*.*'));
0 ignored issues
show
Bug introduced by
It seems like glob($path . $file . thi...TORY_SEPARATOR . '*.*') can also be of type false; however, parameter $arr1 of array_map() does only seem to accept array, 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

61
                array_map('unlink', /** @scrutinizer ignore-type */ glob($path . $file . DIRECTORY_SEPARATOR . '*.*'));
Loading history...
62
                if ($rmdir) {
63
                    rmdir($path . $file);
64
                }
65
            } elseif ('.gitignore' != $file && is_file($path . $file)) {
66
                unlink($path . $file);
67
            }
68
        }
69
    }
70
}
71