| Conditions | 9 |
| Paths | 20 |
| Total 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 |
||
| 51 | public function process(ArticleInterface $article, ArticleMediaInterface $articleMedia): void |
||
| 52 | { |
||
| 53 | $body = $article->getBody(); |
||
| 54 | $mediaId = str_replace('/', '\\/', $articleMedia->getKey()); |
||
| 55 | |||
| 56 | preg_match( |
||
| 57 | "/(<!-- ?EMBED START Image {id: \"$mediaId\"} ?-->)(.+?)(<!-- ?EMBED END Image {id: \"$mediaId\"} ?-->)/im", |
||
| 58 | str_replace(PHP_EOL, '', $body), |
||
| 59 | $embeds |
||
| 60 | ); |
||
| 61 | |||
| 62 | if (empty($embeds)) { |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | |||
| 66 | $figureString = $embeds[2]; |
||
| 67 | $crawler = new Crawler($figureString); |
||
| 68 | $images = $crawler->filter('figure img'); |
||
| 69 | |||
| 70 | /** @var \DOMElement $imageElement */ |
||
| 71 | foreach ($images as $imageElement) { |
||
| 72 | /** @var ImageRendition $rendition */ |
||
| 73 | foreach ($articleMedia->getRenditions() as $rendition) { |
||
| 74 | if ($this->getDefaultImageRendition() === $rendition->getName()) { |
||
| 75 | $attributes = $imageElement->attributes; |
||
| 76 | $altAttribute = null; |
||
| 77 | if ($imageElement->hasAttribute('alt')) { |
||
| 78 | $altAttribute = $attributes->getNamedItem('alt'); |
||
| 79 | } |
||
| 80 | |||
| 81 | while ($attributes->length) { |
||
| 82 | $imageElement->removeAttribute($attributes->item(0)->name); |
||
| 83 | } |
||
| 84 | |||
| 85 | $imageElement->setAttribute('src', $this->mediaManager->getMediaUri($rendition->getImage())); |
||
| 86 | |||
| 87 | if (null === $rendition->getImage()->getId()) { |
||
| 88 | $imageElement->setAttribute('src', $rendition->getPreviewUrl()); |
||
| 89 | } |
||
| 90 | |||
| 91 | $imageElement->setAttribute('data-media-id', $mediaId); |
||
| 92 | $imageElement->setAttribute('data-image-id', $rendition->getImage()->getAssetId()); |
||
| 93 | $imageElement->setAttribute('data-rendition-name', $this->getDefaultImageRendition()); |
||
| 94 | $imageElement->setAttribute('width', (string) $rendition->getWidth()); |
||
| 95 | $imageElement->setAttribute('height', (string) $rendition->getHeight()); |
||
| 96 | |||
| 97 | if (null !== $altAttribute) { |
||
| 98 | $imageElement->setAttribute('alt', $altAttribute->nodeValue); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | $article->setBody(str_replace($figureString, $crawler->filter('body')->html(), $body)); |
||
| 105 | } |
||
| 106 | |||
| 126 |