Completed
Push — master ( 71c78b...6eeaca )
by recca
10:24 queued 05:45
created

Find::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 1
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Recca0120\Terminal\Console\Commands;
4
5
use Exception;
6
use Illuminate\Console\Command;
7
use Symfony\Component\Finder\Finder;
8
use Illuminate\Filesystem\Filesystem;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Input\StringInput;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class Find extends Command
16
{
17
    /**
18
     * The console command name.
19
     *
20
     * @var string
21
     */
22
    protected $name = 'find';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'search for files in a directory hierarchy';
30
31
    /**
32
     * $finder.
33
     *
34
     * @var \Symfony\Component\Finder\Finder
35
     */
36
    protected $finder;
37
38
    /**
39
     * $filesystem.
40
     *
41
     * @var \Illuminate\Filesystem\Filesystem
42
     */
43
    protected $filesystem;
44
45
    /**
46
     * __construct.
47
     *
48
     * @param \Symfony\Component\Finder\Finder  $finder
49
     * @param \Illuminate\Filesystem\Filesystem $filesystem
50
     */
51 8
    public function __construct(Finder $finder, Filesystem $filesystem)
52
    {
53 8
        parent::__construct();
54 8
        $this->finder = $finder;
55 8
        $this->filesystem = $filesystem;
56 8
    }
57
58
    /**
59
     * run.
60
     *
61
     * @param \Symfony\Component\Console\Input\InputInterface   $input
62
     * @param \Symfony\Component\Console\Output\OutputInterface $output
63
     * @return int
64
     */
65 1
    public function run(InputInterface $input, OutputInterface $output)
66
    {
67 1
        $command = (string) $input;
68 1
        $command = strtr($command, [
69 1
            ' -name' => ' -N',
70 1
            ' -type' => ' -T',
71 1
            ' -maxdepth' => ' -M',
72 1
            ' -delete' => ' -d true',
73 1
        ]);
74
75 1
        return parent::run(new StringInput($command), $output);
76
    }
77
78
    /**
79
     * fire.
80
     */
81 7
    public function fire()
82
    {
83
        // set_time_limit(30);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
85 7
        $path = $this->argument('path');
86 7
        $name = $this->option('name');
87 7
        $type = $this->option('type');
88 7
        $maxDepth = $this->option('maxdepth');
89 7
        $delete = filter_var($this->option('delete'), FILTER_VALIDATE_BOOLEAN);
90
91 7
        $root = is_null($this->getLaravel()) === false ?
92 7
            $this->getLaravel()->basePath() : getcwd();
93 7
        $path = rtrim($root, '/').'/'.$path;
94
95 7
        $this->finder->in($path);
96
97 7
        if ($name !== null) {
98 7
            $this->finder->name($name);
1 ignored issue
show
Bug introduced by
It seems like $name defined by $this->option('name') on line 86 can also be of type array; however, Symfony\Component\Finder\Finder::name() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99 7
        }
100
101
        switch ($type) {
102 7
            case 'd':
103 1
                $this->finder->directories();
104 1
                break;
105 6
            case 'f':
106 1
                $this->finder->files();
107 1
                break;
108
        }
109
110 7
        if (is_null($maxDepth) === false) {
111 2
            if ($maxDepth == '0') {
112 1
                $this->line($path);
113
114 1
                return;
115
            }
116 1
            $this->finder->depth('<'.$maxDepth);
117 1
        }
118
119 6
        foreach ($this->finder->getIterator() as $file) {
120 6
            $realPath = $file->getRealpath();
121 6
            if ($delete === true && $this->filesystem->exists($realPath) === true) {
122 2
                $removed = false;
123
                try {
124 2
                    if ($this->filesystem->isDirectory($realPath) === true) {
125 1
                        $removed = $this->filesystem->deleteDirectory($realPath);
126 1
                    } else {
127
                        $removed = $this->filesystem->delete($realPath);
128
                    }
129 2
                } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
130
                }
131 2
                $removed === true ?
132 2
                    $this->info('removed '.$realPath) : $this->error('removed '.$realPath.' fail');
133 2
            } else {
134 5
                $this->line($realPath);
135
            }
136 6
        }
137 6
    }
138
139
    /**
140
     * Get the console command arguments.
141
     *
142
     * @return array
143
     */
144 8
    protected function getArguments()
145
    {
146
        return [
147 8
            ['path', InputArgument::REQUIRED, 'path'],
148 8
        ];
149
    }
150
151
    /**
152
     * Get the console command options.
153
     *
154
     * @return array
155
     */
156 8
    protected function getOptions()
157
    {
158
        return [
159 8
            ['type', 'T', InputOption::VALUE_OPTIONAL, 'File is of type c: [f, d]'],
160 8
            ['name', 'N', InputOption::VALUE_OPTIONAL, 'Base of file name (the path with the leading directories removed) matches shell pattern pattern.  The metacharacters (`*\', `?\', and `[]\') match a `.\' at  the  start of  the  base name (this is a change in findutils-4.2.2; see section STANDARDS CONFORMANCE below).  To ignore a directory and the files under it, use -prune; see an example in the description of -path.  Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns.  The filename matching is performed with the use of the fnmatch(3) library function.   Don\'t forget to enclose the pattern in quotes in order to protect it from expansion by the shell.'],
161 8
            ['maxdepth', 'M', InputOption::VALUE_OPTIONAL, '-maxdepth alias -M'],
162 8
            ['delete', 'd', InputOption::VALUE_OPTIONAL, 'Delete files; true if removal succeeded.  If the removal failed, an error message is issued.  If -delete fails, find\'s exit status will be nonzervagranto (when it  eventually exits).  Use of -delete automatically turns on the -depth option.'],
163 8
        ];
164
    }
165
}
166