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

PhpCsFixerHandler   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 108
Duplicated Lines 17.59 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 10
Bugs 0 Features 2
Metric Value
wmc 12
c 10
b 0
f 2
lcom 1
cbo 7
dl 19
loc 108
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C run() 19 48 8
A setFiles() 0 4 1
A setFilesToAnalyze() 0 4 1
A setLevels() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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