| Conditions | 13 |
| Paths | 400 |
| Total Lines | 116 |
| Code Lines | 62 |
| 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 |
||
| 74 | protected function doActualConvert() |
||
| 75 | { |
||
| 76 | $options = $this->options; |
||
| 77 | |||
| 78 | $beginTimeStack = microtime(true); |
||
| 79 | |||
| 80 | $this->logLn('Stack converter ignited'); |
||
| 81 | |||
| 82 | // If we have set converter options for a converter, which is not in the converter array, |
||
| 83 | // then we add it to the array |
||
| 84 | if (isset($options['converter-options'])) { |
||
| 85 | foreach ($options['converter-options'] as $converterName => $converterOptions) { |
||
| 86 | if (!in_array($converterName, $options['converters'])) { |
||
| 87 | $options['converters'][] = $converterName; |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | //$this->logLn('converters: ' . print_r($options['converters'], true)); |
||
| 93 | |||
| 94 | $defaultConverterOptions = $options; |
||
| 95 | |||
| 96 | unset($defaultConverterOptions['converters']); |
||
| 97 | unset($defaultConverterOptions['converter-options']); |
||
| 98 | $defaultConverterOptions['_skip_input_check'] = true; |
||
| 99 | $defaultConverterOptions['_suppress_success_message'] = true; |
||
| 100 | |||
| 101 | $anyRuntimeErrors = false; |
||
| 102 | foreach ($options['converters'] as $converter) { |
||
| 103 | if (is_array($converter)) { |
||
| 104 | $converterId = $converter['converter']; |
||
| 105 | $converterOptions = $converter['options']; |
||
| 106 | } else { |
||
| 107 | $converterId = $converter; |
||
| 108 | $converterOptions = []; |
||
| 109 | if (isset($options['converter-options'][$converterId])) { |
||
| 110 | // Note: right now, converter-options are not meant to be used, |
||
| 111 | // when you have several converters of the same type |
||
| 112 | $converterOptions = $options['converter-options'][$converterId]; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $converterOptions = array_merge($defaultConverterOptions, $converterOptions); |
||
| 117 | |||
| 118 | // TODO: |
||
| 119 | // Reuse QualityProcessor of previous, unless quality option is overridden |
||
| 120 | // ON the other hand: With the recent change, the quality is not detected until a |
||
| 121 | // converter needs it (after operation checks). So such feature will rarely be needed now |
||
| 122 | |||
| 123 | // If quality is different, we must recalculate |
||
| 124 | /* |
||
| 125 | if ($converterOptions['quality'] != $defaultConverterOptions['quality']) { |
||
| 126 | unset($converterOptions['_calculated_quality']); |
||
| 127 | }*/ |
||
| 128 | |||
| 129 | $beginTime = microtime(true); |
||
| 130 | |||
| 131 | $className = self::getClassNameOfConverter($converterId); |
||
| 132 | |||
| 133 | |||
| 134 | try { |
||
| 135 | $converterDisplayName = call_user_func( |
||
| 136 | [$className, 'getConverterDisplayName'] |
||
| 137 | ); |
||
| 138 | } catch (\Exception $e) { |
||
| 139 | // TODO: handle failure better than this |
||
| 140 | $converterDisplayName = 'Untitled converter'; |
||
| 141 | } |
||
| 142 | |||
| 143 | try { |
||
| 144 | $this->ln(); |
||
| 145 | $this->logLn('Trying: ' . $converterId, 'italic'); |
||
| 146 | |||
| 147 | call_user_func( |
||
| 148 | [$className, 'convert'], |
||
| 149 | $this->source, |
||
| 150 | $this->destination, |
||
| 151 | $converterOptions, |
||
| 152 | $this->logger |
||
| 153 | ); |
||
| 154 | |||
| 155 | //self::runConverterWithTiming($converterId, $source, $destination, $converterOptions, false, $logger); |
||
| 156 | |||
| 157 | $this->logLn($converterDisplayName . ' succeeded :)'); |
||
| 158 | return; |
||
| 159 | } catch (ConverterNotOperationalException $e) { |
||
| 160 | $this->logLn($e->getMessage()); |
||
| 161 | } catch (ConversionFailedException $e) { |
||
| 162 | $this->logLn($e->getMessage(), 'italic'); |
||
| 163 | $prev = $e->getPrevious(); |
||
| 164 | if (!is_null($prev)) { |
||
| 165 | $this->logLn($prev->getMessage(), 'italic'); |
||
| 166 | $this->logLn(' in ' . $prev->getFile() . ', line ' . $prev->getLine(), 'italic'); |
||
| 167 | $this->ln(); |
||
| 168 | } |
||
| 169 | //$this->logLn($e->getTraceAsString()); |
||
| 170 | $anyRuntimeErrors = true; |
||
| 171 | } catch (ConversionDeclinedException $e) { |
||
| 172 | $this->logLn($e->getMessage()); |
||
| 173 | } |
||
| 174 | |||
| 175 | $this->logLn($converterDisplayName . ' failed in ' . round((microtime(true) - $beginTime) * 1000) . ' ms'); |
||
| 176 | } |
||
| 177 | |||
| 178 | $this->ln(); |
||
| 179 | $this->logLn('Stack failed in ' . round((microtime(true) - $beginTimeStack) * 1000) . ' ms'); |
||
| 180 | |||
| 181 | if ($anyRuntimeErrors) { |
||
|
|
|||
| 182 | // At least one converter failed |
||
| 183 | throw new ConversionFailedException( |
||
| 184 | 'None of the converters in the stack could convert the image. ' . |
||
| 185 | 'At least one failed, even though its requirements seemed to be met.' |
||
| 186 | ); |
||
| 187 | } else { |
||
| 188 | // All converters threw a SystemRequirementsNotMetException |
||
| 189 | throw new ConverterNotOperationalException('None of the converters in the stack are operational'); |
||
| 190 | } |
||
| 193 |