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.
Test Setup Failed
Push — master ( aeed96...e25155 )
by Malte
03:19
created

__invoke()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 41
rs 9.536
ccs 25
cts 25
cp 1
cc 4
nc 2
nop 1
crap 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
    /**
16
     * @param ReplaceShortcodesEvent $event
17
     */
18 4
    public function __invoke(ReplaceShortcodesEvent $event)
19
    {
20
        /* The text still containing shortcodes */
21 4
        $text = $event->getText();
22
23
        /* The generated ReplacedShortcode instances. Each ReplacedShortcode contains the Shortcode string in the
24
          original form, it's offset and it's replacement string. */
25 4
        $replacements = $event->getReplacements();
26 4
        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...
27 4
            return;
28
        }
29
30
        /* Do the replacements from end to back to front, so the offsets remain valid. */
31 2
        $replacements_reversed = array_reverse($replacements);
32 2
        $result = array_reduce(
33 2
            $replacements_reversed,
34 1
            function ($state, ReplacedShortcode $r) {
35 2
                $offset = $r->getOffset();
36 2
                $lengthOfShortcode = mb_strlen($r->getText(), 'utf-8');
37 2
                $sourceTextBeforeShortcode = mb_substr($state, 0, $offset, 'utf-8');
38 2
                $sourceTextAfterShortcode = mb_substr($state, $offset + $lengthOfShortcode, null, 'utf-8');
39
40
                if (
41 2
                    preg_match('~(\s*<p>\s*)$~', $sourceTextBeforeShortcode, $prefixMatch)
42 2
                    && preg_match('~(^\s*</p>\s*)~', $sourceTextAfterShortcode, $postfixMatch)
43
                ) {
44 2
                    $sourceTextBeforeShortcode = mb_substr($state, 0, $offset - mb_strlen($prefixMatch[0], 'utf-8'), 'utf-8');
45 2
                    $sourceTextAfterShortcode = mb_substr(
46 2
                        $state,
47 2
                        $offset + $lengthOfShortcode + mb_strlen($postfixMatch[0], 'utf-8'),
48 2
                        null,
49 2
                        'utf-8'
50
                    );
51
                }
52
53 2
                return $sourceTextBeforeShortcode.$r->getReplacement().$sourceTextAfterShortcode;
54 2
            },
55 2
            $text
56
        );
57
58 2
        $event->setResult($result);
59 2
    }
60
}
61