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

FixCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 83
Duplicated Lines 16.87 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 2
cbo 6
dl 14
loc 83
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A configure() 0 6 1
A execute() 0 17 3
A fixDirectory() 14 14 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
/*
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 Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\StyleInterface;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\CS\Fixer;
18
use Symplify\MultiCodingStandard\CodeSniffer\CodeBeautifier;
19
use Symplify\MultiCodingStandard\CodeSniffer\CodeBeautifierFactory;
20
use Symplify\MultiCodingStandard\Console\ExitCode;
21
use Symplify\MultiCodingStandard\Contract\CodeSniffer\CodeBeautifierFactoryInterface;
22
23
final class FixCommand extends Command
24
{
25
    /**
26
     * @var CodeBeautifierFactoryInterface
27
     */
28
    private $codeBeautifierFactory;
29
30
    /**
31
     * @var StyleInterface
32
     */
33
    private $style;
34
35
    /**
36
     * @var Fixer
37
     */
38
    private $fixer;
39
40
    /**
41
     * @var CodeBeautifier
42
     */
43
    private $codeBeautifier;
44
45
    public function __construct(
46
        CodeBeautifierFactoryInterface $codeBeautifierFactory,
47
        Fixer $fixer,
48
        StyleInterface $style
49
    ) {
50
        parent::__construct();
51
52
        $this->codeBeautifierFactory = $codeBeautifierFactory;
53
        $this->fixer = $fixer;
54
        $this->style = $style;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function configure()
61
    {
62
        $this->setName('fix');
63
        $this->addArgument('path', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'The path(s)', null);
64
        $this->setDescription('Fix coding standard in one or more directories.');
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72
        // note: needs to be lazy created, due to constant-options in PHP_CodeSniffer
73
        $this->codeBeautifier = $this->codeBeautifierFactory->create();
74
75
        try {
76
            foreach ($input->getArgument('path') as $path) {
77
                $this->fixDirectory($path);
78
            }
79
80
            return ExitCode::SUCCESS;
81
        } catch (Exception $exception) {
82
            $this->style->error($exception->getMessage());
83
84
            return ExitCode::ERROR;
85
        }
86
    }
87
88
    /**
89
     * @param string $path
90
     */
91 View Code Duplication
    private function fixDirectory($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...
92
    {
93
        // code sniffer
94
        foreach (Finder::create()->in($path)->files() as $filePath => $fileInfo) {
95
            $file = $this->codeBeautifier->processFile($filePath);
96
            var_dump($file->getErrorCount());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($file->getErrorCount()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
97
        }
98
99
        // php-cs-fixer
100
101
        $this->style->success(
102
            sprintf('Directory "%s" was checked!', $path)
103
        );
104
    }
105
}
106