Completed
Push — master ( e3fefc...6fb92f )
by Tomáš
8s
created

CheckCommand::getCodeSniffer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2016 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\MultiCodingStandard\Command;
9
10
use Exception;
11
use PHP_CodeSniffer;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Console\Style\StyleInterface;
17
use Symfony\Component\Finder\Finder;
18
use Symplify\MultiCodingStandard\Console\ExitCode;
19
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeSnifferFactoryInterface;
20
21
final class CheckCommand extends Command
22
{
23
    /**
24
     * @var PHP_CodeSniffer
25
     */
26
    private $codeSniffer;
27
28
    /**
29
     * @var StyleInterface
30
     */
31
    private $style;
32
33
    /**
34
     * @var CodeSnifferFactoryInterface
35
     */
36
    private $codeSnifferFactory;
37
38
    public function __construct(
39
        CodeSnifferFactoryInterface $codeSnifferFactory,
40
        StyleInterface $style
41
    ) {
42
        parent::__construct();
43
44
        $this->codeSnifferFactory = $codeSnifferFactory;
45
        $this->style = $style;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    protected function configure()
52
    {
53
        $this->setName('check');
54
        $this->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s)', null);
55
        $this->setDescription('Check coding standard in one or more directories.');
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    protected function execute(InputInterface $input, OutputInterface $output)
62
    {
63
        try {
64
            foreach ($input->getArgument('path') as $path) {
65
                $this->checkDirectory($path);
66
            }
67
68
            return ExitCode::SUCCESS;
69
        } catch (Exception $exception) {
70
            $this->style->error($exception->getMessage());
71
72
            return ExitCode::ERROR;
73
        }
74
    }
75
76
    /**
77
     * @param string $path
78
     */
79 View Code Duplication
    private function checkDirectory($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
80
    {
81
        // code sniffer
82
        foreach ((new Finder())->in($path)->files() as $filePath => $fileInfo) {
83
            $file = $this->getCodeSniffer()->processFile($filePath);
0 ignored issues
show
Unused Code introduced by
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
85
        }
86
87
        // php-cs-fixer
88
        
89
90
        $this->style->success(
91
            sprintf('Directory "%s" was checked!', $path)
92
        );
93
    }
94
95
    /**
96
     * Lazy loaded due to duplicated constants in setup
97
     * in CodeSniffer for both Sniffer and Fixer.
98
     *
99
     * @return PHP_CodeSniffer
100
     */
101
    private function getCodeSniffer()
102
    {
103
        if ($this->codeSniffer) {
104
            return $this->codeSniffer;
105
        }
106
107
        $this->codeSniffer = $this->codeSnifferFactory->create();
108
109
        return $this->codeSniffer;
110
    }
111
}
112