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

PhpMDHandler::__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 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace PhpGitHooks\Infrastructure\PhpMD;
4
5
use IgnoreFiles\IgnoreFiles;
6
use PhpGitHooks\Application\Message\MessageConfigData;
7
use PhpGitHooks\Command\OutputHandlerInterface;
8
use PhpGitHooks\Infrastructure\Common\RecursiveToolInterface;
9
use PhpGitHooks\Infrastructure\Common\ToolHandler;
10
use Symfony\Component\Process\ProcessBuilder;
11
12
/**
13
 * Class PhpMDHandler.
14
 */
15
class PhpMDHandler extends ToolHandler implements RecursiveToolInterface
16
{
17
    /** @var  array */
18
    private $files;
19
    /** @var  string */
20
    private $needle;
21
    /**
22
     * @var IgnoreFiles
23
     */
24
    private $ignoreFiles;
25
26
    /**
27
     * PhpMDHandler 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
     * @param array $messages
40
     *
41
     * @throws PHPMDViolationsException
42
     */
43
    public function run(array $messages)
44
    {
45
        $this->outputHandler->setTitle('Checking code mess with PHPMD');
46
        $this->output->write($this->outputHandler->getTitle());
47
48
        $errors = [];
49
50
        foreach ($this->files as $file) {
51
            if (!preg_match($this->needle, $file)) {
52
                continue;
53
            }
54
55 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...
56
                $processBuilder = new ProcessBuilder(
57
                    array(
58
                        'php',
59
                        'bin/phpmd',
60
                        $file,
61
                        'text',
62
                        'PmdRules.xml',
63
                        '--minimumpriority',
64
                        1,
65
                    )
66
                );
67
                $process = $processBuilder->getProcess();
68
                $process->run();
69
70
                if (false === $process->isSuccessful()) {
71
                    $errors[] = $process->getOutput();
72
                }
73
            }
74
        }
75
76
        $errors = array_filter($errors, function ($var) {
77
            return !is_null($var);
78
        });
79
80
        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...
81
            $this->writeOutputError(
82
                new PHPMDViolationsException(implode('', $errors)),
83
                MessageConfigData::KEY_ERROR_MESSAGE
84
            );
85
        }
86
87
        $this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
88
    }
89
90
    /**
91
     * @param string $needle
92
     */
93
    public function setNeedle($needle)
94
    {
95
        $this->needle = $needle;
96
    }
97
98
    /**
99
     * @param array $files
100
     */
101
    public function setFiles($files)
102
    {
103
        $this->files = $files;
104
    }
105
}
106