StripAttributesExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 4
eloc 8
c 3
b 1
f 0
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 4 1
A removeAttributesFromTags() 0 7 2
A __construct() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Twig\Extension;
17
18
use Twig\Extension\AbstractExtension;
19
use Twig\TwigFilter;
20
use Veslo\AppBundle\Html\Tag\AttributeRemover;
21
22
/**
23
 * Removes any attributes from tags in html string
24
 */
25
class StripAttributesExtension extends AbstractExtension
26
{
27
    /**
28
     * Removes attributes from tags in html string
29
     *
30
     * @var AttributeRemover
31
     */
32
    private $attributeRemover;
33
34
    /**
35
     * StripAttributesExtension constructor.
36
     *
37
     * @param AttributeRemover $attributeRemover Removes attributes from tags in html string
38
     */
39
    public function __construct(AttributeRemover $attributeRemover)
40
    {
41
        $this->attributeRemover = $attributeRemover;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getFilters()
48
    {
49
        return [
50
            new TwigFilter('stripattributes', [$this, 'removeAttributesFromTags']),
51
        ];
52
    }
53
54
    /**
55
     * Removes any attributes from tags in html string
56
     *
57
     * @param string $html Html string
58
     *
59
     * @return string|null
60
     */
61
    public function removeAttributesFromTags(?string $html): ?string
62
    {
63
        if (empty($html)) {
64
            return $html;
65
        }
66
67
        return $this->attributeRemover->removeAttributes($html);
68
    }
69
}
70