Passed
Pull Request — master (#27)
by Billie
03:41 queued 01:18
created

HtmlStripperExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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 of twig less than ^1.0.0 of twig, and Twig_Filter on versions of
34
     * twig ^2.0.0
35
     *
36
     * @return \Twig_SimpleFilter[]|\Twig_Filter[]
37
     */
38
    public function getFilters()
39
    {
40
        $filters = [];
41
42
        if (class_exists('\Twig_Filter')) {
43
            $filters[] = new \Twig_Filter(
44
                'html_strip',
45
                [$this->htmlStripper, 'toText']
46
            );
47
        } else {
48
            // 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...
49
            $filters[] = new \Twig_SimpleFilter(
50
                'html_strip',
51
                [$this->htmlStripper, 'toText']
52
            );
53
        }
54
55
        return $filters;
56
    }
57
58
    /**
59
     * A convenient name for this extension.
60
     *
61
     * BC Twig ^v1.0.0
62
     *
63
     * @return string
64
     */
65 1
    public function getName()
66
    {
67 1
        return 'html_stripper';
68
    }
69
70
    /**
71
     * This is the extension itself, it's mostly a small wrapper around the actual parser.
72
     *
73
     * @see        https://github.com/PurpleBooth/htmlstrip
74
     * @deprecated v1.1.0 This will be removed in the future, please use the PurpleBooth/htmlstrip library
75
     *
76
     * @param string $html
77
     *
78
     * @return string
79
     */
80 1
    public function toText($html)
81
    {
82 1
        return $this->htmlStripper->toText($html);
83
    }
84
}
85