Failed Conditions
Pull Request — master (#20)
by Arnold
01:53
created

TextExtension   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Test Coverage

Coverage 97.01%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 64
c 2
b 0
f 0
dl 0
loc 207
ccs 65
cts 67
cp 0.9701
rs 10
wmc 29

10 Methods

Rating   Name   Duplication   Size   Complexity  
A less() 0 8 3
A truncate() 0 7 3
A linkifyOther() 0 14 4
A line() 0 9 3
A linkifyMail() 0 8 1
A getName() 0 3 1
A linkifyHttp() 0 13 4
A paragraph() 0 8 2
B linkify() 0 33 7
A getFilters() 0 8 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jasny\Twig;
4
5
use Twig\Extension\AbstractExtension;
6
use Twig\TwigFilter;
7
8
/**
9
 * Text functions for Twig.
10
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
11
class TextExtension extends AbstractExtension
12
{
13
    /**
14
     * Return extension name
15
     *
16
     * @return string
17
     */
18
    public function getName()
19
    {
20
        return 'jasny/text';
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
26 25
    public function getFilters()
27
    {
28
        return [
29 25
            new TwigFilter('paragraph', [$this, 'paragraph'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
30 25
            new TwigFilter('line', [$this, 'line']),
31 25
            new TwigFilter('less', [$this, 'less'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
32 25
            new TwigFilter('truncate', [$this, 'truncate'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
33 25
            new TwigFilter('linkify', [$this, 'linkify'], ['pre_escape' => 'html', 'is_safe' => ['html']])
34
        ];
35
    }
36
37
    /**
38
     * Add paragraph and line breaks to text.
39
     *
40
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
41
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
42
     */
43 2
    public function paragraph($value)
44
    {
45 2
        if (!isset($value)) {
46 1
            return null;
47
        }
48
49 1
        return '<p>' . preg_replace(['~\n(\s*)\n\s*~', '~(?<!</p>)\n\s*~'], ["</p>\n\$1<p>", "<br>\n"], trim($value)) .
50 1
            '</p>';
51
    }
52
53
    /**
54
     * Get a single line
55
     *
56
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @param int    $line   Line number (starts at 1)
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 3 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
58
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
59
     */
60 4
    public function line($value, $line = 1)
61
    {
62 4
        if (!isset($value)) {
63 1
            return null;
64
        }
65
66 3
        $lines = explode("\n", $value);
67
68 3
        return isset($lines[$line - 1]) ? $lines[$line - 1] : null;
69
    }
70
71
    /**
72
     * Cut of text on a pagebreak.
73
     *
74
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
75
     * @param string $replace
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
76
     * @param string $break
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
77
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
78
     */
79 4
    public function less($value, $replace = '...', $break = '<!-- pagebreak -->')
80
    {
81 4
        if (!isset($value)) {
82 1
            return null;
83
        }
84
85 3
        $pos = stripos($value, $break);
86 3
        return $pos === false ? $value : substr($value, 0, $pos) . $replace;
87
    }
88
89
    /**
90
     * Cut of text if it's to long.
91
     *
92
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
93
     * @param int    $length
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
94
     * @param string $replace
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
95
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
96
     */
97 4
    public function truncate($value, $length, $replace = '...')
98
    {
99 4
        if (!isset($value)) {
100 1
            return null;
101
        }
102
103 3
        return strlen($value) <= $length ? $value : substr($value, 0, $length - strlen($replace)) . $replace;
104
    }
105
106
    /**
107
     * Linkify a HTTP(S) link.
108
     *
109
     * @param string $protocol  'http' or 'https'
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
110
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
111
     * @param array  $links     OUTPUT
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 5 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
112
     * @param string $attr
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
113
     * @param string $mode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
114
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
115
     */
116 6
    protected function linkifyHttp($protocol, $text, array &$links, $attr, $mode)
117
    {
118 6
        $regexp = $mode != 'all'
119 5
            ? '~(?:(https?)://([^\s<>]+)|(?<!\w@)\b(www\.[^\s<>]+?\.[^\s<>]+))(?<![\.,:;\?!\'"\|])~i'
120 6
            : '~(?:(https?)://([^\s<>]+)|(?<!\w@)\b([^\s<>@]+?\.[^\s<>]+)(?<![\.,:]))~i';
121
122
        return preg_replace_callback($regexp, function ($match) use ($protocol, &$links, $attr) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
123 5
            if ($match[1]) $protocol = $match[1];
0 ignored issues
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
124 5
            $link = $match[2] ?: $match[3];
125
126 5
            return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . '://' . $link . '">'
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
127 5
                . rtrim($link, '/') . '</a>') . '>';
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
128 6
        }, $text);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
129
    }
130
131
    /**
132
     * Linkify a mail link.
133
     *
134
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
135
     * @param array  $links     OUTPUT
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
136
     * @param string $attr
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
137
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
138
     */
139 5
    protected function linkifyMail($text, array &$links, $attr)
140
    {
141 5
        $regexp = '~([^\s<>]+?@[^\s<>]+?\.[^\s<>]+)(?<![\.,:;\?!\'"\|])~';
142
143
        return preg_replace_callback($regexp, function ($match) use (&$links, $attr) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
144 4
            return '<' . array_push($links, '<a' . $attr . ' href="mailto:' . $match[1] . '">' . $match[1] . '</a>')
145 4
                . '>';
146 5
        }, $text);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
147
    }
148
149
150
    /**
151
     * Linkify a link.
152
     *
153
     * @param string $protocol
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
154
     * @param string $text
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
155
     * @param array  $links     OUTPUT
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 5 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
156
     * @param string $attr
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
157
     * @param string $mode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
158
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
159
     */
160 4
    protected function linkifyOther($protocol, $text, array &$links, $attr, $mode)
161
    {
162 4
        if (strpos($protocol, ':') === false) {
163 4
            $protocol .= in_array($protocol, ['ftp', 'tftp', 'ssh', 'scp']) ? '://' : ':';
164
        }
165
166 4
        $regexp = $mode != 'all'
167 2
            ? '~' . preg_quote($protocol, '~') . '([^\s<>]+)(?<![\.,:;\?!\'"\|])~i'
168 4
            : '~([^\s<>]+)(?<![\.,:])~i';
169
170
        return preg_replace_callback($regexp, function ($match) use ($protocol, &$links, $attr) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
171 4
            return '<' . array_push($links, '<a' . $attr . ' href="' . $protocol . $match[1] . '">' . $match[1]
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
172 4
                . '</a>') . '>';
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
173 4
        }, $text);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
174
    }
175
176
    /**
177
     * Turn all URLs in clickable links.
178
     *
179
     * @param string $value
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
180
     * @param array  $protocols   'http'/'https', 'mail' and also 'ftp', 'scp', 'tel', etc
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 3 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
181
     * @param array  $attributes  HTML attributes for the link
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 2 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
182
     * @param string $mode        normal or all
0 ignored issues
show
Coding Style introduced by
Expected 7 spaces after parameter name; 8 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
183
     * @return string
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
184
     */
185 11
    public function linkify($value, $protocols = ['http', 'mail'], array $attributes = [], $mode = 'normal')
186
    {
187 11
        if (!isset($value)) {
188 1
            return null;
189
        }
190
191
        // Link attributes
192 10
        $attr = '';
193 10
        foreach ($attributes as $key => $val) {
194 1
            $attr .= ' ' . $key . '="' . htmlentities($val) . '"';
195
        }
196
197 10
        $links = [];
198
199
        // Extract existing links and tags
200
        $text = preg_replace_callback('~(<a .*?>.*?</a>|<.*?>)~i', function ($match) use (&$links) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
201 1
            return '<' . array_push($links, $match[1]) . '>';
202 10
        }, $value);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
203
204
        // Extract text links for each protocol
205 10
        foreach ((array)$protocols as $protocol) {
206 10
            switch ($protocol) {
207 10
                case 'http':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
208 10
                case 'https':   $text = $this->linkifyHttp($protocol, $text, $links, $attr, $mode); break;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
209 9
                case 'mail':    $text = $this->linkifyMail($text, $links, $attr); break;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
210 4
                default:        $text = $this->linkifyOther($protocol, $text, $links, $attr, $mode); break;
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
Coding Style introduced by
Closing brace must be on a line by itself
Loading history...
211
            }
212
        }
213
214
        // Insert all link
215
        return preg_replace_callback('/<(\d+)>/', function ($match) use (&$links) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
216 10
            return $links[$match[1] - 1];
217 10
        }, $text);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
218
    }
219
}
220