Koha/C4/ClassSortRoutine.pm
Julian Maurice b168f4a2e9 Bug 21395: Make perlcritic happy
This patch adds a .perlcriticrc (copied from qa-test-tools) and fixes
almost all perlcrictic violations according to this .perlcriticrc
The remaining violations are silenced out by appending a '## no critic'
to the offending lines. They can still be seen by using the --force
option of perlcritic
This patch also modify t/00-testcritic.t to check all Perl files using
the new .perlcriticrc.
I'm not sure if this test script is still useful as it is now equivalent
to `perlcritic --quiet .` and it looks like it is much slower
(approximatively 5 times slower on my machine)

Test plan:
1. Run `perlcritic --quiet .` from the root directory. It should output
   nothing
2. Run `perlcritic --quiet --force .`. It should output 7 errors (6
   StringyEval, 1 BarewordFileHandles)
3. Run `TEST_QA=1 prove t/00-testcritic.t`
4. Read the patch. Check that all changes make sense and do not
   introduce undesired behaviour

Signed-off-by: Bernardo Gonzalez Kriegel <bgkriegel@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
2020-06-29 12:37:02 +02:00

129 lines
3.4 KiB
Perl

package C4::ClassSortRoutine;
# Copyright (C) 2007 LibLime
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
use strict;
use warnings;
require Exporter;
use Class::Factory::Util;
use C4::Context;
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
=head1 NAME
C4::ClassSortRoutine - base object for creation of classification sorting
key generation routines
=head1 SYNOPSIS
use C4::ClassSortRoutine;
=head1 FUNCTIONS
=cut
@ISA = qw(Exporter);
@EXPORT = qw(
&GetSortRoutineNames
&GetClassSortKey
);
# initialization code
my %loaded_routines = ();
my @sort_routines = GetSortRoutineNames();
foreach my $sort_routine (@sort_routines) {
if (eval "require C4::ClassSortRoutine::$sort_routine") {
my $ref;
$ref = \&{"C4::ClassSortRoutine::${sort_routine}::get_class_sort_key"};
if (eval { $ref->("a", "b") }) {
$loaded_routines{$sort_routine} = $ref;
} else {
$loaded_routines{$sort_routine} = \&_get_class_sort_key;
}
} else {
$loaded_routines{$sort_routine} = \&_get_class_sort_key;
}
}
=head2 GetSortRoutineNames
my @routines = GetSortRoutineNames();
Get names of all modules under C4::ClassSortRoutine::*. Adding
a new classification sorting routine can therefore be done
simply by writing a new submodule under C4::ClassSortRoutine and
placing it in the C4/ClassSortRoutine directory.
=cut
sub GetSortRoutineNames {
return C4::ClassSortRoutine->subclasses();
}
=head2 GetClassSortKey
my $cn_sort = GetClassSortKey($sort_routine, $cn_class, $cn_item);
Generates classification sorting key. If $sort_routine does not point
to a valid submodule in C4::ClassSortRoutine, default to a basic
normalization routine.
=cut
sub GetClassSortKey {
my ($sort_routine, $cn_class, $cn_item) = @_;
unless (exists $loaded_routines{$sort_routine}) {
warn "attempting to use non-existent class sorting routine $sort_routine\n";
$loaded_routines{$sort_routine} = \&_get_class_sort_key;
}
my $key = $loaded_routines{$sort_routine}->($cn_class, $cn_item);
# FIXME -- hardcoded length for cn_sort
# should replace with some way of getting column widths from
# the DB schema -- since doing this should ideally be
# independent of the DBMS, deferring for the moment.
return substr($key, 0, 255);
}
=head2 _get_class_sort_key
Basic sorting function. Concatenates classification part
and item, converts to uppercase, changes each run of
whitespace to '_', and removes any non-digit, non-latin
letter characters.
=cut
sub _get_class_sort_key {
my ($cn_class, $cn_item) = @_;
my $key = uc "$cn_class $cn_item";
$key =~ s/\s+/_/;
$key =~ s/[^A-Z_0-9]//g;
return $key;
}
1;
=head1 AUTHOR
Koha Development Team <http://koha-community.org/>
=cut