blib/lib/Data/Validate/VIN.pm

filter: Statementstmt 98.5% 199 / 202 Branchbran 80.0% 48 / 60 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 94.7% 273 / 288 CC 34 SCAR 16.8
CC
34
Cov
94%
get
12.6
_checkVIS
6.1
_checkCheckDigit
6.0
CRAP 5.4
↑ Summary
1 package Data::Validate::VIN;
2  
3 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use Carp;
7  
8 3 our $VERSION = '0.05';
9  
10 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
11 12 my ( $class, $vin ) = @_;
12  
13 12 my $self = bless {}, $class;
14  
15 12 $self->{_allowed} = qr/[A-HJ-NPR-Z0-9]/;
16 12 $self->{errors} = [];
17  
18 # this next one might add to $self->{errors}, which is fine
19 12 $self->{vin} = $self->_checkVIN($vin);
20  
21 # we won't process the wmi, vds or vis if the vin check returned errors
22 12 ▶ unless ( scalar( @{ $self->{errors} } ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$self->{"errors"};} > 0) 3 9 12
23 3 $self->{wmi} = $self->_checkWMI( $self->{vin} );
24 3 $self->{vds} = $self->_checkVDS( $self->{vin} );
25 3 $self->{vis} = $self->_checkVIS( $self->{vin} );
26 3 $self->{checkdigit} = $self->_checkCheckDigit( $self->{vin} );
27 }
28  
29 $self->{valid} =
30 12 ▶ scalar( @{ $self->{errors} } > 0 )
Branch: 1 branch
BranchTrueFalseTotal
scalar(@{$self->{'errors'};} > 0) ? : 11 1 12
31 ? undef
32 : 1;
33  
34 12 return $self;
35 }
36  
37 3 ▶sub valid {
Subroutine valid: called CC=2 SCAR=6.9
Pod documented
38 3 my ($self) = @_;
39  
40 defined $self->{valid}
41 3 ▶ ? return 1
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'valid'} ? : 1 2 3
42 : return;
43 }
44  
45 12 ▶sub errors {
Subroutine errors: called CC=2 SCAR=6.9
Pod documented
46 12 my ($self) = @_;
47  
48 12 scalar( @{ $self->{errors} } > 0 )
49 ? return $self->{errors}
50 12 ▶ : return [];
Branch: 1 branch
BranchTrueFalseTotal
scalar(@{$self->{'errors'};} > 0) ? : 11 1 12
51 }
52  
53 24 ▶sub get {
Subroutine get: called CC=10 SCAR=25.3
Pod documented
54 24 my ( $self, $wanted ) = @_;
55  
56 24 ▶ if ( $wanted =~ /wmi|vds|vis|vin|checkdigit|country|year/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ /wmi|vds|vis|vin|checkdigit|country|year/i) 24 0 24
57 24 ▶ if ( $wanted =~ /vin|checkdigit/i ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($wanted =~ /vin|checkdigit/i) { } 8 16 24
elsif ($wanted =~ /wmi|vds|vis/i) { } 12 4 16
elsif ($wanted =~ /country/i) { } 0 4 4
elsif ($wanted =~ /year/i) { } 4 0 4
58 defined $self->{$wanted}
59 8 ▶ ? return $self->{$wanted}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$wanted} ? : 5 3 8
60 : return;
61 }
62 elsif ( $wanted =~ /wmi|vds|vis/i ) {
63 defined $self->{$wanted}->{$wanted}
64 12 ▶ ? return $self->{$wanted}->{$wanted}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$wanted}{$wanted} ? : 3 9 12
65 : return;
66 }
67 elsif ( $wanted =~ /country/i ) {
68 defined $self->{wmi}->{$wanted}
69 0 ▶ ? return $self->{wmi}->{$wanted}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'wmi'}{$wanted} ? : 0 0 0
70 : return;
71 }
72 elsif ( $wanted =~ /year/i ) {
73 defined $self->{vis}->{$wanted}
74 4 ▶ ? return $self->{vis}->{$wanted}
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'vis'}{$wanted} ? : 1 3 4
75 : return;
76 }
77 else {
78 0 return;
79 }
80 }
81 0 return;
82 }
83  
84 12 ▶sub _checkVIN {
Subroutine _checkVIN: called CC=4 SCAR=13.9
85 12 my ( $self, $_vin ) = @_;
86  
87 12 ▶ if ( not defined $_vin or $_vin =~ /^$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_vin or $_vin =~ /^$/) 2 10 12
Condition100%3/3
not defined $_vin or $_vin =~ /^$/
l!l&&r!l&&!r
1110
MC/DC100%2/2
not defined $_vin or $_vin =~ /^$/
not defined $_vin $_vin =~ /^$/
Truth table100%3/3
not(A) or B
A= not defined $_vinB= $_vin =~ /^$/
ABresult
1X1
011
000
88 2 $self->_trackError("No VIN supplied");
89 2 return;
90 }
91  
92 10 chomp($_vin);
93  
94 $_vin = $self->_checkCharacters(
95 wanted => $self->{_allowed},
96 10 unwanted => qr/[IOQ]/,
97 toCheck => $_vin,
98 section => 'VIN'
99 );
100  
101 10 ▶ if ( length($_vin) != 17 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_vin != 17) 6 4 10
102 6 my $err = sprintf( "%- 17s", $_vin ) . " is not the expected length";
103 6 $self->_trackError($err);
104 }
105  
106 10 return $_vin;
107 }
108  
109 3 ▶sub _checkWMI {
Subroutine _checkWMI: called CC=2 SCAR=6.9
110 3 my ( $self, $_vin ) = @_;
111  
112 my $wmi = {
113 wmi => $self->_checkCharacters(
114 wanted => $self->{_allowed},
115 3 unwanted => qr/[IOQ]/,
116 toCheck => substr( $_vin, 0, 2 ),
117 section => 'WMI'
118 ),
119 };
120  
121 # load known valid WMIs
122 3 my $_allowed = $self->_loadWMI();
123  
124 defined $_allowed->{ $wmi->{wmi} }
125 ? $wmi->{country} = $_allowed->{ $wmi->{wmi} }
126 3 ▶ : $self->_trackError("Unknown WMI: $wmi->{wmi}");
Branch: 1 branch
BranchTrueFalseTotal
defined $$_allowed{$wmi->{'wmi'}} ? : 2 1 3
127  
128 3 return $wmi;
129 }
130  
131 19 ▶sub _checkCharacters {
Subroutine _checkCharacters: called CC=4 SCAR=13.9
132 19 my ( $self, %args ) = @_;
133  
134 # wanted unwanted toCheck section
135  
136 19 my $checked = uc($args{toCheck});
137 19 my @checked = split(q{}, $checked); ## char array
138 19 my @illegal;
139  
140 19 ▶ for (my $i = 0; $i < @checked; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @checked) 155 19 174
141 155 ▶ unless ($checked[$i] =~ /^$args{wanted}+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($checked[$i] =~ /^$args{"wanted"}+$/) 8 147 155
142 8 push @illegal, $checked[$i];
143 }
144 }
145  
146 19 ▶ if (@illegal) {
Branch: 1 branch
BranchTrueFalseTotal
if (@illegal) 5 14 19
147 5 my $err = "Illegal characters in " . $args{section} . ': ' . join(q{}, @illegal);
148 5 $self->_trackError($err);
149 }
150  
151 19 return $checked;
152 }
153  
154 3 ▶sub _loadWMI {
Subroutine _loadWMI: called CC=1 SCAR=0.0
155 3 my ($self) = @_;
156  
157 3 my %wmi;
158  
159 3 $self->_loadCountry( 'AA', 'AH', 'South Africa', \%wmi );
160 3 $self->_loadCountry( 'AJ', 'AN', 'Ivory Coast', \%wmi );
161 3 $self->_loadCountry( 'BA', 'BE', 'Angola', \%wmi );
162 3 $self->_loadCountry( 'BF', 'BK', 'Kenya', \%wmi );
163 3 $self->_loadCountry( 'BL', 'BR', 'Tanzania', \%wmi );
164 3 $self->_loadCountry( 'CA', 'CE', 'Benin', \%wmi );
165 3 $self->_loadCountry( 'CF', 'CK', 'Madagascar', \%wmi );
166 3 $self->_loadCountry( 'CL', 'CR', 'Tunisia', \%wmi );
167 3 $self->_loadCountry( 'DA', 'DE', 'Egypt', \%wmi );
168 3 $self->_loadCountry( 'DF', 'DK', 'Morocco', \%wmi );
169 3 $self->_loadCountry( 'DL', 'DR', 'Zambia', \%wmi );
170 3 $self->_loadCountry( 'EA', 'EE', 'Ethiopia', \%wmi );
171 3 $self->_loadCountry( 'EF', 'EK', 'Mozambique', \%wmi );
172 3 $self->_loadCountry( 'FA', 'FE', 'Ghana', \%wmi );
173 3 $self->_loadCountry( 'FF', 'FK', 'Nigeria', \%wmi );
174 3 $self->_loadCountry( 'JA', 'JT', 'Japan', \%wmi );
175 3 $self->_loadCountry( 'KA', 'KE', 'Sri Lanka', \%wmi );
176 3 $self->_loadCountry( 'KF', 'KK', 'Israel', \%wmi );
177 3 $self->_loadCountry( 'KL', 'KR', 'Korea (South)', \%wmi );
178 3 $self->_loadCountry( 'LA', 'L0', 'China', \%wmi );
179 3 $self->_loadCountry( 'MA', 'ME', 'India', \%wmi );
180 3 $self->_loadCountry( 'MF', 'MK', 'Indonesia', \%wmi );
181 3 $self->_loadCountry( 'ML', 'MR', 'Thailand', \%wmi );
182 3 $self->_loadCountry( 'NF', 'NK', 'Pakistan', \%wmi );
183 3 $self->_loadCountry( 'NL', 'NR', 'Turkey', \%wmi );
184 3 $self->_loadCountry( 'PA', 'PE', 'Philippines', \%wmi );
185 3 $self->_loadCountry( 'PF', 'PK', 'Singapore', \%wmi );
186 3 $self->_loadCountry( 'PL', 'PR', 'Malaysia', \%wmi );
187 3 $self->_loadCountry( 'RA', 'RE', 'United Arab Emirates', \%wmi );
188 3 $self->_loadCountry( 'RF', 'RK', 'Taiwan', \%wmi );
189 3 $self->_loadCountry( 'RL', 'RR', 'Vietnam', \%wmi );
190 3 $self->_loadCountry( 'SA', 'SM', 'United Kingdom', \%wmi );
191 3 $self->_loadCountry( 'SN', 'ST', 'Germany', \%wmi );
192 3 $self->_loadCountry( 'SU', 'SZ', 'Poland', \%wmi );
193 3 $self->_loadCountry( 'S1', 'S4', 'Latvia', \%wmi );
194 3 $self->_loadCountry( 'TA', 'TH', 'Switzerland', \%wmi );
195 3 $self->_loadCountry( 'TJ', 'TP', 'Czech Republic', \%wmi );
196 3 $self->_loadCountry( 'TR', 'TV', 'Hungary', \%wmi );
197 3 $self->_loadCountry( 'TW', 'T1', 'Portugal', \%wmi );
198 3 $self->_loadCountry( 'UH', 'UM', 'Denmark', \%wmi );
199 3 $self->_loadCountry( 'UN', 'UT', 'Ireland', \%wmi );
200 3 $self->_loadCountry( 'UU', 'UZ', 'Romania', \%wmi );
201 3 $self->_loadCountry( 'U5', 'U7', 'Slovakia', \%wmi );
202 3 $self->_loadCountry( 'VA', 'VE', 'Austria', \%wmi );
203 3 $self->_loadCountry( 'VF', 'VR', 'France', \%wmi );
204 3 $self->_loadCountry( 'VS', 'VW', 'Spain', \%wmi );
205 3 $self->_loadCountry( 'VX', 'V2', 'Serbia', \%wmi );
206 3 $self->_loadCountry( 'V3', 'V5', 'Croatia', \%wmi );
207 3 $self->_loadCountry( 'V6', 'V0', 'Estonia', \%wmi );
208 3 $self->_loadCountry( 'WA', 'W0', 'Germany', \%wmi );
209 3 $self->_loadCountry( 'XA', 'XE', 'Bulgaria', \%wmi );
210 3 $self->_loadCountry( 'XF', 'XK', 'Greece', \%wmi );
211 3 $self->_loadCountry( 'XL', 'XR', 'Netherlands', \%wmi );
212 3 $self->_loadCountry( 'XS', 'XW', 'USSR', \%wmi );
213 3 $self->_loadCountry( 'XX', 'X2', 'Luxembourg', \%wmi );
214 3 $self->_loadCountry( 'X3', 'X0', 'Russia', \%wmi );
215 3 $self->_loadCountry( 'YA', 'YE', 'Belgium', \%wmi );
216 3 $self->_loadCountry( 'YF', 'YK', 'Finland', \%wmi );
217 3 $self->_loadCountry( 'YL', 'YR', 'Malta', \%wmi );
218 3 $self->_loadCountry( 'YS', 'YW', 'Sweden', \%wmi );
219 3 $self->_loadCountry( 'YX', 'Y2', 'Norway', \%wmi );
220 3 $self->_loadCountry( 'Y3', 'Y5', 'Belarus', \%wmi );
221 3 $self->_loadCountry( 'Y6', 'Y0', 'Ukraine', \%wmi );
222 3 $self->_loadCountry( 'ZA', 'ZR', 'Italy', \%wmi );
223 3 $self->_loadCountry( 'ZX', 'Z2', 'Slovenia', \%wmi );
224 3 $self->_loadCountry( 'Z3', 'Z5', 'Lithuania', \%wmi );
225 3 $self->_loadCountry( '1A', '10', 'United States', \%wmi );
226 3 $self->_loadCountry( '2A', '20', 'Canada', \%wmi );
227 3 $self->_loadCountry( '3A', '3W', 'Mexico', \%wmi );
228 3 $self->_loadCountry( '3X', '37', 'Costa Rica', \%wmi );
229 3 $self->_loadCountry( '38', '30', 'Cayman Islands', \%wmi );
230 3 $self->_loadCountry( '4A', '40', 'United States', \%wmi );
231 3 $self->_loadCountry( '5A', '50', 'United States', \%wmi );
232 3 $self->_loadCountry( '6A', '6W', 'Australia', \%wmi );
233 3 $self->_loadCountry( '7A', '7E', 'New Zealand', \%wmi );
234 3 $self->_loadCountry( '8A', '8E', 'Argentina', \%wmi );
235 3 $self->_loadCountry( '8F', '8K', 'Chile', \%wmi );
236 3 $self->_loadCountry( '8L', '8R', 'Ecuador', \%wmi );
237 3 $self->_loadCountry( '8S', '8W', 'Peru', \%wmi );
238 3 $self->_loadCountry( '8X', '82', 'Venezuela', \%wmi );
239 3 $self->_loadCountry( '9A', '9E', 'Brazil', \%wmi );
240 3 $self->_loadCountry( '9F', '9K', 'Colombia', \%wmi );
241 3 $self->_loadCountry( '9L', '9R', 'Paraguay', \%wmi );
242 3 $self->_loadCountry( '9S', '9W', 'Uruguay', \%wmi );
243 3 $self->_loadCountry( '9X', '92', 'Trinidad & Tobago', \%wmi );
244 3 $self->_loadCountry( '93', '99', 'Brazil', \%wmi );
245  
246 3 return \%wmi;
247 }
248  
249 3 ▶sub _checkVDS {
Subroutine _checkVDS: called CC=2 SCAR=7.1
250 3 my ( $self, $_vin ) = @_;
251  
252 my $vds = {
253 vds => length($_vin) == 17
254 ? $self->_checkCharacters(
255 wanted => $self->{_allowed},
256 3 ▶ unwanted => qr/[IOQ]/,
Branch: 1 branch
BranchTrueFalseTotal
length $_vin == 17 ? : 3 0 3
257 toCheck => substr( $_vin, 3, 6 ),
258 section => 'VDS'
259 )
260 : undef
261 };
262  
263 3 return $vds;
264 }
265  
266 3 ▶sub _checkVIS {
Subroutine _checkVIS: called CC=6 SCAR=18.1
267 3 my ( $self, $_vin ) = @_;
268  
269 my $vis = {
270 vis => length($_vin) == 17
271 ? $self->_checkCharacters(
272 wanted => $self->{_allowed},
273 3 ▶ unwanted => qr/[IOQ]/,
Branch: 1 branch
BranchTrueFalseTotal
length $_vin == 17 ? : 3 0 3
274 toCheck => substr( $_vin, 9, 8 ),
275 section => 'VIS'
276 )
277 : undef
278 };
279  
280 3 ▶ if ( defined $vis->{vis} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $vis->{"vis"}) 3 0 3
281  
282 3 my %years = (
283 A => [ 1980, 2010 ],
284 L => [ 1990, 2020 ],
285 Y => [ 2000, 2030 ],
286 B => [ 1981, 2011 ],
287 M => [ 1991, 2021 ],
288 1 => [ 2001, 2031 ],
289 C => [ 1982, 2012 ],
290 N => [ 1992, 2022 ],
291 2 => [ 2002, 2032 ],
292 D => [ 1983, 2013 ],
293 P => [ 1993, 2023 ],
294 3 => [ 2003, 2033 ],
295 E => [ 1984, 2014 ],
296 R => [ 1994, 2024 ],
297 4 => [ 2004, 2034 ],
298 F => [ 1985, 2015 ],
299 S => [ 1995, 2025 ],
300 5 => [ 2005, 2035 ],
301 G => [ 1986, 2016 ],
302 T => [ 1996, 2026 ],
303 6 => [ 2006, 2036 ],
304 H => [ 1987, 2017 ],
305 V => [ 1997, 2027 ],
306 7 => [ 2007, 2037 ],
307 J => [ 1988, 2018 ],
308 W => [ 1998, 2028 ],
309 8 => [ 2008, 2038 ],
310 K => [ 1989, 2019 ],
311 X => [ 1999, 2029 ],
312 9 => [ 2009, 2039 ],
313 );
314  
315 3 my $yearDigit = substr( $vis->{vis}, 0, 1 );
316  
317 my $year =
318 defined $years{$yearDigit}
319 3 ▶ ? $years{$yearDigit}
Branch: 1 branch
BranchTrueFalseTotal
defined $years{$yearDigit} ? : 2 1 3
320 : undef;
321  
322 3 ▶ if ($year) {
Branch: 1 branch
BranchTrueFalseTotal
if ($year) { } 2 1 3
323 2 $vis->{year} = $year;
324 }
325 else {
326 $self->_trackError("Illegal character in 10th position: $yearDigit")
327 1 ▶ unless $vis->{year};
Branch: 1 branch
BranchTrueFalseTotal
unless $vis->{"year"} 1 0 1
328 }
329 }
330  
331 3 return $vis;
332 }
333  
334 3 ▶sub _checkCheckDigit {
Subroutine _checkCheckDigit: called CC=6 SCAR=18.0
335 3 my ( $self, $_vin ) = @_;
336  
337 3 ▶ return unless length($_vin) == 17;
Branch: 1 branch
BranchTrueFalseTotal
unless length $_vin == 17 0 3 3
338  
339 3 my $passedCheckDigit = substr( $_vin, 8, 1 );
340  
341 3 my %vals = (
342 A => 1,
343 B => 2,
344 C => 3,
345 D => 4,
346 E => 5,
347 F => 6,
348 G => 7,
349 H => 8,
350 J => 1,
351 K => 2,
352 L => 3,
353 M => 4,
354 N => 5,
355 P => 7,
356 R => 9,
357 S => 2,
358 T => 3,
359 U => 4,
360 V => 5,
361 W => 6,
362 X => 7,
363 Y => 8,
364 Z => 9
365 );
366  
367 # Add the numeric pieces
368 # these are worth face value
369 3 for ( 0 .. 9 ) {
370 30 $vals{$_} = $_;
371 }
372  
373 3 my %wghts = (
374 1 => 8,
375 2 => 7,
376 3 => 6,
377 4 => 5,
378 5 => 4,
379 6 => 3,
380 7 => 2,
381 8 => 10,
382 9 => 0,
383 10 => 9,
384 11 => 8,
385 12 => 7,
386 13 => 6,
387 14 => 5,
388 15 => 4,
389 16 => 3,
390 17 => 2
391 );
392  
393 3 my @vinbits = split( // => $_vin );
394  
395 3 my $sum;
396  
397 3 my $ind = 1;
398 3 for my $bit (@vinbits) {
399 51 $sum += $vals{$bit} * $wghts{$ind};
400 51 $ind++;
401 }
402  
403 3 my $calcCheckDigit = $sum % 11;
404 3 ▶ $calcCheckDigit = 'X'
Branch: 1 branch
BranchTrueFalseTotal
if $calcCheckDigit == 10 0 3 3
405 if $calcCheckDigit == '10';
406  
407 3 ▶ $calcCheckDigit =~ /$passedCheckDigit/
Branch: 1 branch
BranchTrueFalseTotal
$calcCheckDigit =~ /$passedCheckDigit/ ? : 1 2 3
408 ? return $calcCheckDigit
409 : $self->_trackError(
410 "Checkdigit mismatch; expected $calcCheckDigit, got $passedCheckDigit");
411  
412 2 return;
413 }
414  
415 258 ▶sub _loadCountry {
Subroutine _loadCountry: called CC=2 SCAR=6.9
416 258 my ( $self, $start, $end, $country, $store ) = @_;
417  
418 258 $store->{$start} = $country;
419  
420 258 ▶ until ( $start eq $end ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start eq $end) 1752 258 2010
421 1752 my @pieces = split( // => $start );
422 1752 my $next = $self->_next( $pieces[1] );
423  
424 1752 $start = $pieces[0] . $next;
425  
426 1752 $store->{$start} = $country;
427 }
428  
429 258 return;
430 }
431  
432 1752 ▶sub _next {
Subroutine _next: called CC=2 SCAR=7.0
433 1752 my ( $self, $current ) = @_;
434  
435 1752 my @fields = qw{ A B C D E F G H J K L M N P R S T
436 U V W X Y Z 1 2 3 4 5 6 7 8 9 0 };
437  
438 1752 my %order = map { ( $fields[$_], $_ ) } 0 .. scalar(@fields) - 1;
439  
440 1752 my $max = scalar(@fields) - 1;
441  
442 my $next =
443 $order{$current} == $max
444 ? $fields[0]
445 1752 ▶ : $fields[ $order{$current} + 1 ];
Branch: 1 branch
BranchTrueFalseTotal
$order{$current} == $max ? : 0 1752 1752
446  
447 1752 return $next;
448 }
449  
450 17 ▶sub _trackError {
Subroutine _trackError: called CC=1 SCAR=0.0
451 17 my ( $self, $error ) = @_;
452  
453 17 push @{ $self->{errors} }, $error;
454  
455 17 return;
456 }
457  
458 3 1;
459 __END__
↑ Summary