| Conditions | 20 |
| Paths | 1680 |
| Total Lines | 139 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | 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 |
||
| 21 | protected function doActualConvert() |
||
| 22 | { |
||
| 23 | $options = $this->options; |
||
| 24 | |||
| 25 | $beginTimeStack = microtime(true); |
||
| 26 | |||
| 27 | $anyRuntimeErrors = false; |
||
| 28 | |||
| 29 | $converters = $options['converters']; |
||
| 30 | if (count($options['extra-converters']) > 0) { |
||
| 31 | $converters = array_merge($converters, $options['extra-converters']); |
||
| 32 | /*foreach ($options['extra-converters'] as $extra) { |
||
| 33 | $converters[] = $extra; |
||
| 34 | }*/ |
||
| 35 | } |
||
| 36 | |||
| 37 | // preferred-converters |
||
| 38 | if (count($options['preferred-converters']) > 0) { |
||
| 39 | foreach (array_reverse($options['preferred-converters']) as $prioritizedConverter) { |
||
| 40 | foreach ($converters as $i => $converter) { |
||
| 41 | if (is_array($converter)) { |
||
| 42 | $converterId = $converter['converter']; |
||
| 43 | } else { |
||
| 44 | $converterId = $converter; |
||
| 45 | } |
||
| 46 | if ($converterId == $prioritizedConverter) { |
||
| 47 | unset($converters[$i]); |
||
| 48 | array_unshift($converters, $converter); |
||
| 49 | break; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | // perhaps write the order to the log? (without options) - but this requires some effort |
||
| 54 | } |
||
| 55 | |||
| 56 | // shuffle |
||
| 57 | if ($options['shuffle']) { |
||
| 58 | shuffle($converters); |
||
| 59 | } |
||
| 60 | |||
| 61 | //$this->logLn(print_r($converters)); |
||
| 62 | //$options['converters'] = $converters; |
||
| 63 | //$defaultConverterOptions = $options; |
||
| 64 | $defaultConverterOptions = []; |
||
| 65 | |||
| 66 | foreach ($this->options2->getOptionsMap() as $id => $option) { |
||
| 67 | if ($option->isValueExplicitlySet() && !($option instanceof GhostOption)) { |
||
| 68 | //$this->logLn('hi' . $id); |
||
| 69 | $defaultConverterOptions[$id] = $option->getValue(); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | //unset($defaultConverterOptions['converters']); |
||
| 74 | //unset($defaultConverterOptions['converter-options']); |
||
| 75 | $defaultConverterOptions['_skip_input_check'] = true; |
||
| 76 | $defaultConverterOptions['_suppress_success_message'] = true; |
||
| 77 | unset($defaultConverterOptions['converters']); |
||
| 78 | unset($defaultConverterOptions['extra-converters']); |
||
| 79 | unset($defaultConverterOptions['converter-options']); |
||
| 80 | unset($defaultConverterOptions['preferred-converters']); |
||
| 81 | unset($defaultConverterOptions['shuffle']); |
||
| 82 | |||
| 83 | // $this->logLn('converters: ' . print_r($converters, true)); |
||
| 84 | |||
| 85 | //return; |
||
| 86 | foreach ($converters as $converter) { |
||
| 87 | if (is_array($converter)) { |
||
| 88 | $converterId = $converter['converter']; |
||
| 89 | $converterOptions = isset($converter['options']) ? $converter['options'] : []; |
||
| 90 | } else { |
||
| 91 | $converterId = $converter; |
||
| 92 | $converterOptions = []; |
||
| 93 | if (isset($options['converter-options'][$converterId])) { |
||
| 94 | // Note: right now, converter-options are not meant to be used, |
||
| 95 | // when you have several converters of the same type |
||
| 96 | $converterOptions = $options['converter-options'][$converterId]; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $converterOptions = array_merge($defaultConverterOptions, $converterOptions); |
||
| 100 | /* |
||
| 101 | if ($converterId != 'stack') { |
||
| 102 | //unset($converterOptions['converters']); |
||
| 103 | //unset($converterOptions['converter-options']); |
||
| 104 | } else { |
||
| 105 | //$converterOptions['converter-options'] = |
||
| 106 | $this->logLn('STACK'); |
||
| 107 | $this->logLn('converterOptions: ' . print_r($converterOptions, true)); |
||
| 108 | }*/ |
||
| 109 | |||
| 110 | $beginTime = microtime(true); |
||
| 111 | |||
| 112 | $this->ln(); |
||
| 113 | $this->logLn('Trying: '.$converterId, 'italic'); |
||
| 114 | |||
| 115 | $converter = ConverterFactory::makeConverter( |
||
| 116 | $converterId, |
||
| 117 | $this->source, |
||
| 118 | $this->destination, |
||
| 119 | $converterOptions, |
||
| 120 | $this->logger |
||
| 121 | ); |
||
| 122 | |||
| 123 | try { |
||
| 124 | $converter->doConvert(); |
||
| 125 | |||
| 126 | //self::runConverterWithTiming($converterId, $source, $destination, $converterOptions, false, $logger); |
||
| 127 | |||
| 128 | $this->logLn($converterId.' succeeded :)'); |
||
| 129 | $this->converterUsed = $converterId; |
||
| 130 | //throw new ConverterNotOperationalException('...'); |
||
| 131 | return; |
||
| 132 | } catch (ConverterNotOperationalException $e) { |
||
| 133 | $this->logLn($e->getMessage()); |
||
| 134 | } catch (ConversionSkippedException $e) { |
||
| 135 | $this->logLn($e->getMessage()); |
||
| 136 | } catch (ConversionFailedException $e) { |
||
| 137 | $this->logLn($e->getMessage(), 'italic'); |
||
| 138 | $prev = $e->getPrevious(); |
||
| 139 | if (!is_null($prev)) { |
||
| 140 | $this->logLn($prev->getMessage(), 'italic'); |
||
| 141 | $this->logLn(' in '.$prev->getFile().', line '.$prev->getLine(), 'italic'); |
||
| 142 | $this->ln(); |
||
| 143 | } |
||
| 144 | //$this->logLn($e->getTraceAsString()); |
||
| 145 | $anyRuntimeErrors = true; |
||
| 146 | } |
||
| 147 | $this->logLn($converterId.' failed in '.round((microtime(true) - $beginTime) * 1000).' ms'); |
||
| 148 | } |
||
| 149 | |||
| 150 | $this->ln(); |
||
| 151 | $this->logLn('Stack failed in '.round((microtime(true) - $beginTimeStack) * 1000).' ms'); |
||
| 152 | |||
| 153 | // Hm, Scrutinizer complains that $anyRuntimeErrors is always false. But that is not true! |
||
| 154 | if ($anyRuntimeErrors) { |
||
|
|
|||
| 155 | // At least one converter failed |
||
| 156 | throw new ConversionFailedException('None of the converters in the stack could convert the image.'); |
||
| 157 | } else { |
||
| 158 | // All converters threw a SystemRequirementsNotMetException |
||
| 159 | throw new ConverterNotOperationalException('None of the converters in the stack are operational'); |
||
| 160 | } |
||
| 168 |