HeaderCommand::configure()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 19

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 24
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the php-formatter package
5
 *
6
 * Copyright (c) 2014-2016 Marc Morera
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Marc Morera <[email protected]>
14
 */
15
16
namespace Mmoreram\PHPFormatter\Command;
17
18
use Exception;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Input\InputOption;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Symfony\Component\Filesystem\Filesystem;
25
26
use Mmoreram\PHPFormatter\Finder\ConfigFinder;
27
use Mmoreram\PHPFormatter\Finder\FileFinder;
28
use Mmoreram\PHPFormatter\Fixer\HeaderFixer;
29
use Mmoreram\PHPFormatter\Loader\ConfigLoader;
30
31
/**
32
 * Class HeaderCommand.
33
 */
34
class HeaderCommand extends Command
35
{
36
    /**
37
     * @var string
38
     *
39
     * Command name
40
     */
41
    const COMMAND_NAME = 'header';
42
43
    /**
44
     * configure.
45
     */
46 View Code Duplication
    protected function configure()
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...
47
    {
48
        $this
49
            ->setName('formatter:header:fix')
50
            ->setDescription('Ensures that all PHP files have the header defined in the config file')
51
            ->addArgument(
52
                'path',
53
                InputArgument::REQUIRED,
54
                'Path'
55
            )
56
            ->addOption(
57
                '--config',
58
                '-c',
59
                InputOption::VALUE_OPTIONAL,
60
                'Config file directory',
61
                getcwd()
62
            )
63
            ->addOption(
64
                'dry-run',
65
                null,
66
                InputOption::VALUE_NONE,
67
                'Just print the result, nothing is overwritten'
68
            );
69
    }
70
71
    /**
72
     * Execute command.
73
     *
74
     * @param InputInterface  $input  Input
75
     * @param OutputInterface $output Output
76
     *
77
     * @return int|null|void
78
     *
79
     * @throws Exception
80
     */
81
    protected function execute(InputInterface $input, OutputInterface $output)
82
    {
83
        $verbose = $output->getVerbosity();
84
        $path = $input->getArgument('path');
85
        $dryRun = $input->getOption('dry-run');
86
        $fileFinder = new FileFinder();
87
        $configLoader = new ConfigLoader();
88
        $configFinder = new ConfigFinder();
89
90
        /**
91
         * This section is just for finding the right values to work with in
92
         * this execution.
93
         *
94
         * $options array will have, after this block, all these values
95
         */
96
        $configPath = rtrim($input->getOption('config'), DIRECTORY_SEPARATOR);
97
98
        $header = $configLoader->loadConfigValue(
99
            self::COMMAND_NAME,
100
            $configFinder->findConfigFile($configPath)
101
        );
102
103
        if (empty($header)) {
104
            throw new Exception('Header definition must be defined in .formatter.yml file under header');
105
        }
106
107
        /**
108
         * Building the real directory or file to work in.
109
         */
110
        $filesystem = new Filesystem();
111
        if (!$filesystem->isAbsolutePath($path)) {
112
            $path = getcwd() . DIRECTORY_SEPARATOR . $path;
113
        }
114
115 View Code Duplication
        if (!is_file($path) && !is_dir($path)) {
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...
116
            throw new Exception('Directory or file "' . $path . '" does not exist');
117
        }
118
119
        /*
120
         * Dry-run message
121
         */
122
        if ($dryRun && $verbose >= OutputInterface::VERBOSITY_VERBOSE) {
123
            $output->writeln('# This process has been executed in mode dry-run');
124
        }
125
126
        if ($verbose >= OutputInterface::VERBOSITY_VERBOSE) {
127
            $output->writeln('# Executing process in ' . $path);
128
        }
129
130
        /**
131
         * Creates the new HeaderFixer.
132
         */
133
        $headerFixer = new HeaderFixer($header);
0 ignored issues
show
Documentation introduced by
$header is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
134
135
        $files = $fileFinder->findPHPFilesByPath($path);
136
137
        /*
138
         * If verbose level is higher or equal than -vv, we print the config
139
         * file data, if is not empty.
140
         */
141
        if ($verbose >= OutputInterface::VERBOSITY_VERBOSE) {
142
            $output->writeln("# Header used:\n\n" . $header);
143
        }
144
145
        $output->writeln('#');
146
147
        /*
148
         * Each found php file is processed
149
         */
150 View Code Duplication
        foreach ($files as $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...
151
            $data = $file->getContents();
152
            $result = $headerFixer->fix($data);
153
154
            if ($result === false || $data === $result) {
155
                continue;
156
            }
157
158
            if ($verbose >= OutputInterface::VERBOSITY_NORMAL) {
159
                $output->writeln('# ' . $file);
160
            }
161
162
            if (!$dryRun) {
163
                file_put_contents($file->getRealPath(), $result);
164
            }
165
        }
166
    }
167
}
168