StrictCommand::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 Mmoreram\PHPFormatter\Fixer\StrictFixer;
20
use Symfony\Component\Console\Command\Command;
21
use Symfony\Component\Console\Input\InputArgument;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Input\InputOption;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Filesystem\Filesystem;
26
27
use Mmoreram\PHPFormatter\Finder\ConfigFinder;
28
use Mmoreram\PHPFormatter\Finder\FileFinder;
29
use Mmoreram\PHPFormatter\Loader\ConfigLoader;
30
31
/**
32
 * Class StrictCommand.
33
 */
34
class StrictCommand extends Command
35
{
36
    /**
37
     * @var string
38
     *
39
     * Command name
40
     */
41
    const COMMAND_NAME = 'strict';
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:strict:fix')
50
            ->setDescription('Ensures that all PHP files have strict mode defined in config file. Only valid for PHP7.0>')
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
        $configInFile = $configFinder->findConfigFile($configPath);
98
99
        $strict = $configLoader->loadConfigValue(
100
            self::COMMAND_NAME,
101
            $configInFile
102
        );
103
104
        if (!array_key_exists(self::COMMAND_NAME, $configInFile)) {
105
            throw new Exception('Strict definition must be defined in .formatter.yml file under');
106
        }
107
108
        /**
109
         * Building the real directory or file to work in.
110
         */
111
        $filesystem = new Filesystem();
112
        if (!$filesystem->isAbsolutePath($path)) {
113
            $path = getcwd() . DIRECTORY_SEPARATOR . $path;
114
        }
115
116 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...
117
            throw new Exception('Directory or file "' . $path . '" does not exist');
118
        }
119
120
        /*
121
         * Dry-run message
122
         */
123
        if ($dryRun && $verbose >= OutputInterface::VERBOSITY_VERBOSE) {
124
            $output->writeln('# This process has been executed in mode dry-run');
125
        }
126
127
        if ($verbose >= OutputInterface::VERBOSITY_VERBOSE) {
128
            $output->writeln('# Executing process in ' . $path);
129
        }
130
131
        /**
132
         * Creates the new StrictFixer.
133
         */
134
        $strictFixer = new StrictFixer($strict);
0 ignored issues
show
Documentation introduced by
$strict is of type array, but the function expects a boolean.

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