| Conditions | 16 |
| Paths | 673 |
| Total Lines | 99 |
| Code Lines | 55 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 52 | public function run(OutputInterface $output) |
||
| 53 | { |
||
| 54 | /** @type DebugFormatterHelper $debugFormatter */ |
||
| 55 | $debugFormatter = $this->getHelperSet()->get('debug_formatter'); |
||
| 56 | |||
| 57 | $arguments = $this->resolveProcessArgs(); |
||
| 58 | |||
| 59 | $builder = new ProcessBuilder($arguments); |
||
| 60 | $process = $builder->getProcess(); |
||
| 61 | |||
| 62 | if (null !== $dispatcher = $this->getEventDispatcher()) { |
||
| 63 | $event = new PreExecuteEvent($this, $process); |
||
| 64 | $dispatcher->dispatch(Event::PRE_EXECUTE, $event); |
||
| 65 | |||
| 66 | if ($event->isPropagationStopped()) { |
||
| 67 | return true; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERY_VERBOSE) { |
||
| 72 | $output->writeln( |
||
| 73 | sprintf( |
||
| 74 | ' // Setting timeout for %d seconds.', |
||
| 75 | $this->getParameter('timeout') |
||
| 76 | ) |
||
| 77 | ); |
||
| 78 | } |
||
| 79 | |||
| 80 | $process->setTimeout($this->getParameter('timeout') !== 0 ? $this->getParameter('timeout') : null); |
||
|
|
|||
| 81 | |||
| 82 | if ($this->hasParameter('cwd')) { |
||
| 83 | $process->setWorkingDirectory($this->getParameter('cwd')); |
||
| 84 | } |
||
| 85 | |||
| 86 | |||
| 87 | if (get_class($this) === 'Bldr\Block\Execute\Task\ExecuteTask') { |
||
| 88 | $output->writeln( |
||
| 89 | ['', sprintf(' <info>[%s]</info> - <comment>Starting</comment>', $this->getName()), ''] |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | |||
| 93 | if ($output->getVerbosity() === OutputInterface::VERBOSITY_VERBOSE || $this->getParameter('dry_run')) { |
||
| 94 | $output->writeln(' // '.$process->getCommandLine()); |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($this->getParameter('dry_run')) { |
||
| 98 | return true; |
||
| 99 | } |
||
| 100 | |||
| 101 | if ($this->hasParameter('output')) { |
||
| 102 | $append = $this->hasParameter('append') && $this->getParameter('append') ? 'a' : 'w'; |
||
| 103 | $stream = fopen($this->getParameter('output'), $append); |
||
| 104 | $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, true); |
||
| 105 | } |
||
| 106 | |||
| 107 | $output->writeln("<fg=blue>==============================\n</fg=blue>"); |
||
| 108 | $output->writeln( |
||
| 109 | $debugFormatter->start( |
||
| 110 | spl_object_hash($process), |
||
| 111 | $process->getCommandLine() |
||
| 112 | ) |
||
| 113 | ); |
||
| 114 | |||
| 115 | $process->run( |
||
| 116 | function ($type, $buffer) use ($output, $debugFormatter, $process) { |
||
| 117 | if ($this->getParameter('raw')) { |
||
| 118 | $output->write($buffer, false, OutputInterface::OUTPUT_RAW); |
||
| 119 | |||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | $output->write( |
||
| 124 | $debugFormatter->progress( |
||
| 125 | spl_object_hash($process), |
||
| 126 | $buffer, |
||
| 127 | Process::ERR === $type |
||
| 128 | ) |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | ); |
||
| 132 | |||
| 133 | $output->writeln( |
||
| 134 | $debugFormatter->stop( |
||
| 135 | spl_object_hash($process), |
||
| 136 | $process->getCommandLine(), |
||
| 137 | $process->isSuccessful() |
||
| 138 | ) |
||
| 139 | ); |
||
| 140 | $output->writeln("<fg=blue>==============================</fg=blue>"); |
||
| 141 | |||
| 142 | if (null !== $dispatcher) { |
||
| 143 | $event = new PostExecuteEvent($this, $process); |
||
| 144 | $dispatcher->dispatch(Event::POST_EXECUTE, $event); |
||
| 145 | } |
||
| 146 | |||
| 147 | if (!in_array($process->getExitCode(), $this->getParameter('successCodes'))) { |
||
| 148 | throw new TaskRuntimeException($this->getName(), $process->getErrorOutput()); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 165 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.