Completed
Pull Request — master (#27)
by Billie
05:12
created

HtmlStripperExtension   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 41.18%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 5
c 3
b 1
f 0
lcom 1
cbo 5
dl 0
loc 69
ccs 7
cts 17
cp 0.4118
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFilters() 0 19 2
A getName() 0 4 1
A toText() 0 4 1
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
/**
13
 * This is sets up the twig extension.
14
 */
15
class HtmlStripperExtension extends \Twig_Extension
16
{
17
    /**
18
     * @var HtmlStripper
19
     */
20
    private $htmlStripper;
21
22
    /**
23
     * HtmlStripperExtension constructor.
24
     */
25 3
    public function __construct()
26
    {
27 3
        $this->htmlStripper = new HtmlStripperImplementation();
28 3
    }
29
30
    /**
31
     * This gets an array of the filters that this extension provides.
32
     *
33
     * It'll return Twig_SimpleFilter on versions less than ^1.0.0 of twig, and Twig_Filter with version ^2.0.0
34
     *
35
     * @return \Twig_SimpleFilter[]|\Twig_Filter[]
36
     */
37
    public function getFilters()
38
    {
39
        $filters = [];
40
41
        if (class_exists('\Twig_Filter')) {
42
            $filters[] = new \Twig_Filter(
43
                'html_strip',
44
                [$this->htmlStripper, 'toText']
45
            );
46
        } else {
47
            // 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...
48
            $filters[] = new \Twig_SimpleFilter(
49
                'html_strip',
50
                [$this->htmlStripper, 'toText']
51
            );
52
        }
53
54
        return $filters;
55
    }
56
57
    /**
58
     * A convenient name for this extension.
59
     *
60
     * Included for backwards
61
     *
62
     * @return string
63
     */
64 1
    public function getName()
65
    {
66 1
        return 'html_stripper';
67
    }
68
69
    /**
70
     * This is the extension itself, it's mostly a small wrapper around the actual parser.
71
     *
72
     * @see        https://github.com/PurpleBooth/htmlstrip
73
     * @deprecated v1.1.0 This will be removed in the future, please use the PurpleBooth/htmlstrip library
74
     *
75
     * @param string $html
76
     *
77
     * @return string
78
     */
79 1
    public function toText($html)
80
    {
81 1
        return $this->htmlStripper->toText($html);
82
    }
83
}
84