Use more perlcritic policies

This commit is contained in:
eleksir
2022-04-03 18:53:10 +03:00
parent 2428e6edff
commit e0159a6c65
10 changed files with 70 additions and 43 deletions

View File

@@ -1,3 +1,18 @@
#!/usr/bin/env perlcritic
# Задействованные модули критика:
#
# Perl::Critic::More
# Perl::Critic::Pulp
# Perl::Critic::TooMuchCode
# Perl::Critic::Lax
# Perl::Critic::CognitiveComplexity
# Perl::Critic::Policy::CompileTime
# Perl::Critic::Policy::ProhibitImplicitImport
# Perl::Critic::Policy::Dynamic::NoIndirect
# Perl::Critic::StricterSubs
# Perl::Critic::Policy::ProhibitImplicitImport
# Perl::Critic::Policy::CodeLayout::RequireUseUTF8
[-CodeLayout::RequireTidyCode]
[-ValuesAndExpressions::ProhibitMagicNumbers]
[-ControlStructures::ProhibitUnlessBlocks]
@@ -14,4 +29,14 @@
[-Subroutines::ProhibitExplicitReturnUndef]
[-RegularExpressions::ProhibitEscapedMetacharacters]
[-InputOutput::RequireBriefOpen]
[-Modules::ProhibitExcessMainComplexity]
[-Modules::ProhibitExcessMainComplexity]
[-Editor::RequireEmacsFileVariables]
[-TooMuchCode::ProhibitUnnecessaryUTF8Pragma]
[-ValuesAndExpressions::RequireConstantOnLeftSideOfEquality]
[-Lax::RequireConstantOnLeftSideOfEquality::ExceptEq]
[-Lax::ProhibitEmptyQuotes::ExceptAsFallback]
[-TooMuchCode::ProhibitDuplicateLiteral]
[-CodeLayout::RequireASCII]
[-ErrorHandling::RequireUseOfExceptions]
[-Miscellanea::ProhibitUnrestrictedNoCritic]
[-ValuesAndExpressions::RestrictLongStrings]

View File

