Completed
Push — master ( 06332b...67b389 )
by Vladimir
02:32
created

TextExtension   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 15.91%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
lcom 0
cbo 4
dl 0
loc 92
rs 10
c 2
b 0
f 0
ccs 7
cts 44
cp 0.1591

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getFilters() 0 8 1
A getName() 0 4 1
A twig_summary_filter() 0 20 3
A twig_truncate_filter() 0 20 4
B twig_wordwrap_filter() 0 23 4
1
<?php
2
3
namespace allejo\stakx\Twig;
4
5
use allejo\stakx\Utilities\HtmlUtils;
6
use Twig_Environment;
7
use Twig_Extension;
8
use Twig_SimpleFilter;
9
10
/**
11
 * This file is part of Twig.
12
 *
13
 * (c) 2009 Fabien Potencier
14
 *
15
 * For the full copyright and license information, please view the LICENSE
16
 * file that was distributed with this source code.
17
 *
18
 * @author Henrik Bjornskov <[email protected]>
19
 */
20
class TextExtension extends Twig_Extension
21
{
22
    /**
23
     * Returns a list of filters.
24
     *
25
     * @return Twig_SimpleFilter[]
26
     */
27 1
    public function getFilters()
28
    {
29
        return array(
30 1
            new Twig_SimpleFilter('summary', array($this, 'twig_summary_filter')),
31 1
            new Twig_SimpleFilter('truncate', array($this, 'twig_truncate_filter'), array('needs_environment' => true)),
32 1
            new Twig_SimpleFilter('wordwrap', array($this, 'twig_wordwrap_filter'), array('needs_environment' => true)),
33 1
        );
34
    }
35
36
    /**
37
     * Name of this extension.
38
     *
39
     * @return string
40
     */
41 14
    public function getName()
42
    {
43 14
        return 'Text';
44
    }
45
46
    public function twig_summary_filter($value, $paragraphCount = 1)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
47
    {
48
        if (!extension_loaded('dom'))
49
        {
50
            @trigger_error('The DOM Extension is not loaded and is necessary for the "summary" Twig filter.', E_WARNING);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
51
            return $value;
52
        }
53
54
        $dom = new \DOMDocument();
55
        $paragraphs = HtmlUtils::htmlXPath($dom, $value, sprintf('//body/p[position() <= %d]', $paragraphCount));
56
57
        $summary = '';
58
59
        foreach ($paragraphs as $paragraph)
60
        {
61
            $summary .= $dom->saveHTML($paragraph);
62
        }
63
64
        return $summary;
65
    }
66
67
    public function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
68
    {
69
        if (mb_strlen($value, $env->getCharset()) > $length)
70
        {
71
            if ($preserve)
72
            {
73
                // If breakpoint is on the last word, return the value without separator.
74
                if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset())))
75
                {
76
                    return $value;
77
                }
78
79
                $length = $breakpoint;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $length. This often makes code more readable.
Loading history...
80
            }
81
82
            return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator;
83
        }
84
85
        return $value;
86
    }
87
88
    public function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
89
    {
90
        $sentences = array();
91
92
        $previous = mb_regex_encoding();
93
        mb_regex_encoding($env->getCharset());
94
95
        $pieces = mb_split($separator, $value);
96
        mb_regex_encoding($previous);
97
98
        foreach ($pieces as $piece)
99
        {
100
            while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length)
101
            {
102
                $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
103
                $piece = mb_substr($piece, $length, 2048, $env->getCharset());
104
            }
105
106
            $sentences[] = $piece;
107
        }
108
109
        return implode($separator, $sentences);
110
    }
111
}
112