Completed
Push — master ( b67fb9...4a9b36 )
by Vladimir
03:31 queued 01:00
created

Twig_Extensions_Extension_Text::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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Twig.
5
 *
6
 * (c) 2009 Fabien Potencier
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Henrik Bjornskov <[email protected]>
12
 */
13
class Twig_Extensions_Extension_Text extends Twig_Extension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
14
{
15
    /**
16
     * Returns a list of filters.
17
     *
18
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Twig_SimpleFilter[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
19
     */
20 1
    public function getFilters ()
21
    {
22
        return array(
23 1
            new Twig_SimpleFilter('summary',  'twig_summary_filter'),
24 1
            new Twig_SimpleFilter('truncate', 'twig_truncate_filter', array('needs_environment' => true)),
25 1
            new Twig_SimpleFilter('wordwrap', 'twig_wordwrap_filter', array('needs_environment' => true)),
26 1
        );
27
    }
28
29
    /**
30
     * Name of this extension.
31
     *
32
     * @return string
33
     */
34 1
    public function getName ()
35
    {
36 1
        return 'Text';
37
    }
38
}
39
40
function twig_summary_filter ($value, $paragraphCount = 1)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
41
{
42
    if (function_exists('simplexml_load_string'))
43
    {
44
        $content = simplexml_load_string('<html>' . $value . '</html>');
45
        $count = min($paragraphCount, $content->count());
46
        $children = $content->children();
47
48
        $summary = "";
49
50
        for ($i = 0; $i < $count; $i++)
51
        {
52
            $summary .= $children[$i]->asXml();
53
        }
54
55
        return $summary;
56
    }
57
58
    return $value;
59
}
60
61
function twig_truncate_filter (Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
62
{
63
    if (mb_strlen($value, $env->getCharset()) > $length)
64
    {
65
        if ($preserve)
66
        {
67
            // If breakpoint is on the last word, return the value without separator.
68
            if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset())))
69
            {
70
                return $value;
71
            }
72
73
            $length = $breakpoint;
74
        }
75
76
        return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator;
77
    }
78
79
    return $value;
80
}
81
82
function twig_wordwrap_filter (Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
0 ignored issues
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

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