Conditions | 10 |
Paths | 7 |
Total Lines | 52 |
Code Lines | 35 |
Lines | 20 |
Ratio | 38.46 % |
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 |
||
71 | protected function execute(InputInterface $input, OutputInterface $output) |
||
72 | { |
||
73 | $failedInstances = []; |
||
74 | $io = new SymfonyStyle($input, $output); |
||
75 | |||
76 | $driver = $input->getArgument('driver'); |
||
77 | |||
78 | $output->writeln('<bg=yellow;fg=red>Clearing cache operation can take a while, please be patient...</>'); |
||
79 | |||
80 | $callback = function ($name) use ($output, &$failedInstances) { |
||
81 | try { |
||
82 | if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
||
83 | $output->writeln("<fg=yellow>Clearing instance {$name} cache...</>"); |
||
84 | } |
||
85 | $this->phpfastcache->get($name)->clear(); |
||
86 | if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
||
87 | $output->writeln("<fg=green>Cache instance {$name} cleared</>"); |
||
88 | } |
||
89 | } catch (PhpfastcacheDriverCheckException $e) { |
||
90 | $failedInstances[] = $name; |
||
91 | if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
||
92 | $output->writeln("<fg=red>Cache instance {$name} not cleared, got exception: " . '<bg=red;options=bold>' . $e->getMessage() . '</>'); |
||
93 | } else { |
||
94 | $output->writeln("<fg=red>Cache instance {$name} not cleared (increase verbosity to get more information).</>"); |
||
95 | } |
||
96 | } |
||
97 | }; |
||
98 | |||
99 | $caches = $this->parameters; |
||
100 | |||
101 | if ($driver) { |
||
102 | View Code Duplication | if (\array_key_exists($driver, $caches[ 'drivers' ])) { |
|
|
|||
103 | $callback($driver); |
||
104 | if (!\count($failedInstances)) { |
||
105 | $io->success("Cache instance {$driver} cleared"); |
||
106 | } else { |
||
107 | $io->error("Cache instance {$driver} not cleared"); |
||
108 | } |
||
109 | } else { |
||
110 | $io->error("Cache instance {$driver} does not exists"); |
||
111 | } |
||
112 | View Code Duplication | } else { |
|
113 | foreach ($caches[ 'drivers' ] as $name => $parameters) { |
||
114 | $callback($name); |
||
115 | } |
||
116 | if (!\count($failedInstances)) { |
||
117 | $io->success('All caches instances got cleared'); |
||
118 | } else { |
||
119 | $io->success('Almost all caches instances got cleared, except these: ' . \implode(', ', $failedInstances)); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | } |
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.