Completed
Push — master ( a1ebee...1be64c )
by Pablo
06:24
created

PhpCsFixerHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace PhpGitHooks\Infrastructure\PhpCsFixer;
4
5
use IgnoreFiles\IgnoreFiles;
6
use PhpGitHooks\Application\Message\MessageConfigData;
7
use PhpGitHooks\Command\OutputHandlerInterface;
8
use PhpGitHooks\Infrastructure\Common\InteractiveToolInterface;
9
use PhpGitHooks\Infrastructure\Common\ToolHandler;
10
use Symfony\Component\Process\ProcessBuilder;
11
12
class PhpCsFixerHandler extends ToolHandler implements InteractiveToolInterface, PhpCsFixerHandlerInterface
13
{
14
    /** @var array */
15
    private $files;
16
    /** @var string */
17
    private $filesToAnalyze;
18
    /** @var  array */
19
    private $levels = [];
20
    /**
21
     * @var IgnoreFiles
22
     */
23
    private $ignoreFiles;
24
25
    /**
26
     * PhpCsFixerHandler constructor.
27
     *
28
     * @param OutputHandlerInterface $outputHandler
29
     * @param IgnoreFiles            $ignoreFiles
30
     */
31
    public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles)
32
    {
33
        parent::__construct($outputHandler);
34
        $this->ignoreFiles = $ignoreFiles;
35
    }
36
37
    /**
38
     * @throws PhpCsFixerException
39
     */
40
    public function run(array $messages)
41
    {
42
        foreach ($this->levels as $level => $value) {
43
            if (true === $value) {
44
                $this->outputHandler->setTitle('Checking '.strtoupper($level).' code style with PHP-CS-FIXER');
45
                $this->output->write($this->outputHandler->getTitle());
46
47
                $errors = array();
48
49
                foreach ($this->files as $file) {
50
                    $srcFile = preg_match($this->filesToAnalyze, $file);
51
52
                    if (!$srcFile) {
53
                        continue;
54
                    }
55
56 View Code Duplication
                    if (false === $this->ignoreFiles->isIgnored($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...
57
                        $processBuilder = new ProcessBuilder(
58
                            array(
59
                                'php',
60
                                'bin/php-cs-fixer',
61
                                '--dry-run',
62
                                'fix',
63
                                $file,
64
                                '--level='.$level,
65
                            )
66
                        );
67
68
                        $phpCsFixer = $processBuilder->getProcess();
69
                        $phpCsFixer->run();
70
71
                        if (false === $phpCsFixer->isSuccessful()) {
72
                            $errors[] = $phpCsFixer->getOutput();
73
                        }
74
                    }
75
                }
76
77
                if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
                    $this->writeOutputError(
79
                        new PhpCsFixerException(implode('', $errors)),
80
                        $messages[MessageConfigData::KEY_ERROR_MESSAGE]
81
                    );
82
                }
83
84
                $this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
85
            }
86
        }
87
    }
88
89
    /**
90
     * throw new PhpCsFixerException(implode('', $errors));
91
     * }.
92
     *
93
     * $this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
94
     * }
95
     *
96
     * /**
97
     * @param array $files
98
     */
99
    public function setFiles(array $files)
100
    {
101
        $this->files = $files;
102
    }
103
104
    /**
105
     * @param string $filesToAnalyze
106
     */
107
    public function setFilesToAnalyze($filesToAnalyze)
108
    {
109
        $this->filesToAnalyze = $filesToAnalyze;
110
    }
111
112
    /**
113
     * @param array $levels
114
     */
115
    public function setLevels(array $levels)
116
    {
117
        $this->levels = $levels;
118
    }
119
}
120