| Conditions | 11 |
| Paths | 9 |
| Total Lines | 61 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 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 |
||
| 25 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 26 | { |
||
| 27 | $this->initConfiguration($input->getOption('configFile')); |
||
| 28 | $debugMode = $input->getOption('debug'); |
||
| 29 | $debugFile = $input->getOption('debugFile'); |
||
| 30 | |||
| 31 | if (!$this->isInitialized()) { |
||
| 32 | $output->writeln('<error>System was not initialized. Please run inventorio init.</error>'); |
||
| 33 | return Command::FAILURE; |
||
| 34 | } |
||
| 35 | |||
| 36 | if (!$debugFile) { |
||
| 37 | $this->initCollectors($debugMode); |
||
| 38 | $inventory = []; |
||
| 39 | $client = new \Startwind\Inventorio\Util\Client(new Client()); |
||
| 40 | |||
| 41 | foreach ($this->collectors as $collector) { |
||
| 42 | if ($collector instanceof InventoryAwareCollector) { |
||
| 43 | $collector->setInventory($inventory); |
||
| 44 | } |
||
| 45 | if ($collector instanceof ClientAwareCollector) { |
||
| 46 | $collector->setClient($client); |
||
| 47 | } |
||
| 48 | |||
| 49 | if ($debugMode) $start = time(); |
||
| 50 | |||
| 51 | $collected = $collector->collect(); |
||
| 52 | if ($collected) { |
||
| 53 | $inventory[$collector->getIdentifier()] = $collected; |
||
| 54 | } else { |
||
| 55 | $inventory[$collector->getIdentifier()] = self::NOT_APPLICABLE; |
||
| 56 | } |
||
| 57 | if ($debugMode) { |
||
| 58 | $output->writeln('DEBUG: running ' . $collector->getIdentifier() . ' took ' . (time() - $start) . ' seconds'); |
||
|
|
|||
| 59 | } |
||
| 60 | } |
||
| 61 | |||
| 62 | Memory::getInstance()->setCollection($inventory); |
||
| 63 | } else { |
||
| 64 | $inventory = json_decode(file_get_contents(__DIR__ . '/../../debug/debug.json'), true); |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($debugMode) { |
||
| 68 | $output->writeln('DEBUG: collection result:'); |
||
| 69 | $output->writeln(json_encode($inventory, JSON_PRETTY_PRINT)); |
||
| 70 | } |
||
| 71 | |||
| 72 | $reporter = new InventorioReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId()); |
||
| 73 | |||
| 74 | try { |
||
| 75 | $reporter->report($inventory); |
||
| 76 | } catch (Exception $exception) { |
||
| 77 | $output->writeln('<error> '); |
||
| 78 | $output->writeln(' Unable to run reporter. '); |
||
| 79 | $output->writeln(' </error>'); |
||
| 80 | $output->writeln(''); |
||
| 81 | $output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>'); |
||
| 82 | return Command::FAILURE; |
||
| 83 | } |
||
| 84 | |||
| 85 | return Command::SUCCESS; |
||
| 86 | } |
||
| 88 |