Completed
Push — master ( d372d6...3eec25 )
by Christian
02:37
created

PhpCsFixerOld::execute()   B

Complexity

Conditions 9
Paths 16

Size

Total Lines 57
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 10.1536

Importance

Changes 0
Metric Value
dl 0
loc 57
ccs 25
cts 33
cp 0.7576
rs 7.0745
c 0
b 0
f 0
cc 9
eloc 35
nc 16
nop 2
crap 10.1536

How to fix   Long Method   

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
namespace uuf6429\ElderBrother\Action;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Process\Process;
8
use uuf6429\ElderBrother\Change\FileList;
9
10
class PhpCsFixerOld extends ActionAbstract
11
{
12
    /**
13
     * @var FileList
14
     */
15
    protected $files;
16
17
    /**
18
     * @var string|null
19
     */
20
    protected $binFile;
21
22
    /**
23
     * @var string|null
24
     */
25
    protected $configFile;
26
27
    /**
28
     * @var bool
29
     */
30
    protected $addAutomatically;
31
32
    /**
33
     * Runs all the provided files through PHP-CS-Fixer, fixing any code style issues.
34
     *
35
     * @param FileList    $files            The files to check
36
     * @param string|null $binFile          (Optional, default is from vendor) File path to PHP-CS-Fixer binary
37
     * @param string|null $configFile       (Optional, default is project root) File path to PHP-CS-Fixer config
38
     * @param bool        $addAutomatically (Optional, default is true) Whether to add modified files to commit or not
39
     */
40 3
    public function __construct(FileList $files, $binFile = null, $configFile = null, $addAutomatically = true)
41
    {
42 3
        $this->files = $files;
43 3
        $this->binFile = $binFile ?: $this->getBinFile();
44 3
        $this->configFile = $configFile;
45 3
        $this->addAutomatically = $addAutomatically;
46 3
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getName()
52
    {
53
        return 'PHP Code Style Fixer (PhpCsFixerOld)';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 3
    public function isSupported()
60
    {
61 3
        if (!file_exists($this->binFile)) {
62
            $this->logger->warning(
63
                sprintf(
64
                    'PHP-CS-Fixer could not be found in: %s',
65
                    $this->binFile
66
                )
67
            );
68
69
            return false;
70
        }
71
72 3
        return true;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 3
    public function execute(InputInterface $input, OutputInterface $output)
79
    {
80 3
        $files = $this->files->toArray();
81
82 3
        if (empty($files)) {
83 1
            return;
84
        }
85
86 2
        $progress = $this->createProgressBar($input, $output);
87 2
        $progress->start(count($files));
88
89 2
        $failed = [];
90
91 2
        foreach ($files as $file) {
92 2
            $progress->setMessage('Checking <info>' . $file . '</info>...');
93
94 2
            $process = new Process(
95
                sprintf(
96 2
                    'php -f %s -- fix %s %s',
97 2
                    escapeshellarg($this->binFile),
98
                    escapeshellarg($file),
99 2
                    $this->configFile ? ('--config-file=' . escapeshellarg($this->configFile)) : ''
100
                )
101
            );
102 2
            $process->run();
103
104 2
            switch ($process->getExitCode()) {
105 2
                case 0:
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $process->getExitCode() of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
106
                    // file has been changed
107 1
                    if ($this->addAutomatically) {
108
                        exec('git add ' . escapeshellarg($file));
109
                    }
110 1
                    break;
111 1
                case 1:
112
                    // file not changed
113 1
                    break;
114
                default:
115
                    // some sort of error
116
                    $failed[$file] = explode("\n", str_replace(PHP_EOL, "\n", $process->getOutput()));
117
                    break;
118
            }
119
120 2
            $progress->advance();
121
        }
122
123 2
        if (count($failed)) {
124
            $message = 'PhpCsFixerOld failed for the following file(s):';
125
            foreach ($failed as $file => $result) {
126
                $message .= PHP_EOL . '- ' . $file . ':';
127
                $message .= PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $result);
128
            }
129
            throw new \RuntimeException($message);
130
        }
131
132 2
        $progress->setMessage('Finished.');
133 2
        $progress->finish();
134 2
    }
135
136
    /**
137
     * @return string
138
     */
139
    protected function getBinFile()
140
    {
141
        return PROJECT_ROOT . 'vendor'
142
            . DIRECTORY_SEPARATOR . 'friendsofphp'
143
            . DIRECTORY_SEPARATOR . 'php-cs-fixer'
144
            . DIRECTORY_SEPARATOR . 'php-cs-fixer';
145
    }
146
}
147