Text.pm

filter: Statementstmt 100% 261 / 261 Branchbran 97.2% 105 / 108 Conditioncond 85.0% 74 / 87 MC/DCmcdc 68.0% 32 / 47 Subroutinesub 100% 37 / 37 Podpod 75.0% 12 / 16 totaltotal 93.7% 521 / 556 CC 91 SCAR 23.3
CC
91
Cov
96%
parse_tweet
23.0
extract_urls_with_indices
17.0
is_valid_url
15.3
CRAP 10.3
↑ Summary Text/Configuration.pm »
1 package Twitter::Text;
2 4 ▶use 5.010000;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶no if $^V lt v5.13.9, 'warnings', 'utf8'; ## no critic (ValuesAndExpressions::ProhibitMismatchedOperators)
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 DEFAULT_TCO_URL_LENGTHS => {
10 short_url_length => 23,
11 },
12 MAX_WEIGHTENED_LENGTH => 280,
13 MAX_URL_LENGTH => 4096,
14 MAX_TCO_SLUG_LENGTH => 40,
15 URL_PROTOCOL_LENGTH => length 'https://',
16 4 };
17 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use List::Util qw(min);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use List::UtilsBy qw(nsort_by);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Net::IDN::Encode qw(domain_to_ascii);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use Twitter::Text::Configuration;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use Twitter::Text::Regexp;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Twitter::Text::Regexp::Emoji;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Unicode::Normalize qw(NFC);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 our $VERSION = "0.08";
28 4 our @EXPORT = (
29 # Extraction
30 qw(
31 extract_cashtags
32 extract_cashtags_with_indices
33 extract_hashtags
34 extract_hashtags_with_indices
35 extract_mentioned_screen_names
36 extract_mentioned_screen_names_with_indices
37 extract_mentions_or_lists_with_indices
38 extract_urls
39 extract_urls_with_indices
40 ),
41 # Validation
42 qw(
43 is_valid_hashtag
44 is_valid_list
45 is_valid_tweet
46 is_valid_url
47 is_valid_username
48 parse_tweet
49 ),
50 );
51  
52 24 ▶sub extract_emoji_with_indices {
Subroutine extract_emoji_with_indices: called CC=2 SCAR=6.9
Pod undocumented
53 24 my ($text) = @_;
54 24 my $emoji = [];
55  
56 24 ▶ while ($text =~ /($Twitter::Text::Regexp::Emoji::valid_emoji)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($Twitter::Text::Regexp::Emoji::valid_emoji)/g) 318 24 342
57 318 my $emoji_text = $1;
58 318 my $start_position = $-[1];
59 318 my $end_position = $+[1];
60 318 push @$emoji, {
61 emoji => $emoji_text,
62 indices => [ $start_position, $end_position ],
63 };
64 }
65 24 return $emoji;
66 }
67  
68 1 ▶sub _remove_overlapping_entities {
Subroutine _remove_overlapping_entities: called CC=4 SCAR=13.9
69 1 my ($entities) = @_;
70  
71 1 ▶ $entities = [ nsort_by { $_->{indices}->[0] } @$entities ];
Subroutine __ANON__: called CC=1 SCAR=0.0
72 # remove duplicates
73 1 my $ret = [];
74 1 my $prev;
75  
76 1 for my $entity (@$entities) {
77 4 ▶ unless ($prev && $prev->{indices}->[1] > $entity->{indices}->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prev and $prev->{"indices"}[1] > $entity->{"indices"}[0]) 2 2 4
Condition100%3/3
$prev and $prev->{"indices"}[1] > $entity->{"indices"}[0]
!ll&&!rl&&r
112
MC/DC100%2/2
$prev and $prev->{"indices"}[1] > $entity->{"indices"}[0]
$prev $prev->{"indices"}[1] > $entity->{"indices"}[0]
Truth table100%3/3
A and B
A= $prevB= $prev->{"indices"}[1] > $entity->{"indices"}[0]
ABresult
0X0
100
111
78 2 push @$ret, $entity;
79 }
80 4 $prev = $entity;
81 }
82 1 return $ret;
83 }
84  
85 8 ▶sub extract_cashtags {
Subroutine extract_cashtags: called CC=1 SCAR=0.0
Pod undocumented
86 8 my ($text) = @_;
87 8 return [ map { $_->{cashtag} } @{ extract_cashtags_with_indices($text) } ];
88 }
89  
90 11 ▶sub extract_cashtags_with_indices {
Subroutine extract_cashtags_with_indices: called CC=3 SCAR=11.0
Pod undocumented
91 11 my ($text) = @_;
92  
93 11 ▶ return [] unless $text =~ /\$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\$/ 1 10 11
94  
95 10 my $tags = [];
96  
97 10 ▶ while ($text =~ /($Twitter::Text::Regexp::valid_cashtag)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($Twitter::Text::Regexp::valid_cashtag)/g) 14 10 24
98 14 my ($before, $dollar, $cash_text) = ($2, $3, $4);
99 14 my $start_position = $-[3];
100 14 my $end_position = $+[4];
101 14 push @$tags, {
102 cashtag => $cash_text,
103 indices => [ $start_position, $end_position ],
104 };
105 }
106  
107 10 return $tags;
108 }
109  
110 75 ▶sub extract_hashtags {
Subroutine extract_hashtags: called CC=1 SCAR=0.0
Pod documented
111 75 my ($text) = @_;
112 75 return [ map { $_->{hashtag} } @{ extract_hashtags_with_indices($text) } ];
113 }
114  
115 85 ▶sub extract_hashtags_with_indices {
Subroutine extract_hashtags_with_indices: called CC=7 SCAR=19.5
Pod documented
116 85 my ($text, $options) = @_;
117  
118 85 ▶ return [] unless $text =~ /[##]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /[#\x{ff03}]/ 1 84 85
119  
120 84 ▶ $options->{check_url_overlap} = 1 unless exists $options->{check_url_overlap};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"check_url_overlap"} 83 1 84
121  
122 84 my $tags = [];
123  
124 84 ▶ while ($text =~ /($Twitter::Text::Regexp::valid_hashtag)/gp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($Twitter::Text::Regexp::valid_hashtag)/gp) 151 84 235
125 151 my ($before, $hash, $hash_text) = ($2, $3, $4);
126 151 my $start_position = $-[3];
127 151 my $end_position = $+[4];
128 151 my $after = ${^POSTMATCH};
129  
130 151 ▶ unless ($after =~ $Twitter::Text::Regexp::end_hashtag_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($after =~ /$Twitter::Text::Regexp::end_hashtag_match/) 149 2 151
131 149 push @$tags, {
132 hashtag => $hash_text,
133 indices => [ $start_position, $end_position ],
134 };
135 }
136 }
137  
138 84 ▶ if ($options->{check_url_overlap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"check_url_overlap"}) 83 1 84
139 83 my $urls = extract_urls_with_indices($text);
140  
141 83 ▶ if (@$urls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$urls) 1 82 83
142 1 $tags = [ @$tags, @$urls ];
143 # remove duplicates
144 1 $tags = _remove_overlapping_entities($tags);
145 # remove URL entities
146 1 $tags = [ grep { $_->{hashtag} } @$tags ];
147 }
148 }
149  
150 84 return $tags;
151 }
152  
153 27 ▶sub extract_mentioned_screen_names {
Subroutine extract_mentioned_screen_names: called CC=1 SCAR=0.0
Pod documented
154 27 my ($text) = @_;
155 27 return [ map { $_->{screen_name} } @{ extract_mentioned_screen_names_with_indices($text) } ];
156 }
157  
158 32 ▶sub extract_mentioned_screen_names_with_indices {
Subroutine extract_mentioned_screen_names_with_indices: called CC=4 SCAR=13.9
Pod documented
159 32 my ($text) = @_;
160  
161 32 ▶ return [] unless $text;
Branch: 1 branch
BranchTrueFalseTotal
unless $text 1 31 32
162  
163 31 my $possible_screen_name = [];
164  
165 31 for my $mention_or_list (@{ extract_mentions_or_lists_with_indices($text) }) {
166 32 ▶ next if length $mention_or_list->{list_slug};
Branch: 1 branch
BranchTrueFalseTotal
if length $mention_or_list->{"list_slug"} 1 31 32
167 push @$possible_screen_name, {
168 screen_name => $mention_or_list->{screen_name},
169 indices => $mention_or_list->{indices},
170 31 };
171 }
172  
173 31 return $possible_screen_name;
174 }
175  
176 37 ▶sub extract_mentions_or_lists_with_indices {
Subroutine extract_mentions_or_lists_with_indices: called CC=6 SCAR=17.9
Pod documented
177 37 my ($text) = @_;
178  
179 37 ▶ return [] unless $text =~ /[@@]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /[\@\x{ff20}]/ 1 36 37
180  
181 36 my $possible_entries = [];
182  
183 36 ▶ while ($text =~ /($Twitter::Text::Regexp::valid_mention_or_list)/gp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($Twitter::Text::Regexp::valid_mention_or_list)/gp) 42 36 78
184 42 my ($before, $at, $screen_name, $list_slug) = ($2, $3, $4, $5);
185 42 my $start_position = $-[4] - 1;
186 42 ▶ my $end_position = $+[ defined $list_slug ? 5 : 4 ];
Branch: 1 branch
BranchTrueFalseTotal
defined $list_slug ? : 4 38 42
187 42 my $after = ${^POSTMATCH};
188  
189 42 ▶ unless ($after =~ $Twitter::Text::Regexp::end_mention_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($after =~ /$Twitter::Text::Regexp::end_mention_match/) 38 4 42
190 38 ▶ push @$possible_entries, {
Condition100%2/2
$list_slug || ''
l!l
434
MC/DC100%1/1
$list_slug || ''
$list_slug
Truth table100%2/2
A
A= $list_slug
Aresult
11
00
191 screen_name => $screen_name,
192 list_slug => $list_slug || '',
193 indices => [ $start_position, $end_position ],
194 };
195 }
196 }
197 36 return $possible_entries;
198 }
199  
200 1665 ▶sub extract_urls {
Subroutine extract_urls: called CC=1 SCAR=0.0
Pod documented
201 1665 my ($text) = @_;
202 1665 my $urls = extract_urls_with_indices($text);
203 1665 return [ map { $_->{url} } @$urls ];
204 }
205  
206 1813 ▶sub extract_urls_with_indices {
Subroutine extract_urls_with_indices: called CC=17 SCAR=28.3
Pod documented
207 1813 my ($text, $options) = @_;
208 1813 ▶ $options ||= {
Condition100%2/2
$options ||= {"extract_url_without_protocol", 1}
l!l
11812
MC/DC100%1/1
$options ||= {"extract_url_without_protocol", 1}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
209 extract_url_without_protocol => 1,
210 };
211  
212 1813 ▶ return [] unless $text && ($options->{extract_url_without_protocol} ? $text =~ /\./ : $text =~ /:/);
Branch: 2 branches
BranchTrueFalseTotal
unless $text and $options->{"extract_url_without_protocol"} ? $text =~ /\./ : $text =~ /:/ 106 1707 1813
$options->{'extract_url_without_protocol'} ? : 1811 1 1812
Condition100%3/3
$text and $options->{"extract_url_without_protocol"} ? $text =~ /\./ : $text =~ /:/
!ll&&!rl&&r
11051707
MC/DC100%2/2
$text and $options->{"extract_url_without_protocol"} ? $text =~ /\./ : $text =~ /:/
$text $options->{"extract_url_without_protocol"} ? $text =~ /\./ : $text =~ /:/
Truth table100%3/3
A and B
A= $textB= $options->{"extract_url_without_protocol"} ? $text =~ /\./ : $text =~ /:/
ABresult
0X0
100
111
213  
214 1707 my $urls = [];
215  
216 1707 ▶ while ($text =~ /($Twitter::Text::Regexp::valid_url)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /($Twitter::Text::Regexp::valid_url)/g) 1765 1707 3472
217 1765 my $before = $3;
218 1765 my $url = $4;
219 1765 my $protocol = $5;
220 1765 my $domain = $6;
221 1765 my $path = $8;
222 1765 my ($start, $end) = ($-[4], $+[4]);
223  
224 1765 ▶ if (!$protocol) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $protocol) { } 59 1706 1765
225 59 ▶ next if !$options->{extract_url_without_protocol} || $before =~ $Twitter::Text::Regexp::invalid_url_without_protocol_preceding_chars;
Branch: 1 branch
BranchTrueFalseTotal
if not $options->{"extract_url_without_protocol"} or $before =~ /$Twitter::Text::Regexp::invalid_url_without_protocol_preceding_chars/ 15 44 59
Condition66%2/3
not $options->{"extract_url_without_protocol"} or $before =~ /$Twitter::Text::Regexp::invalid_url_without_protocol_preceding_chars/
l!l&&r!l&&!r
01544
MC/DC50%1/2
not $options->{"extract_url_without_protocol"} or $before =~ /$Twitter::Text::Regexp::invalid_url_without_protocol_preceding_chars/
not $options->{"extract_url_without_protocol"} $before =~ /$Twitter::Text::Regexp::invalid_url_without_protocol_preceding_chars/
Truth table66%2/3
not(A) or B
A= not $options->{"extract_url_without_protocol"}B= $before =~ /$Twitter::Text::Regexp::invalid_url_without_protocol_preceding_chars/
ABresult
1X1
011
000
226 44 my $last_url;
227  
228 44 ▶ while ($domain =~ /($Twitter::Text::Regexp::valid_ascii_domain)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($domain =~ /($Twitter::Text::Regexp::valid_ascii_domain)/g) 55 44 99
229 55 my $ascii_domain = $1;
230 55 ▶ next unless _is_valid_domain(length $url, $ascii_domain, $protocol);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_valid_domain(length $url, $ascii_domain, $protocol) 2 53 55
231 53 $last_url = {
232 url => $ascii_domain,
233 indices => [ $start + $-[0], $start + $+[0] ],
234 };
235 53 push @$urls, $last_url;
236 }
237  
238 # no ASCII-only domain found. Skip the entire URL
239 44 ▶ next unless $last_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $last_url 2 42 44
240  
241 # last_url only contains domain. Need to add path and query if they exist.
242 42 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 15 27 42
243 # last_url was not added. Add it to urls here.
244 15 my $last_url_after = $url;
245 15 $last_url_after =~ s/$domain/$last_url->{url}/e;
246 15 $last_url->{url} = $last_url_after;
247 15 $last_url->{indices}->[1] = $end;
248 }
249 } else {
250 1706 ▶ if ($url =~ /($Twitter::Text::Regexp::valid_tco_url)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ /($Twitter::Text::Regexp::valid_tco_url)/) 15 1691 1706
251 15 ▶ next if $2 && length $2 >= MAX_TCO_SLUG_LENGTH;
Branch: 1 branch
BranchTrueFalseTotal
if $2 and length $2 >= 40 1 14 15
Condition66%2/3
$2 and length $2 >= 40
!ll&&!rl&&r
0141
MC/DC50%1/2
$2 and length $2 >= 40
$2 length $2 >= 40
Truth table66%2/3
A and B
A= $2B= length $2 >= 40
ABresult
0X0
100
111
252 14 $url = $1;
253 14 $end = $start + length $url;
254 }
255  
256 1705 ▶ next unless _is_valid_domain(length $url, $domain, $protocol);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_valid_domain(length $url, $domain, $protocol) 10 1695 1705
257  
258 1695 push @$urls, {
259 url => $url,
260 indices => [ $start, $end ],
261 };
262  
263 }
264 }
265  
266 1707 return $urls;
267 }
268  
269 1760 ▶sub _is_valid_domain {
Subroutine _is_valid_domain: called CC=5 SCAR=16.1
270 1760 my ($url_length, $domain, $protocol) = @_;
271 1760 ▶ croak 'invalid empty domain' unless $domain;
Branch: 1 branch
BranchTrueFalseTotal
unless $domain 0 1760 1760
272  
273 1760 my $original_domain_length = length $domain;
274 1760 my $encoded_domain = eval { domain_to_ascii($domain) };
275  
276 1760 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 12 1748 1760
277 12 return 0;
278 }
279 1748 my $updated_domain_length = length $encoded_domain;
280 1748 ▶ $url_length += $updated_domain_length - $original_domain_length if $updated_domain_length > $original_domain_length;
Branch: 1 branch
BranchTrueFalseTotal
if $updated_domain_length > $original_domain_length 168 1580 1748
281 1748 ▶ $url_length += URL_PROTOCOL_LENGTH unless $protocol;
Branch: 1 branch
BranchTrueFalseTotal
unless $protocol 53 1695 1748
282 1748 return $url_length <= MAX_URL_LENGTH;
283 }
284  
285 8 ▶sub is_valid_tweet {
Subroutine is_valid_tweet: called CC=1 SCAR=0.0
Pod undocumented
286 8 my ($text) = @_;
287 return parse_tweet(
288 $text,
289 {
290 config => Twitter::Text::Configuration::V1,
291 }
292 8 )->{valid};
293 }
294  
295 8 ▶sub is_valid_hashtag {
Subroutine is_valid_hashtag: called CC=3 SCAR=11.0
Pod documented
296 8 my ($hashtag) = @_;
297  
298 8 ▶ return 0 unless length $hashtag;
Branch: 1 branch
BranchTrueFalseTotal
unless length $hashtag 1 7 8
299  
300 7 my $extracted = extract_hashtags($hashtag);
301 7 ▶ return scalar(@$extracted) == 1 && $extracted->[0] eq (substr $hashtag, 1);
Condition66%2/3
scalar @$extracted == 1 && $extracted->[0] eq substr($hashtag, 1)
!ll&&!rl&&r
106
MC/DC50%1/2
scalar @$extracted == 1 && $extracted->[0] eq substr($hashtag, 1)
scalar @$extracted == 1 $extracted->[0] eq substr($hashtag, 1)
Truth table66%2/3
A && B
A= scalar @$extracted == 1B= $extracted->[0] eq substr($hashtag, 1)
ABresult
0X0
100
111
302 }
303  
304 6 ▶sub is_valid_list {
Subroutine is_valid_list: called CC=4 SCAR=13.9
Pod documented
305 6 my ($username_list) = @_;
306 6 ▶ return !!($username_list =~ /\A($Twitter::Text::Regexp::valid_mention_or_list)\z/ && $2 eq '' && $5 && length $5);
Condition88%8/9
$username_list =~ /\A($Twitter::Text::Regexp::valid_mention_or_list)\z/ && $2 eq '' && $5 && length $5
!ll&&!rl&&r
501
$username_list =~ /\A($Twitter::Text::Regexp::valid_mention_or_list)\z/ and $2 eq "" and $5
!ll&&!rl&&r
411
$username_list =~ /\A($Twitter::Text::Regexp::valid_mention_or_list)\z/ and $2 eq ""
!ll&&!rl&&r
312
MC/DC75%3/4
$username_list =~ /\A($Twitter::Text::Regexp::valid_mention_or_list)\z/ && $2 eq '' && $5 && length $5
$username_list =~ /\A($Twitter::Text::Regexp::valid_mention_or_list)\z/ $2 eq "" $5 length $5
Truth table80%4/5
A and B and C && D
A= $username_list =~ /\A($Twitter::Text::Regexp::valid_mention_or_list)\z/B= $2 eq ""C= $5D= length $5
ABCDresult
0XXX0
10XX0
110X0
11100
11111
307 }
308  
309 33 ▶sub is_valid_url {
Subroutine is_valid_url: called CC=15 SCAR=27.3
Pod documented
310 33 my ($url, %opts) = @_;
311 33 ▶ my $unicode_domains = exists $opts{unicode_domains} ? $opts{unicode_domains} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'unicode_domains'} ? : 2 31 33
312 33 ▶ my $require_protocol = exists $opts{require_protocol} ? $opts{require_protocol} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'require_protocol'} ? : 6 27 33
313  
314 33 ▶ return 0 unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 2 31 33
315  
316 31 my ($url_parts) = $url =~ /($Twitter::Text::Regexp::validate_url_unencoded)/;
317 31 ▶ return 0 unless $url_parts && $url_parts eq $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url_parts and $url_parts eq $url 0 31 31
Condition33%1/3
$url_parts and $url_parts eq $url
!ll&&!rl&&r
0031
MC/DC0%0/2
$url_parts and $url_parts eq $url
$url_parts $url_parts eq $url
Truth table33%1/3
A and B
A= $url_partsB= $url_parts eq $url
ABresult
0X0
100
111
318  
319 31 my ($scheme, $authorithy, $path, $query, $fragment) = ($2, $3, $4, $5, $6);
320 31 ▶ return 0 unless ((!$require_protocol || (_valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) && $scheme =~ /\Ahttps?\Z/i))
Branch: 1 branch
BranchTrueFalseTotal
unless !$require_protocol || _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) && $scheme =~ /\Ahttps?\Z/i and _valid_match($path, $Twitter::Text::Regexp::validate_url_path) and _valid_match($query, $Twitter::Text::Regexp::validate_url_query, 1) and _valid_match($fragment, $Twitter::Text::Regexp::validate_url_fragment, 1) 3 28 31
Condition86%13/15
!$require_protocol || _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) && $scheme =~ /\Ahttps?\Z/i and _valid_match($path, $Twitter::Text::Regexp::validate_url_path) and _valid_match($query, $Twitter::Text::Regexp::validate_url_query, 1) and _valid_match($fragment, $Twitter::Text::Regexp::validate_url_fragment, 1)
!ll&&!rl&&r
2128
!$require_protocol || _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) && $scheme =~ /\Ahttps?\Z/i and _valid_match($path, $Twitter::Text::Regexp::validate_url_path) and _valid_match($query, $Twitter::Text::Regexp::validate_url_query, 1)
!ll&&!rl&&r
2029
!$require_protocol || _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) && $scheme =~ /\Ahttps?\Z/i and _valid_match($path, $Twitter::Text::Regexp::validate_url_path)
!ll&&!rl&&r
1129
not $require_protocol or _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) and $scheme =~ /\Ahttps?\Z/i
l!l&&r!l&&!r
5251
_valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) and $scheme =~ /\Ahttps?\Z/i
!ll&&!rl&&r
0125
MC/DC66%4/6
!$require_protocol || _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) && $scheme =~ /\Ahttps?\Z/i and _valid_match($path, $Twitter::Text::Regexp::validate_url_path) and _valid_match($query, $Twitter::Text::Regexp::validate_url_query, 1) and _valid_match($fragment, $Twitter::Text::Regexp::validate_url_fragment, 1)
not $require_protocol _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme) $scheme =~ /\Ahttps?\Z/i _valid_match($path, $Twitter::Text::Regexp::validate_url_path) _valid_match($query, $Twitter::Text::Regexp::validate_url_query, 1) _valid_match($fragment, $Twitter::Text::Regexp::validate_url_fragment, 1)
Truth table50%5/10
not(A) or B and C and D and E and F
A= not $require_protocolB= _valid_match($scheme, $Twitter::Text::Regexp::validate_url_scheme)C= $scheme =~ /\Ahttps?\Z/iD= _valid_match($path, $Twitter::Text::Regexp::validate_url_path)E= _valid_match($query, $Twitter::Text::Regexp::validate_url_query, 1)F= _valid_match($fragment, $Twitter::Text::Regexp::validate_url_fragment, 1)
ABCDEFresult
00XXXX0
010XXX0
1XX0XX0
0110XX0
1XX10X0
01110X0
1XX1100
0111100
1XX1111
0111111
321 && _valid_match($path, $Twitter::Text::Regexp::validate_url_path)
322 && _valid_match($query, $Twitter::Text::Regexp::validate_url_query, 1)
323 && _valid_match($fragment, $Twitter::Text::Regexp::validate_url_fragment, 1));
324  
325 28 ▶ return ($unicode_domains && _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_unicode_authority))
Condition100%9/9
$unicode_domains && _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_unicode_authority) || !$unicode_domains && _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_authority)
l!l&&r!l&&!r
1819
$unicode_domains and _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_unicode_authority)
!ll&&!rl&&r
2818
not $unicode_domains and _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_authority)
!ll&&!rl&&r
811
MC/DC75%3/4
$unicode_domains && _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_unicode_authority) || !$unicode_domains && _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_authority)
$unicode_domains _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_unicode_authority) not $unicode_domains _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_authority)
Truth table57%4/7
A and B || not(C) and D
A= $unicode_domainsB= _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_unicode_authority)C= not $unicode_domainsD= _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_authority)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
326 || (!$unicode_domains && _valid_match($authorithy, $Twitter::Text::Regexp::validate_url_authority));
327 }
328  
329 142 ▶sub _valid_match {
Subroutine _valid_match: called CC=6 SCAR=18.0
330 142 my ($string, $regex, $optional) = @_;
331 142 ▶ return (defined $string && ($string =~ /($regex)/) && $1 eq $string) unless $optional;
Branch: 1 branch
BranchTrueFalseTotal
unless $optional 84 58 142
Condition83%5/6
defined $string && $string =~ /($regex)/ && $1 eq $string
!ll&&!rl&&r
2874
defined $string and $string =~ /($regex)/
!ll&&!rl&&r
0282
MC/DC66%2/3
defined $string && $string =~ /($regex)/ && $1 eq $string
defined $string $string =~ /($regex)/ $1 eq $string
Truth table75%3/4
A and B && C
A= defined $stringB= $string =~ /($regex)/C= $1 eq $string
ABCresult
0XX0
10X0
1100
1111
332 58 ▶ return !(defined $string && (!($string =~ /($regex)/) || $1 ne $string));
Condition83%5/6
defined $string && (!($string =~ /($regex)/) || $1 ne $string)
!ll&&!rl&&r
4981
not $string =~ /($regex)/ or $1 ne $string
l!l&&r!l&&!r
018
MC/DC66%2/3
defined $string && (!($string =~ /($regex)/) || $1 ne $string)
defined $string not $string =~ /($regex)/ $1 ne $string
Truth table75%3/4
A && not(B) or C
A= defined $stringB= not $string =~ /($regex)/C= $1 ne $string
ABCresult
0XX0
1000
11X1
1011
333 }
334  
335 5 ▶sub is_valid_username {
Subroutine is_valid_username: called CC=3 SCAR=11.0
Pod documented
336 5 my ($username) = @_;
337  
338 5 ▶ return 0 unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 1 4 5
339  
340 4 my $extracted = extract_mentioned_screen_names($username);
341 4 ▶ return scalar(@$extracted) == 1 && $extracted->[0] eq substr($username, 1);
Condition66%2/3
scalar @$extracted == 1 && $extracted->[0] eq substr($username, 1)
!ll&&!rl&&r
013
MC/DC50%1/2
scalar @$extracted == 1 && $extracted->[0] eq substr($username, 1)
scalar @$extracted == 1 $extracted->[0] eq substr($username, 1)
Truth table66%2/3
A && B
A= scalar @$extracted == 1B= $extracted->[0] eq substr($username, 1)
ABresult
0X0
100
111
342 }
343  
344 ## no critic (Subroutines::ProhibitExcessComplexity)
345 52 ▶sub parse_tweet {
Subroutine parse_tweet: called CC=23 SCAR=31.4
Pod documented
346 52 my ($text, $options) = @_;
347 # merge options
348 52 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
2824
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
349 52 $options->{$_} = DEFAULT_TCO_URL_LENGTHS()->{$_} for keys %{ DEFAULT_TCO_URL_LENGTHS() };
350  
351 52 my $normalized_text = NFC($text);
352  
353 52 ▶ return _empty_parse_results() unless length $normalized_text > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless length $normalized_text > 0 1 51 52
354  
355 51 ▶ my $config = $options->{config} || Twitter::Text::Configuration::default_configuration;
Condition66%2/3
$options->{'config'} || Twitter::Text::Configuration::default_configuration()
l!l&&r!l&&!r
27240
MC/DC0%0/2
$options->{'config'} || Twitter::Text::Configuration::default_configuration()
$options->{'config'} Twitter::Text::Configuration::default_configuration()
Truth table66%2/3
A || B
A= $options->{'config'}B= Twitter::Text::Configuration::default_configuration()
ABresult
1X1
011
000
356 51 my $scale = $config->{scale};
357 51 my $max_weighted_tweet_length = $config->{maxWeightedTweetLength};
358 51 my $scaled_max_weighted_tweet_length = $max_weighted_tweet_length * $scale;
359 51 my $transformed_url_length = $config->{transformedURLLength} * $scale;
360 51 my $ranges = $config->{ranges};
361  
362 51 my $url_entities = extract_urls_with_indices($normalized_text);
363 51 ▶ my $emoji_entities = $config->{emojiParsingEnabled} ? extract_emoji_with_indices($normalized_text) : [];
Branch: 1 branch
BranchTrueFalseTotal
$config->{'emojiParsingEnabled'} ? : 24 27 51
364  
365 51 my $has_invalid_chars = 0;
366 51 my $weighted_count = 0;
367 51 my $offset = 0;
368 51 my $display_offset = 0;
369 51 my $valid_offset = 0;
370  
371 51 ▶ while ($offset < length $normalized_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $normalized_text) 29340 51 29391
372 29340 my $char_weight = $config->{defaultWeight};
373 29340 my $entity_length = 0;
374  
375 29340 for my $url_entity (@$url_entities) {
376 26249 ▶ if ($url_entity->{indices}->[0] == $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url_entity->{"indices"}[0] == $offset) 37 26212 26249
377 37 $entity_length = $url_entity->{indices}->[1] - $url_entity->{indices}->[0];
378 37 $weighted_count += $transformed_url_length;
379 37 $offset += $entity_length;
380 37 $display_offset += $entity_length;
381  
382 37 ▶ if ($weighted_count <= $scaled_max_weighted_tweet_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weighted_count <= $scaled_max_weighted_tweet_length) 31 6 37
383 31 $valid_offset += $entity_length;
384 }
385 # Finding a match breaks the loop
386 37 last;
387 }
388 }
389  
390 29340 for my $emoji_entity (@$emoji_entities) {
391 22967 ▶ if ($emoji_entity->{indices}->[0] == $offset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($emoji_entity->{"indices"}[0] == $offset) 318 22649 22967
392 318 $entity_length = $emoji_entity->{indices}->[1] - $emoji_entity->{indices}->[0];
393 318 $weighted_count += $char_weight; # the default weight
394 318 $offset += $entity_length;
395 318 $display_offset += $entity_length;
396  
397 318 ▶ if ($weighted_count <= $scaled_max_weighted_tweet_length) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weighted_count <= $scaled_max_weighted_tweet_length) 298 20 318
398 298 $valid_offset += $entity_length;
399 }
400 # Finding a match breaks the loop
401 318 last;
402 }
403 }
404  
405 29340 ▶ next if $entity_length > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $entity_length > 0 355 28985 29340
406  
407 28985 ▶ if ($offset < length $normalized_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < length $normalized_text) 28985 0 28985
408 28985 my $code_point = substr $normalized_text, $offset, 1;
409  
410 28985 for my $range (@$ranges) {
411 32573 my ($chr) = unpack 'U', $code_point;
412 32573 my ($range_start, $range_end) = ($range->{start}, $range->{end});
413  
414 32573 ▶ if ($range_start <= $chr && $chr <= $range_end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range_start <= $chr and $chr <= $range_end) 26811 5762 32573
Condition100%3/3
$range_start <= $chr and $chr <= $range_end
!ll&&!rl&&r
3575926811
MC/DC100%2/2
$range_start <= $chr and $chr <= $range_end
$range_start <= $chr $chr <= $range_end
Truth table100%3/3
A and B
A= $range_start <= $chrB= $chr <= $range_end
ABresult
0X0
100
111
415 26811 $char_weight = $range->{weight};
416 26811 last;
417 }
418 }
419  
420 28985 $weighted_count += $char_weight;
421  
422 28985 ▶ $has_invalid_chars = _contains_invalid($code_point) unless $has_invalid_chars;
Branch: 1 branch
BranchTrueFalseTotal
unless $has_invalid_chars 28981 4 28985
423 28985 my $codepoint_length = length $code_point;
424 28985 $offset += $codepoint_length;
425 28985 $display_offset += $codepoint_length;
426  
427 28985 ▶ if (!$has_invalid_chars && ($weighted_count <= $scaled_max_weighted_tweet_length)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $has_invalid_chars and $weighted_count <= $scaled_max_weighted_tweet_length) 5190 23795 28985
Condition100%3/3
not $has_invalid_chars and $weighted_count <= $scaled_max_weighted_tweet_length
!ll&&!rl&&r
5237905190
MC/DC100%2/2
not $has_invalid_chars and $weighted_count <= $scaled_max_weighted_tweet_length
not $has_invalid_chars $weighted_count <= $scaled_max_weighted_tweet_length
Truth table100%3/3
not(A) and B
A= not $has_invalid_charsB= $weighted_count <= $scaled_max_weighted_tweet_length
ABresult
0X0
100
111
428 5190 $valid_offset += $codepoint_length;
429 }
430 }
431 }
432  
433 51 my $normalized_text_offset = length($text) - length($normalized_text);
434 51 my $scaled_weighted_length = $weighted_count / $scale;
435 51 ▶ my $is_valid = !$has_invalid_chars && ($scaled_weighted_length <= $max_weighted_tweet_length);
Condition100%3/3
!$has_invalid_chars && $scaled_weighted_length <= $max_weighted_tweet_length
!ll&&!rl&&r
12030
MC/DC100%2/2
!$has_invalid_chars && $scaled_weighted_length <= $max_weighted_tweet_length
!$has_invalid_chars $scaled_weighted_length <= $max_weighted_tweet_length
Truth table100%3/3
not(A) && B
A= !$has_invalid_charsB= $scaled_weighted_length <= $max_weighted_tweet_length
ABresult
0X0
100
111
436 51 my $permilage = int($scaled_weighted_length * 1000 / $max_weighted_tweet_length);
437  
438 return +{
439 51 ▶ weighted_length => $scaled_weighted_length,
Branch: 1 branch
BranchTrueFalseTotal
$is_valid ? : 30 21 51
440 valid => $is_valid ? 1 : 0,
441 permillage => $permilage,
442 display_range_start => 0,
443 display_range_end => $display_offset + $normalized_text_offset - 1,
444 valid_range_start => 0,
445 valid_range_end => $valid_offset + $normalized_text_offset - 1,
446 };
447 }
448 ## use critic
449  
450 1 ▶sub _empty_parse_results {
Subroutine _empty_parse_results: called CC=1 SCAR=0.0
451 return {
452 1 weighted_length => 0,
453 valid => 0,
454 permillage => 0,
455 display_range_start => 0,
456 display_range_end => 0,
457 valid_range_start => 0,
458 valid_range_end => 0,
459 };
460 }
461  
462 28981 ▶sub _contains_invalid {
Subroutine _contains_invalid: called CC=3 SCAR=11.0
463 28981 my ($text) = @_;
464  
465 28981 ▶ return 0 if !$text || length $text == 0;
Branch: 1 branch
BranchTrueFalseTotal
if not $text or length $text == 0 3 28978 28981
Condition66%2/3
not $text or length $text == 0
l!l&&r!l&&!r
3028978
MC/DC50%1/2
not $text or length $text == 0
not $text length $text == 0
Truth table66%2/3
not(A) or B
A= not $textB= length $text == 0
ABresult
1X1
011
000
466 28978 return $text =~ qr/[$Twitter::Text::Regexp::INVALID_CHARACTERS]/;
467 }
468  
469 4 1;
470 __END__
↑ Summary Text/Configuration.pm »