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

CodeSnifferHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 8.43 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 9
c 7
b 0
f 1
lcom 1
cbo 8
dl 7
loc 83
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B run() 7 28 5
A setNeddle() 0 4 1
A setFiles() 0 4 1
A setStandard() 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\CodeSniffer;
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\ToolHandler;
10
use Symfony\Component\Process\Process;
11
use Symfony\Component\Process\ProcessBuilder;
12
13
/**
14
 * Class CodeSnifferHandler.
15
 */
16
class CodeSnifferHandler extends ToolHandler
17
{
18
    /** @var array */
19
    private $files;
20
    /** @var string */
21
    private $neddle;
22
    /** @var string */
23
    private $standard = 'PSR2';
24
    /**
25
     * @var IgnoreFiles
26
     */
27
    private $ignoreFiles;
28
29
    /**
30
     * CodeSnifferHandler constructor.
31
     *
32
     * @param OutputHandlerInterface $outputHandler
33
     * @param IgnoreFiles            $ignoreFiles
34
     */
35
    public function __construct(OutputHandlerInterface $outputHandler, IgnoreFiles $ignoreFiles)
36
    {
37
        parent::__construct($outputHandler);
38
        $this->ignoreFiles = $ignoreFiles;
39
    }
40
41
    /**
42
     * @param array $messages
43
     *
44
     * @throws InvalidCodingStandardException
45
     */
46
    public function run(array $messages)
47
    {
48
        $this->outputHandler->setTitle('Checking code style with PHPCS');
49
        $this->output->write($this->outputHandler->getTitle());
50
51
        foreach ($this->files as $file) {
52
            if (!preg_match($this->neddle, $file)) {
53
                continue;
54
            }
55
56
            if (false === $this->ignoreFiles->isIgnored($file)) {
57
                $processBuilder = new ProcessBuilder(array('php', 'bin/phpcs', '--standard='.$this->standard, $file));
58
                /** @var Process $phpCs */
59
                $phpCs = $processBuilder->getProcess();
60
                $phpCs->run();
61
62 View Code Duplication
                if (false === $phpCs->isSuccessful()) {
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...
63
                    $this->outputHandler->setError($phpCs->getOutput());
64
                    $this->output->writeln($this->outputHandler->getError());
65
                    $this->output->writeln(BadJobLogo::paint($messages[MessageConfigData::KEY_ERROR_MESSAGE]));
66
67
                    throw new InvalidCodingStandardException();
68
                }
69
            }
70
        }
71
72
        $this->output->writeln($this->outputHandler->getSuccessfulStepMessage());
73
    }
74
75
    /**
76
     * @param string $neddle
77
     */
78
    public function setNeddle($neddle)
79
    {
80
        $this->neddle = $neddle;
81
    }
82
83
    /**
84
     * @param array $files
85
     */
86
    public function setFiles($files)
87
    {
88
        $this->files = $files;
89
    }
90
91
    /**
92
     * @param array $standard
93
     */
94
    public function setStandard($standard)
95
    {
96
        $this->standard = $standard;
0 ignored issues
show
Documentation Bug introduced by
It seems like $standard of type array is incompatible with the declared type string of property $standard.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
97
    }
98
}
99