@@ -1,6 +1,6 @@
#!/usr/bin/perl
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
@@ -29,9 +29,10 @@ no Cwd;
use Cwd::utf8 qw (chdir abs_path);
use File::Basename qw (basename dirname);
use File::Path qw (make_path);
use File::Spec ();
use Log::Any qw ($log);
use Log::Any::Adapter;
use Log::Any::For::Std;
use Log::Any::Adapter ();
use Log::Any::For::Std ();
use POSIX qw (setgid setuid setsid);
use Conf qw (LoadConf);
@@ -70,8 +71,8 @@ if ($UID == 0) {
make_path (
$piddir, {
uid => $uid,
group => $gid
}
group => $gid,
},
);
}
@@ -82,8 +83,8 @@ if ($UID == 0) {
make_path (
$logdir, {
uid => $uid,
group => $gid
}
group => $gid,
},
) or die "Unable to create $logdir: $OS_ERROR\n";
}
}
@@ -103,14 +104,14 @@ if (defined $logfile) {
# verbosity paticulary for CHI.
Log::Any::Adapter->set (
{
category => 'CHI::Driver'
category => 'CHI::Driver',
},
'File',
$logfile,
log_level => 'warn'
log_level => 'warn',
);
} else {
Log::Any::Adapter->set ('File', '/dev/null');
Log::Any::Adapter->set ('File', File::Spec->devnull);
}
# По-умолчанию, демонизируемся, если явно не указано, что этого делать не надо
@@ -126,7 +127,7 @@ my $apath = abs_path $PROGRAM_NAME;
unless (defined $apath) {
$log->fatal ("[FATAL] Unable to get abs_path: $OS_ERROR\n");
exit 1;
};
}
my $pidfile = sprintf '%s/%s.pid', $piddir, basename $apath;
$log->info ("[INFO] Creating pidfile at $pidfile");
@@ -152,7 +153,7 @@ $PROGRAM_NAME = abs_path $PROGRAM_NAME; ## no critic (Variables::Re
# И запустим уже наш сервис
RunGames ();
if (-f $pidfile) {
if (-e $pidfile) {
unlink $pidfile;
}
@@ -160,11 +161,11 @@ exit 0;
sub daemonize {
__fork ();
POSIX::setsid ();
open STDIN, '<', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
setsid ();
open STDIN, '<', File::Spec->devnull; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
# we handle STDOUT && STDERR via Log::Any::For::Std, so do not close them explicitly
#open STDOUT, '>', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
#open STDERR, '>', '/dev/null'; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
#open STDOUT, '>', File::Spec->devnull; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
#open STDERR, '>', File::Spec->devnull; ## no critic (InputOutput::RequireCheckedSyscalls, InputOutput::RequireCheckedOpen)
__fork ();
umask 0;
return;
@@ -188,7 +189,7 @@ sub __fork {
sub __signal_handler {
$log->info ("[INFO] Caught a signal $OS_ERROR");
if (-f $pidfile) {
if (-e $pidfile) {
unlink $pidfile;
}

View File

@@ -1,6 +1,6 @@
#!/usr/bin/perl
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;

View File

@@ -1,3 +1,4 @@
## no critic
requires 'Carp', '==1.50';
requires 'Clone', '==0.45';
requires 'Cwd::utf8', '==0.011';
@@ -10,6 +11,7 @@ requires 'English', '0';
requires 'Exporter', '==5.74';
requires 'File::Basename', '0';
requires 'File::Path', '==2.18';
requires 'File::Spec', '==3.75';
requires 'JSON::XS', '==4.03';
requires 'Log::Any', '==1.710';
requires 'Log::Any::Adapter', '==1.710';

View File

@@ -1,14 +1,14 @@
package Conf;
# loads config
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Encode qw (encode);
use JSON::XS;
use JSON::XS ();
use version; our $VERSION = qw (1.0);
use Exporter qw (import);

View File

@@ -1,7 +1,7 @@
package Games;
# Общие модули - синтаксис, кодировки итд
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
@@ -9,14 +9,13 @@ use open qw (:std :utf8);
# Модули для работы приложения
use Clone qw (clone);
use Data::Dumper;
use Data::Dumper qw (Dumper);
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);
# Чтобы "уж точно" использовать hiredis-биндинги, загрузим этот модуль перед Mojo::Redis
use Protocol::Redis::XS;
use Mojo::Redis;
use Mojo::IOLoop;
use Mojo::IOLoop::Signal;
use Protocol::Redis::XS ();
use Mojo::Redis ();
use Mojo::IOLoop ();
use Mojo::IOLoop::Signal ();
use Conf qw (LoadConf);
use Games::Archeologist qw (Dig);
@@ -99,7 +98,7 @@ my $parse_message = sub {
if (defined $answer->{message}) {
$self->json ($send_to)->notify (
$send_to => $answer
$send_to => $answer,
);
}
@@ -110,7 +109,7 @@ my $__signal_handler = sub {
my ($self, $name) = @_;
$log->info ("[INFO] Caught a signal $name");
if (defined $main::pidfile && -f $main::pidfile) {
if (defined $main::pidfile && -e $main::pidfile) {
unlink $main::pidfile;
}
@@ -123,7 +122,7 @@ sub RunGames {
$log->info ("[INFO] Connecting to $c->{server}, $c->{port}");
my $redis = Mojo::Redis->new (
sprintf 'redis://%s:%s/1', $c->{server}, $c->{port}
sprintf 'redis://%s:%s/1', $c->{server}, $c->{port},
);
$log->info ('[INFO] Registering connection-event callback');
@@ -140,11 +139,11 @@ sub RunGames {
my ($conn, $error) = @_;
$log->error ("[ERROR] Redis connection error: $error");
return;
}
},
);
return;
}
},
);
my $pubsub = $redis->pubsub;
@@ -155,13 +154,13 @@ sub RunGames {
$log->debug ("[DEBUG] Subscribing to $channel");
$sub->{$channel} = $pubsub->json ($channel)->listen (
$channel => sub { $parse_message->(@_); }
$channel => sub { $parse_message->(@_); },
);
}
Mojo::IOLoop::Signal->on (
TERM => $__signal_handler,
INT => $__signal_handler
INT => $__signal_handler,
);
do { Mojo::IOLoop->start } until Mojo::IOLoop->is_running;

View File

@@ -1,12 +1,12 @@
package Games::Archeologist;
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use DBI;
use DBI ();
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);

View File

@@ -1,12 +1,12 @@
package Games::Fisher;
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use English qw ( -no_match_vars );
use DBI;
use DBI ();
use Log::Any qw ($log);
use Math::Random::Secure qw (irand);

View File

@@ -1,12 +1,12 @@
package Games::Gamestat;
# Stores and calculates player stats for games
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
use open qw (:std :utf8);
use DBI;
use DBI ();
use English qw ( -no_match_vars );
use File::Path qw (make_path);
use Mojo::Util qw (trim);

View File

@@ -1,6 +1,6 @@
package Util;
use 5.018;
use 5.018; ## no critic (ProhibitImplicitImport)
use strict;
use warnings;
use utf8;
@@ -8,7 +8,7 @@ use open qw (:std :utf8);
use English qw ( -no_match_vars );
use Digest::SHA qw (sha1_base64);
use Encode;
use Encode qw (encode_utf8);
use version; our $VERSION = qw (1.0);
use Exporter qw (import);