| Conditions | 14 |
| Paths | 481 |
| Total Lines | 72 |
| Code Lines | 42 |
| 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 |
||
| 51 | public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = []) |
||
| 52 | { |
||
| 53 | // Get service URL |
||
| 54 | if (!empty($content)) { |
||
| 55 | $serviceURL = $content; |
||
| 56 | } elseif (!empty($arguments['url'])) { |
||
| 57 | $serviceURL = $arguments['url']; |
||
| 58 | } else { |
||
| 59 | return ''; |
||
| 60 | } |
||
| 61 | |||
| 62 | // See https://github.com/oscarotero/Embed#example-with-all-options for service arguments |
||
| 63 | $serviceArguments = []; |
||
| 64 | if (!empty($arguments['width'])) { |
||
| 65 | $serviceArguments['min_image_width'] = $arguments['width']; |
||
| 66 | } |
||
| 67 | if (!empty($arguments['height'])) { |
||
| 68 | $serviceArguments['min_image_height'] = $arguments['height']; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** @var EmbedResource $embed */ |
||
| 72 | $embed = Injector::inst()->create(Embeddable::class, $serviceURL); |
||
| 73 | if (!empty($serviceArguments)) { |
||
| 74 | $embed->setOptions(array_merge($serviceArguments, (array) $embed->getOptions())); |
||
| 75 | } |
||
| 76 | |||
| 77 | // Allow resolver to be mocked |
||
| 78 | $dispatcher = null; |
||
| 79 | if (isset($extra['resolver'])) { |
||
| 80 | $dispatcher = Injector::inst()->create( |
||
| 81 | $extra['resolver']['class'], |
||
| 82 | $serviceURL, |
||
| 83 | $extra['resolver']['config'] |
||
| 84 | ); |
||
| 85 | } elseif (Injector::inst()->has(DispatcherInterface::class)) { |
||
| 86 | $dispatcher = Injector::inst()->get(DispatcherInterface::class); |
||
| 87 | } |
||
| 88 | |||
| 89 | if ($dispatcher) { |
||
|
|
|||
| 90 | $embed->setDispatcher($dispatcher); |
||
| 91 | } |
||
| 92 | |||
| 93 | // Process embed |
||
| 94 | try { |
||
| 95 | $embed = $embed->getEmbed(); |
||
| 96 | } catch (InvalidUrlException $e) { |
||
| 97 | $message = (Director::isDev()) |
||
| 98 | ? $e->getMessage() |
||
| 99 | : _t(__CLASS__ . '.INVALID_URL', 'There was a problem loading the media.'); |
||
| 100 | |||
| 101 | $attr = [ |
||
| 102 | 'class' => 'ss-media-exception embed' |
||
| 103 | ]; |
||
| 104 | |||
| 105 | $result = HTML::createTag( |
||
| 106 | 'div', |
||
| 107 | $attr, |
||
| 108 | HTML::createTag('p', [], $message) |
||
| 109 | ); |
||
| 110 | return $result; |
||
| 111 | } |
||
| 112 | |||
| 113 | // Convert embed object into HTML |
||
| 114 | if ($embed && $embed instanceof Adapter) { |
||
| 115 | $result = static::embedForTemplate($embed, $arguments); |
||
| 116 | if ($result) { |
||
| 117 | return $result; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | // Fallback to link to service |
||
| 122 | return static::linkEmbed($arguments, $serviceURL, $serviceURL); |
||
| 123 | } |
||
| 235 |