Completed
Push — develop ( 20877f...045e3f )
by
unknown
13s
created

ImportCommandAbstract::getFinder()   C

Complexity

Conditions 10
Paths 6

Size

Total Lines 56
Code Lines 29

Duplication

Lines 13
Ratio 23.21 %

Code Coverage

Tests 19
CRAP Score 15.8005

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 13
loc 56
ccs 19
cts 31
cp 0.6129
rs 6.7741
cc 10
eloc 29
nc 6
nop 1
crap 15.8005

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * base abstract for import based commands where a bunch of file must be collected and
4
 * done something with them..
5
 */
6
7
namespace Graviton\ImportExport\Command;
8
9
use Graviton\ImportExport\Exception\MissingTargetException;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Finder\Finder;
14
use Symfony\Component\Finder\SplFileInfo;
15
16
/**
17
 * @author   List of contributors <https://github.com/libgraviton/import-export/graphs/contributors>
18
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
19
 * @link     http://swisscom.ch
20
 */
21
abstract class ImportCommandAbstract extends Command
22
{
23
    /**
24
     * @var Finder
25
     */
26
    protected $finder;
27
28
    /**
29
     * @param Finder $finder finder instance
30
     */
31 6
    public function __construct(
32
        Finder $finder
33
    ) {
34 6
        $this->finder = $finder;
35 6
        parent::__construct();
36 6
    }
37
38
    /**
39
     * Executes the current command.
40
     *
41
     * @param InputInterface  $input  User input on console
42
     * @param OutputInterface $output Output of the command
43
     *
44
     * @return integer
45
     */
46 6
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 6
        $exitCode = 1;
49
        try {
50 6
            $exitCode = $this->doImport($this->getFinder($input), $input, $output);
0 ignored issues
show
Bug introduced by
It seems like $this->getFinder($input) targeting Graviton\ImportExport\Co...ndAbstract::getFinder() can also be of type array<integer,object<Sym...nt\Finder\SplFileInfo>>; however, Graviton\ImportExport\Co...andAbstract::doImport() does only seem to accept object<Symfony\Component\Finder\Finder>, 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...
51 6
        } catch (MissingTargetException $e) {
52 1
            $output->writeln('<error>' . $e->getMessage() . '</error>');
53
        }
54 6
        return $exitCode;
55
    }
56
57
    /**
58
     * Returns a Finder according to the input params
59
     *
60
     * @param InputInterface $input User input on console
61
     *
62
     * @return Finder|SplFileInfo[] finder
63
     */
64 6
    protected function getFinder($input)
65
    {
66 6
        $files = $input->getArgument('file');
67 6
        $inputFile = $input->getOption('input-file');
68
69 6
        if (empty($files) && is_null($inputFile)) {
70
            throw new \LogicException('You either need to provide <file> arguments or the --input-file option.');
71
        }
72
73 6
        $finder = $this->finder->files();
74
75
        /**
76
         * @param SplFileInfo $file
77
         * @return bool
78
         */
79 View Code Duplication
        $filter = function (SplFileInfo $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
80 6
            if (!in_array($file->getExtension(), ['yml','json']) || $file->isDir()) {
81 1
                return false;
82
            }
83 6
            return true;
84 6
        };
85
86 6
        if (is_null($inputFile)) {
87
            // normal way - file arguments..
88 6 View Code Duplication
            foreach ($files as $file) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
89 6
                if (is_file($file)) {
90 4
                    $finder->in(dirname($file))->name(basename($file));
91 4
                } else {
92 2
                    $finder->in($file);
93
                }
94 6
            }
95 6
        } else {
96
            // file list via input file
97
            if (!file_exists($inputFile)) {
98
                throw new \LogicException('File '.$inputFile.' does not seem to exist.');
99
            }
100
101
            $fileList = explode(PHP_EOL, file_get_contents($inputFile));
102
103
            $fileList = array_filter(
104
                $fileList,
105
                function ($val) {
106
                    if (!empty($val)) {
107
                        return true;
108
                    }
109
                    return false;
110
                }
111
            );
112
113
            $finder->append($fileList);
114
        }
115
116 6
        $finder->ignoreDotFiles(true)->filter($filter);
117
118 6
        return $finder;
119
    }
120
121
    /**
122
     * the actual command can do his import here..
123
     *
124
     * @param Finder          $finder finder
125
     * @param InputInterface  $input  input
126
     * @param OutputInterface $output output
127
     *
128
     * @return mixed
129
     */
130
    abstract protected function doImport(Finder $finder, InputInterface $input, OutputInterface $output);
131
}
132