Conditions | 15 |
Paths | 99 |
Total Lines | 87 |
Code Lines | 37 |
Lines | 19 |
Ratio | 21.84 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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)) { |
|
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); |
||
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) { |
|
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 |
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.