GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RemoveWrappingParagraphElementsEventHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 41 4
1
<?php
2
3
namespace Webfactory\ShortcodeBundle\Handler;
4
5
use Thunder\Shortcode\Event\ReplaceShortcodesEvent;
6
use Thunder\Shortcode\Shortcode\ReplacedShortcode;
7
8
/**
9
 * Removes <p>...</p> tags if they directly wrap a short code.
10
 *
11
 * @see https://github.com/thunderer/Shortcode/issues/40
12
 */
13
final class RemoveWrappingParagraphElementsEventHandler
14
{
15
    public function __invoke(ReplaceShortcodesEvent $event)
16
    {
17
        /* The text still containing shortcodes */
18
        $text = $event->getText();
19
20
        /* The generated ReplacedShortcode instances. Each ReplacedShortcode contains the Shortcode string in the
21
          original form, it's offset and it's replacement string. */
22
        $replacements = $event->getReplacements();
23
        if (!$replacements) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $replacements of type Thunder\Shortcode\Shortcode\ReplacedShortcode[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
24
            return;
25
        }
26
27
        /* Do the replacements from end to back to front, so the offsets remain valid. */
28
        $replacements_reversed = array_reverse($replacements);
29
        $result = array_reduce(
30
            $replacements_reversed,
31
            function ($state, ReplacedShortcode $r) {
32
                $offset = $r->getOffset();
33
                $lengthOfShortcode = mb_strlen($r->getText(), 'utf-8');
34
                $sourceTextBeforeShortcode = mb_substr($state, 0, $offset, 'utf-8');
35
                $sourceTextAfterShortcode = mb_substr($state, $offset + $lengthOfShortcode, null, 'utf-8');
36
37
                if (
38
                    preg_match('~(\s*<p>\s*)$~', $sourceTextBeforeShortcode, $prefixMatch)
39
                    && preg_match('~(^\s*</p>\s*)~', $sourceTextAfterShortcode, $postfixMatch)
40
                ) {
41
                    $sourceTextBeforeShortcode = mb_substr($state, 0, $offset - mb_strlen($prefixMatch[0], 'utf-8'), 'utf-8');
42
                    $sourceTextAfterShortcode = mb_substr(
43
                        $state,
44
                        $offset + $lengthOfShortcode + mb_strlen($postfixMatch[0], 'utf-8'),
45
                        null,
46
                        'utf-8'
47
                    );
48
                }
49
50
                return $sourceTextBeforeShortcode.$r->getReplacement().$sourceTextAfterShortcode;
51
            },
52
            $text
53
        );
54
55
        $event->setResult($result);
56
    }
57
}
58