• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

heimrichhannot / contao-utils-bundle / 15017483614

14 May 2025 09:41AM UTC coverage: 25.302% (-54.2%) from 79.482%
15017483614

Pull #96

github

koertho
backport anonymize utils
Pull Request #96: Backport anonymize utils

6 of 7 new or added lines in 2 files covered. (85.71%)

248 existing lines in 22 files now uncovered.

1488 of 5881 relevant lines covered (25.3%)

1.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/Twig/StringExtension.php
1
<?php
2

3
/*
4
 * Copyright (c) 2021 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\Twig;
10

11
use Contao\Controller;
12
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
13
use HeimrichHannot\UtilsBundle\String\AnonymizerUtil;
14
use Twig\Extension\AbstractExtension;
15
use Twig\TwigFilter;
16

17
class StringExtension extends AbstractExtension
18
{
19
    /**
20
     * @var AnonymizerUtil
21
     */
22
    private $anonymizerUtil;
23
    /**
24
     * @var ContaoFrameworkInterface
25
     */
26
    private $framework;
27

28
    public function __construct(AnonymizerUtil $anonymizerUtil, ContaoFrameworkInterface $framework)
29
    {
30
        $this->anonymizerUtil = $anonymizerUtil;
3✔
31
        $this->framework = $framework;
3✔
32
    }
33

34
    /**
35
     * Get list of twig filters.
36
     *
37
     * @return array|\Twig_SimpleFilter[]
38
     */
39
    public function getFilters()
40
    {
41
        return [
1✔
42
            new TwigFilter('autolink', [$this, 'autolink'], ['deprecated' => true]),
1✔
43
            new TwigFilter('anonymize_email', [$this, 'anonymizeEmail']),
1✔
44
            new TwigFilter('replace_inserttag', [$this, 'replaceInsertTag'], ['deprecated' => true]),
1✔
45
        ];
1✔
46
    }
47

48
    public function autolink(string $text, array $options = []): string
49
    {
UNCOV
50
        return preg_replace_callback('@(?P<url>(?:http(s)?://)[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#\[\]\@!$&\'()*+,;=]+)@i', function ($matches) use ($options) {
×
UNCOV
51
            if (!isset($matches['url'])) {
×
UNCOV
52
                return '';
×
53
            }
54

UNCOV
55
            return '<a'.(isset($options['blank']) && $options['blank'] ? ' target=" _blank"' : '').' href="'.$matches['url'].'">'.$matches['url'].'</a>';
×
UNCOV
56
        }, $text, $options['limit'] ?? -1);
×
57
    }
58

59
    public function anonymizeEmail(string $text): string
60
    {
61
        return $this->anonymizerUtil->anonymizeEmail($text);
1✔
62
    }
63

64
    public function replaceInsertTag(string $text, bool $cache = true)
65
    {
66
        $this->framework->initialize();
1✔
67

68
        return $this->framework->getAdapter(Controller::class)->replaceInsertTags($text, $cache);
1✔
69
    }
70
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc