Completed
Push — master ( 6d9bc6...2c6699 )
by Christian
02:48
created

PhpCsFixer::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 28
ccs 15
cts 17
cp 0.8824
rs 8.439
c 1
b 0
f 0
cc 5
eloc 16
nc 4
nop 2
crap 5.0406
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\Finder\SplFileInfo as SfyFileInfo;
8
use Symfony\Component\Process\Process;
9
use uuf6429\ElderBrother\Change\FileList;
10
11
class PhpCsFixer extends ActionAbstract
12
{
13
    const NONE_LEVEL = 0;
14
    const PSR0_LEVEL = 1;
15
    const PSR1_LEVEL = 3;
16
    const PSR2_LEVEL = 7;
17
    const SYMFONY_LEVEL = 15;
18
    const CONTRIB_LEVEL = 32;
19
20
    /**
21
     * @var FileList
22
     */
23
    protected $files;
24
25
    /**
26
     * @var int|null
27
     */
28
    protected $level;
29
30
    /**
31
     * @var string[]
32
     */
33
    protected $fixers;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $addAutomatically;
39
40
    /**
41
     * Runs all the provided files through PHP-CS-Fixer, fixing any code style issues.
42
     *
43
     * @param FileList $files            The files to check
44
     * @param int|null $level            (Optional, defaults to NONE_LEVEL) Fixer level to use
45
     * @param string[] $fixers           (Optional, default is empty) Set the fixers to use
46
     * @param bool     $addAutomatically (Optional, default is true) Whether to add modified files to commit or not
47
     */
48 3
    public function __construct(FileList $files, $level = self::NONE_LEVEL, $fixers = [], $addAutomatically = true)
49
    {
50 3
        $this->files = $files;
51 3
        $this->level = $level;
52 3
        $this->fixers = $fixers;
53 3
        $this->addAutomatically = $addAutomatically;
54 3
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getName()
60
    {
61
        return 'PHP Code Style Fixer (PhpCsFixer)';
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function isSupported()
68
    {
69
        if (!class_exists('\Symfony\CS\Fixer')) {
70
            $this->logger->warning(
71
                'PHP-CS-Fixer could not be loaded: class \Symfony\CS\Fixer not found.'
72
            );
73
74
            return false;
75
        }
76
77
        return true;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 3
    public function execute(InputInterface $input, OutputInterface $output)
84
    {
85 3
        $fixer = $this->getCsFixer();
86 3
        $fixers = $this->resolveFixers($fixer, $this->level, $this->fixers);
87 3
        $cache = new \Symfony\CS\FileCacheManager(false, getcwd(), $fixers);
88
89 3
        if(!$this->files->count()){
90 1
            return;
91
        }
92
93 2
        $progress = $this->createProgressBar($input, $output);
94 2
        $progress->start($this->files->count());
95
96
        /** @var SfyFileInfo $file */
97 2
        foreach ($this->files->getSourceIterator() as $file) {
98 2
            $progress->setMessage('Checking <info>' . $file->getRelativePathname() . '</info>...');
99
100 2
            if ($fixer->fixFile($file, $fixers, false, false, $cache) && $this->addAutomatically) {
101
                $process = new Process('git add ' . escapeshellarg($file->getRelativePathname()));
102
                $process->mustRun();
103
            }
104
105 2
            $progress->advance();
106
        }
107
108 2
        $progress->setMessage('Finished.');
109 2
        $progress->finish();
110 2
    }
111
112
    /**
113
     * @return \Symfony\CS\Fixer
114
     */
115 3
    private function getCsFixer()
116
    {
117 3
        $fixer = new \Symfony\CS\Fixer();
118 3
        $fixer->registerBuiltInFixers();
119 3
        $fixer->registerBuiltInConfigs();
120
121 3
        return $fixer;
122
    }
123
124
    /**
125
     * @param \Symfony\CS\Fixer $fixer
126
     * @param int               $level
127
     * @param string[]          $fixers
128
     *
129
     * @return \Symfony\CS\FixerInterface[]
130
     */
131 3
    private function resolveFixers($fixer, $level, $fixers)
132
    {
133 3
        $resolver = new \Symfony\CS\ConfigurationResolver();
134
        $resolver
135 3
            ->setAllFixers($fixer->getFixers())
136 3
            ->setConfig(
137 3
                \Symfony\CS\Config\Config::create()
138 3
                    ->level($level)
139 3
                    ->fixers($fixers)
140
            )
141 3
            ->resolve();
142
143 3
        return $resolver->getFixers();
144
    }
145
}
146