Completed
Push — master ( 851d6f...c63a9b )
by Christian
02:28
created

PhpCsFixerOld::isSupported()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
crap 6
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
    public function isSupported()
60
    {
61
        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
        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
        $progress = $this->createProgressBar($input, $output, count($files));
83 3
        $progress->start();
84
85 3
        $failed = [];
86
87 3
        foreach ($files as $file) {
88 2
            $progress->setMessage('Processing ' . $file . '...');
89
90 2
            $process = new Process(
91
                sprintf(
92 2
                    'php -f %s -- fix %s %s',
93 2
                    escapeshellarg($this->binFile),
94
                    escapeshellarg($file),
95 2
                    $this->configFile ? ('--config-file=' . escapeshellarg($this->configFile)) : ''
96
                )
97
            );
98 2
            $process->run();
99
100 2
            switch ($process->getExitCode()) {
101 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...
102
                    // file has been changed
103 1
                    if ($this->addAutomatically) {
104
                        exec('git add ' . escapeshellarg($file));
105
                    }
106 1
                    break;
107 1
                case 1:
108
                    // file not changed
109 1
                    break;
110
                default:
111
                    // some sort of error
112
                    $failed[$file] = explode("\n", str_replace(PHP_EOL, "\n", $process->getOutput()));
113
                    break;
114
            }
115
116 2
            $progress->advance();
117
        }
118
119 3
        if (count($failed)) {
120
            $message = 'PhpCsFixerOld failed for the following file(s):';
121
            foreach ($failed as $file => $result) {
122
                $message .= PHP_EOL . '- ' . $file . ':';
123
                $message .= PHP_EOL . ' - ' . implode(PHP_EOL . ' - ', $result);
124
            }
125
            throw new \RuntimeException($message);
126
        }
127
128 3
        $progress->finish();
129 3
    }
130
131
    /**
132
     * @return string
133
     */
134
    protected function getBinFile()
135
    {
136
        return PROJECT_ROOT . 'vendor'
137
            . DIRECTORY_SEPARATOR . 'friendsofphp'
138
            . DIRECTORY_SEPARATOR . 'php-cs-fixer'
139
            . DIRECTORY_SEPARATOR . 'php-cs-fixer';
140
    }
141
}
142