| Conditions | 11 |
| Paths | 266 |
| Total Lines | 87 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 34 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
| 35 | { |
||
| 36 | $startRun = time(); |
||
| 37 | |||
| 38 | $remote = $input->getOption('remote'); |
||
| 39 | |||
| 40 | if ($remote) { |
||
| 41 | Runner::getInstance()->setRemote($remote); |
||
| 42 | } |
||
| 43 | |||
| 44 | $this->initConfiguration($input->getOption('configFile')); |
||
| 45 | |||
| 46 | $reporter = new InventorioGradeReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId()); |
||
| 47 | |||
| 48 | $debugMode = $input->getOption('debug'); |
||
| 49 | |||
| 50 | if (!$this->isInitialized()) { |
||
| 51 | $output->writeln('<error>System was not initialized. Please run inventorio init.</error>'); |
||
| 52 | return Command::FAILURE; |
||
| 53 | } |
||
| 54 | |||
| 55 | $this->initCollectors($debugMode); |
||
| 56 | |||
| 57 | $inventory = []; |
||
| 58 | |||
| 59 | $client = new \Startwind\Inventorio\Util\Client(new Client()); |
||
| 60 | |||
| 61 | $progressBar = new ProgressBar($output, count($this->collectors)); |
||
| 62 | |||
| 63 | $progressBar->setFormat(' %current%/%max% [%bar%] %percent:3s%% - %message%'); |
||
| 64 | $progressBar->setMessage('initializing'); |
||
| 65 | |||
| 66 | $progressBar->start(); |
||
| 67 | |||
| 68 | foreach ($this->collectors as $collector) { |
||
| 69 | if ($debugMode) $start = time(); |
||
| 70 | |||
| 71 | $progressBar->setMessage('collector: ' . $collector->getIdentifier()); |
||
| 72 | $progressBar->display(); |
||
| 73 | |||
| 74 | if ($collector instanceof InventoryAwareCollector) { |
||
| 75 | $collector->setInventory($inventory); |
||
| 76 | } |
||
| 77 | if ($collector instanceof ClientAwareCollector) { |
||
| 78 | $collector->setClient($client); |
||
| 79 | } |
||
| 80 | |||
| 81 | $collected = $collector->collect(); |
||
| 82 | |||
| 83 | if ($collected) { |
||
| 84 | $inventory[$collector->getIdentifier()] = $collected; |
||
| 85 | } else { |
||
| 86 | $inventory[$collector->getIdentifier()] = self::NOT_APPLICABLE; |
||
| 87 | } |
||
| 88 | |||
| 89 | $progressBar->advance(); |
||
| 90 | |||
| 91 | if ($debugMode) { |
||
| 92 | $output->writeln('DEBUG: running ' . $collector->getIdentifier() . ' took ' . time() - $start . ' seconds'); |
||
|
|
|||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $progressBar->setMessage('finished after ' . time() - $startRun . ' seconds'); |
||
| 97 | |||
| 98 | $progressBar->finish(); |
||
| 99 | |||
| 100 | $output->writeln(['', '']); |
||
| 101 | |||
| 102 | if ($debugMode) { |
||
| 103 | $output->writeln('DEBUG: collection result:'); |
||
| 104 | $output->writeln(json_encode($inventory, JSON_PRETTY_PRINT)); |
||
| 105 | } |
||
| 106 | |||
| 107 | Memory::getInstance()->setCollection($inventory); |
||
| 108 | |||
| 109 | try { |
||
| 110 | $reporter->report($inventory); |
||
| 111 | } catch (Exception $exception) { |
||
| 112 | $output->writeln('<error> '); |
||
| 113 | $output->writeln(' Unable to run reporter. '); |
||
| 114 | $output->writeln(' </error>'); |
||
| 115 | $output->writeln(''); |
||
| 116 | $output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>'); |
||
| 117 | return Command::FAILURE; |
||
| 118 | } |
||
| 119 | |||
| 120 | return Command::SUCCESS; |
||
| 121 | } |
||
| 123 |