Completed
Push — master ( e6cd28...033951 )
by Pablo
07:44
created

PhpCsFixerHandler   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 20.8 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 11
Bugs 1 Features 2
Metric Value
wmc 14
c 11
b 1
f 2
lcom 1
cbo 8
dl 26
loc 125
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C run() 26 49 8
A setFiles() 0 4 1
A setFilesToAnalyze() 0 4 1
A setLevels() 0 4 1
A getErrors() 0 10 2

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\BadJobLogo;
8
use PhpGitHooks\Command\OutputHandlerInterface;
9
use PhpGitHooks\Infrastructure\Common\InteractiveToolInterface;
10
use PhpGitHooks\Infrastructure\Common\ToolHandler;
11
use Symfony\Component\Process\ProcessBuilder;
12
13
class PhpCsFixerHandler extends ToolHandler implements InteractiveToolInterface, PhpCsFixerHandlerInterface
14
{
15
    /** @var array */
16
    private $files;
17
    /** @var string */
18
    private $filesToAnalyze;
19
    /** @var  array */
20
    private $levels = [];
21
    /**
22
     * @var IgnoreFiles
23
     */
24
    private $ignoreFiles;
25
26
    /**
27
     * PhpCsFixerHandler constructor.
28
     *
29
     * @param OutputHandlerInterface $outputHandler
30
     * @param IgnoreFiles            $ignoreFiles
31
     */
32
    public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles)
33
    {
34
        parent::__construct($outputHandler);
35
        $this->ignoreFiles = $ignoreFiles;
36
    }
37
38
    /**
39
     * @throws PhpCsFixerException
40
     */
41
    public function run(array $messages)
42
    {
43
        foreach ($this->levels as $level => $value) {
44
            if (true === $value) {
45
                $this->outputHandler->setTitle('Checking '.strtoupper($level).' code style with PHP-CS-FIXER');
46
                $this->output->write($this->outputHandler->getTitle());
47
48
                $errors = array();
49
50
                foreach ($this->files as $file) {
51
                    $srcFile = preg_match($this->filesToAnalyze, $file);
52
53
                    if (!$srcFile) {
54
                        continue;
55
                    }
56
57 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...
58
                        $processBuilder = new ProcessBuilder(
59
                            array(
60
                                'php',
61
                                'bin/php-cs-fixer',
62
                                '--dry-run',
63
                                'fix',
64
                                $file,
65
                                '--level='.$level,
66
                            )
67
                        );
68
69
                        $phpCsFixer = $processBuilder->getProcess();
70
                        $phpCsFixer->run();
71
72
                        if (false === $phpCsFixer->isSuccessful()) {
73
                            $errors[] = $phpCsFixer->getOutput();
74
                        }
75
                    }
76
                }
77
78 View Code Duplication
                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...
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...
79
                    $this->outputHandler->setError($this->getErrors($errors));
80
                    $this->output->writeln($this->outputHandler->getError());
81
                    $this->output->writeln(BadJobLogo::paint($messages[MessageConfigData::KEY_ERROR_MESSAGE]));
82
83
                    throw  new PhpCsFixerException();
84
                }
85
86
                $this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
87
            }
88
        }
89
    }
90
91
    /**
92
     * throw new PhpCsFixerException(implode('', $errors));
93
     * }.
94
     *
95
     * $this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
96
     * }
97
     *
98
     * /**
99
     * @param array $files
100
     */
101
    public function setFiles(array $files)
102
    {
103
        $this->files = $files;
104
    }
105
106
    /**
107
     * @param string $filesToAnalyze
108
     */
109
    public function setFilesToAnalyze($filesToAnalyze)
110
    {
111
        $this->filesToAnalyze = $filesToAnalyze;
112
    }
113
114
    /**
115
     * @param array $levels
116
     */
117
    public function setLevels(array $levels)
118
    {
119
        $this->levels = $levels;
120
    }
121
122
    /**
123
     * @param array $errors
124
     *
125
     * @return null|string
126
     */
127
    private function getErrors(array $errors)
128
    {
129
        $errorText = null;
130
131
        foreach ($errors as $error) {
132
            $errorText .= trim($error)."\n";
133
        }
134
135
        return $errorText;
136
    }
137
}
138