Completed
Pull Request — master (#27)
by Billie
02:23
created

HtmlStripperExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 38.89%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 71
ccs 7
cts 18
cp 0.3889
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getName() 0 4 1
A toText() 0 4 1
A getFilters() 0 20 2
1
<?php
2
3
/*
4
 * Copyright (C) 2016 Billie Thompson
5
 *
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license.  See the LICENSE file for details.
8
 */
9
10
namespace PurpleBooth;
11
12
use ReflectionClass;
13
14
/**
15
 * This is sets up the twig extension.
16
 */
17
class HtmlStripperExtension extends \Twig_Extension
18
{
19
    /**
20
     * @var HtmlStripper
21
     */
22
    private $htmlStripper;
23
24
    /**
25
     * HtmlStripperExtension constructor.
26
     */
27 3
    public function __construct()
28
    {
29 3
        $this->htmlStripper = new HtmlStripperImplementation();
30 3
    }
31
32
    /**
33
     * This gets an array of the filters that this extension provides.
34
     *
35
     * It'll return Twig_SimpleFilter on versions of twig less than ^1.0.0 of twig, and Twig_Filter on versions of
36
     * twig ^2.0.0
37
     *
38
     * @return \Twig_SimpleFilter[]|\Twig_Filter[]
39
     */
40
    public function getFilters()
41
    {
42
        $filters = [];
43
44
        $reflection = new ReflectionClass('\Twig_Filter');
45
        if ($reflection->isInstantiable()) {
46
            $filters[] = new \Twig_Filter(
47
                'html_strip',
48
                [$this->htmlStripper, 'toText']
49
            );
50
        } else {
51
            // BC Twig ^1.0.0
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
52
            $filters[] = new \Twig_SimpleFilter(
53
                'html_strip',
54
                [$this->htmlStripper, 'toText']
55
            );
56
        }
57
58
        return $filters;
59
    }
60
61
    /**
62
     * A convenient name for this extension.
63
     *
64
     * BC Twig ^v1.0.0
65
     *
66
     * @return string
67
     */
68 1
    public function getName()
69
    {
70 1
        return 'html_stripper';
71
    }
72
73
    /**
74
     * This is the extension itself, it's mostly a small wrapper around the actual parser.
75
     *
76
     * @see        https://github.com/PurpleBooth/htmlstrip
77
     * @deprecated v1.1.0 This will be removed in the future, please use the PurpleBooth/htmlstrip library
78
     *
79
     * @param string $html
80
     *
81
     * @return string
82
     */
83 1
    public function toText($html)
84
    {
85 1
        return $this->htmlStripper->toText($html);
86
    }
87
}
88