Completed
Pull Request — master (#48)
by Vladimir
03:17
created

TextExtension   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
lcom 0
cbo 3
dl 0
loc 92
ccs 6
cts 6
cp 1
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 4 1
A getFilters() 0 8 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 Twig_Environment;
6
use Twig_Extension;
7
use Twig_SimpleFilter;
8
9
/**
10
 * This file is part of Twig.
11
 *
12
 * (c) 2009 Fabien Potencier
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 *
17
 * @author Henrik Bjornskov <[email protected]>
18
 */
19
class TextExtension extends Twig_Extension
20
{
21
    /**
22
     * Returns a list of filters.
23
     *
24
     * @return array
25
     */
26 2
    public function getFilters()
27
    {
28
        return array(
29 2
            new Twig_SimpleFilter('summary', array($this, 'twig_summary_filter')),
30 2
            new Twig_SimpleFilter('truncate', array($this, 'twig_truncate_filter'), array('needs_environment' => true)),
31 2
            new Twig_SimpleFilter('wordwrap', array($this, 'twig_wordwrap_filter'), array('needs_environment' => true)),
32
        );
33
    }
34
35
    /**
36
     * Name of this extension.
37
     *
38
     * @return string
39
     */
40 14
    public function getName()
41
    {
42 14
        return 'Text';
43
    }
44
45
    public function twig_summary_filter($value, $paragraphCount = 1)
0 ignored issues
show
Coding Style introduced by
function twig_summary_filter() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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...
46
    {
47
        if (function_exists('simplexml_load_string'))
48
        {
49
            $content = simplexml_load_string('<html>' . $value . '</html>');
50
            $count = min($paragraphCount, $content->count());
51
            $children = $content->children();
52
53
            $summary = '';
54
55
            for ($i = 0; $i < $count; ++$i)
56
            {
57
                $summary .= $children[$i]->asXml();
58
            }
59
60
            return $summary;
61
        }
62
63
        return $value;
64
    }
65
66
    public function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
0 ignored issues
show
Coding Style introduced by
function twig_truncate_filter() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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...
67
    {
68
        if (mb_strlen($value, $env->getCharset()) > $length)
69
        {
70
            if ($preserve)
71
            {
72
                // If breakpoint is on the last word, return the value without separator.
73
                if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset())))
74
                {
75
                    return $value;
76
                }
77
78
                $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...
79
            }
80
81
            return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator;
82
        }
83
84
        return $value;
85
    }
86
87
    public function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
0 ignored issues
show
Coding Style introduced by
function twig_wordwrap_filter() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

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