HttpMessageMarkupExtension::markup()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 10
cp 0
rs 9.584
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Http\HttplugBundle\Collector\Twig;
6
7
use Twig\Extension\AbstractExtension;
8
use Twig\TwigFilter;
9
10
/**
11
 * @author Tobias Nyholm <[email protected]>
12
 */
13
class HttpMessageMarkupExtension extends AbstractExtension
14
{
15
    /**
16
     * {@inheritdoc}
17
     *
18
     * @return array
19
     */
20 1
    public function getFilters()
21
    {
22
        return [
23 1
            new TwigFilter('httplug_markup', [$this, 'markup'], ['is_safe' => ['html']]),
24
        ];
25
    }
26
27
    /**
28
     * @param string $message http message
29
     */
30
    public function markup($message)
31
    {
32
        @trigger_error('"httplug_markup" twig extension is deprecated since version 1.17 and will be removed in 2.0. Use "@Httplug/http_message.html.twig" template instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
33
34
        $safeMessage = htmlentities($message);
35
        $parts = preg_split('|\\r?\\n\\r?\\n|', $safeMessage, 2);
36
37
        if (!isset($parts[1])) {
38
            // This is not a HTTP message
39
            return $safeMessage;
40
        }
41
42
        if (empty($parts[1])) {
43
            $parts[1] = '(This message has no captured body)';
44
        }
45
46
        // make header names bold
47
        $headers = preg_replace("|\n(.*?): |si", "\n<b>$1</b>: ", $parts[0]);
48
49
        return sprintf("%s\n\n<div class='httplug-http-body httplug-hidden'>%s</div>", $headers, $parts[1]);
50
    }
51
52
    public function getName()
53
    {
54
        return 'httplug.message_markup';
55
    }
56
}
57