From 51b6ba3439e3a6be125ad94c47f2d4e46b56b763 Mon Sep 17 00:00:00 2001 From: Blaine Motsinger Date: Thu, 11 Jul 2024 16:02:33 -0400 Subject: [PATCH] Reverse unless in codebase --- bin/delete_expired_secrets.pl | 2 +- bin/increment_version.pl | 2 +- lib/Pasteburn.pm | 4 ++-- lib/Pasteburn/Config.pm | 15 +++++++------ lib/Pasteburn/Controller/Secret.pm | 10 ++++----- lib/Pasteburn/Crypt/Hash.pm | 8 +++---- lib/Pasteburn/Crypt/Storage.pm | 6 +++--- lib/Pasteburn/DB.pm | 2 +- lib/Pasteburn/Model/Secrets.pm | 28 ++++++++++++------------- t/996_perl-tidy.t | 2 +- t/997_perl-critic.t | 2 +- t/998_pod-checker.t | 2 +- t/lib/Pasteburn/Test.pm | 10 ++++----- t/unit/lib-Pasteburn-Config/_validate.t | 2 +- 14 files changed, 47 insertions(+), 48 deletions(-) diff --git a/bin/delete_expired_secrets.pl b/bin/delete_expired_secrets.pl index 8a11b4e..672a091 100644 --- a/bin/delete_expired_secrets.pl +++ b/bin/delete_expired_secrets.pl @@ -26,7 +26,7 @@ die "select secrets failed: $exception"; }; -exit unless @secrets; +exit if !@secrets; my @bind_values; foreach my $secret_hashref (@secrets) { diff --git a/bin/increment_version.pl b/bin/increment_version.pl index 11b6a66..8637ee8 100644 --- a/bin/increment_version.pl +++ b/bin/increment_version.pl @@ -90,7 +90,7 @@ sub find_all_files { File::Find::find( sub { my $file = $File::Find::name; - return unless $file =~ /\.pm$/; + return if $file !~ /\.pm$/; push( @modules, File::Spec->abs2rel( $file, $dir ) ); }, diff --git a/lib/Pasteburn.pm b/lib/Pasteburn.pm index 311a1f8..f76d9fb 100644 --- a/lib/Pasteburn.pm +++ b/lib/Pasteburn.pm @@ -22,7 +22,7 @@ BEGIN { set views => config->{appdir} . 'views'; set footer => $conf->{footer}; - unless ( config->{views} ) { + if ( !config->{views} ) { die("FATAL: views is not set"); } } @@ -34,7 +34,7 @@ hook before => sub { foreach my $session_id ( keys %{$session_secrets} ) { my $secret_obj = Pasteburn::Model::Secrets->get( id => $session_id ); - unless ($secret_obj) { + if ( !$secret_obj ) { delete $session_secrets->{$session_id}; } } diff --git a/lib/Pasteburn/Config.pm b/lib/Pasteburn/Config.pm index eb79b5f..2e29e41 100644 --- a/lib/Pasteburn/Config.pm +++ b/lib/Pasteburn/Config.pm @@ -34,7 +34,7 @@ sub _get_conf_path { sub _load { my $rc = _get_conf_path(); - unless ( -f $rc ) { + if ( !-f $rc ) { die "$rc is not present"; } @@ -46,12 +46,12 @@ sub _validate { # verify required config sections foreach my $required (qw{ secret passphrase cookie footer }) { - unless ( exists $config->{$required} ) { + if ( !exists $config->{$required} ) { die "config section $required is required\n"; } } - unless ( exists $config->{secret}{age} ) { + if ( !exists $config->{secret}{age} ) { die "config section secret age is required\n"; } @@ -59,16 +59,15 @@ sub _validate { die "config section secret age must be a positive integer\n"; } - unless ( exists $config->{secret}{scrub} && ( $config->{secret}{scrub} == 1 || $config->{secret}{scrub} == 0 ) ) { + if ( !exists $config->{secret}{scrub} || $config->{secret}{scrub} !~ qr/^[0|1]$/ ) { die "config section secret scrub is required\n"; } - unless ( exists $config->{passphrase}{allow_blank} - && ( $config->{passphrase}{allow_blank} == 1 || $config->{passphrase}{allow_blank} == 0 ) ) { + if ( !exists $config->{passphrase}{allow_blank} || $config->{passphrase}{allow_blank} !~ qr/^[0|1]$/ ) { die "config section passphrase allow_blank is required\n"; } - unless ( exists $config->{cookie}{secret_key} && $config->{cookie}{secret_key} ) { + if ( !exists $config->{cookie}{secret_key} || !$config->{cookie}{secret_key} ) { die "config section cookie secret_key is required\n"; } @@ -77,7 +76,7 @@ sub _validate { die "config section cookie secret_key is the default string and must be updated\n"; } - unless ( exists $config->{footer}{links} && ( $config->{footer}{links} == 1 || $config->{footer}{links} == 0 ) ) { + if ( !exists $config->{footer}{links} || $config->{footer}{links} !~ qr/^[0|1]$/ ) { die "config section footer links is required\n"; } diff --git a/lib/Pasteburn/Controller/Secret.pm b/lib/Pasteburn/Controller/Secret.pm index 65bf7b6..edb2cf4 100644 --- a/lib/Pasteburn/Controller/Secret.pm +++ b/lib/Pasteburn/Controller/Secret.pm @@ -35,7 +35,7 @@ post q{/secret} => sub { }; if ( config->{passphrase}{allow_blank} ) { - unless ($secret) { + if ( !$secret ) { $template_params->{message_type} = 'error'; $template_params->{message} = 'The secret parameter is required'; response->{status} = HTTP::Status::HTTP_BAD_REQUEST; @@ -43,7 +43,7 @@ post q{/secret} => sub { } } else { - unless ( $secret && $passphrase ) { + if ( !$secret || !$passphrase ) { $template_params->{message_type} = 'error'; $template_params->{message} = 'The secret and passphrase parameters are required'; response->{status} = HTTP::Status::HTTP_BAD_REQUEST; @@ -86,7 +86,7 @@ get q{/secret/:id} => sub { # check the db for the secret. my $secret_obj = Pasteburn::Model::Secrets->get( id => $id ); - unless ($secret_obj) { + if ( !$secret_obj ) { my $session_secrets = session->read('secrets'); if ( exists $session_secrets->{$id} ) { delete $session_secrets->{$id}; @@ -134,7 +134,7 @@ post q{/secret/:id} => sub { # check the db for the secret. my $secret_obj = Pasteburn::Model::Secrets->get( id => $id ); - unless ($secret_obj) { + if ( !$secret_obj ) { Pasteburn::set_session_response( { type => 'error', message => 'That secret does not exist or has expired', @@ -177,7 +177,7 @@ post q{/secret/:id} => sub { return template secret => $template_params; } - unless ( $secret_obj->validate_passphrase( passphrase => $passphrase ) ) { + if ( !$secret_obj->validate_passphrase( passphrase => $passphrase ) ) { $template_params->{message_type} = 'error'; $template_params->{message} = 'That passphrase is not correct'; response->{status} = HTTP::Status::HTTP_UNAUTHORIZED; diff --git a/lib/Pasteburn/Crypt/Hash.pm b/lib/Pasteburn/Crypt/Hash.pm index 47280a5..b3b646b 100644 --- a/lib/Pasteburn/Crypt/Hash.pm +++ b/lib/Pasteburn/Crypt/Hash.pm @@ -25,7 +25,7 @@ sub generate { @_, }; - unless ( defined $arg->{string} ) { + if ( !defined $arg->{string} ) { die "string is required\n"; } @@ -63,11 +63,11 @@ sub validate { @_, }; - unless ( $arg->{hash} ) { + if ( !$arg->{hash} ) { die "hash is required\n"; } - unless ( defined $arg->{string} ) { + if ( !defined $arg->{string} ) { die "string is required\n"; } @@ -76,7 +76,7 @@ sub validate { my $input = $self->generate( string => $arg->{string}, salt => $salt ); - return unless length($input) == length( $arg->{hash} ); + return if length($input) != length( $arg->{hash} ); my $match = 1; foreach my $i ( 0 .. length($input) - 1 ) { diff --git a/lib/Pasteburn/Crypt/Storage.pm b/lib/Pasteburn/Crypt/Storage.pm index 8f7ea66..3a7cb2d 100644 --- a/lib/Pasteburn/Crypt/Storage.pm +++ b/lib/Pasteburn/Crypt/Storage.pm @@ -14,7 +14,7 @@ sub new { @_, }; - unless ( defined $arg->{passphrase} ) { + if ( !defined $arg->{passphrase} ) { die "passphrase argument is required\n"; } @@ -30,7 +30,7 @@ sub encode { @_, }; - unless ( $arg->{secret} ) { + if ( !$arg->{secret} ) { die "secret argument is required\n"; } @@ -44,7 +44,7 @@ sub decode { @_, }; - unless ( $arg->{secret} ) { + if ( !$arg->{secret} ) { die "secret argument is required\n"; } diff --git a/lib/Pasteburn/DB.pm b/lib/Pasteburn/DB.pm index b2b3b07..cd7f989 100644 --- a/lib/Pasteburn/DB.pm +++ b/lib/Pasteburn/DB.pm @@ -26,7 +26,7 @@ sub load { $module_path =~ s/\w+\.pm//; my $db = Cwd::realpath( $module_path . '/../../db/pasteburn.sqlite3' ); - unless ( -f $db ) { + if ( !-f $db ) { die "$db is not readable"; } diff --git a/lib/Pasteburn/Model/Secrets.pm b/lib/Pasteburn/Model/Secrets.pm index a6fbacb..a871c99 100644 --- a/lib/Pasteburn/Model/Secrets.pm +++ b/lib/Pasteburn/Model/Secrets.pm @@ -89,7 +89,7 @@ sub get { my ( @where, @bind_values ); foreach my $key ( keys %{$arg} ) { - unless ( defined $arg->{$key} ) { + if ( !defined $arg->{$key} ) { next; } @@ -103,7 +103,7 @@ sub get { my $secret_hashref = $class->_dbh->selectrow_hashref( $sql, undef, @bind_values ); - unless ($secret_hashref) { + if ( !$secret_hashref ) { return; } @@ -118,7 +118,7 @@ sub store { }; foreach my $attribute ( 'passphrase', 'secret' ) { - unless ( defined $self->{$attribute} ) { + if ( !defined $self->{$attribute} ) { die "$attribute is required"; } } @@ -169,7 +169,7 @@ sub store { sub _generate_id { my $self = shift; - unless ( Scalar::Util::blessed($self) ) { + if ( !Scalar::Util::blessed($self) ) { die "_generate_id must be called as an object method"; } @@ -179,7 +179,7 @@ sub _generate_id { sub _update_object { my $self = shift; - unless ( Scalar::Util::blessed($self) ) { + if ( !Scalar::Util::blessed($self) ) { die "_update_object must be called as an object method"; } @@ -207,15 +207,15 @@ sub validate_passphrase { @_, }; - unless ( Scalar::Util::blessed($self) ) { + if ( !Scalar::Util::blessed($self) ) { die "validate_passphrase must be called as an object method"; } - unless ( $self->id ) { + if ( !$self->id ) { die "validate_passphrase cannot be run for a nonexistent secret"; } - unless ( defined $arg->{passphrase} ) { + if ( !defined $arg->{passphrase} ) { die "passphrase is required"; } @@ -224,7 +224,7 @@ sub validate_passphrase { # the code up to this point will allow empty string submitted from the interface, # but not allow an undef to be stored. # although unlikely to fail, still verify the hashed passphrase is in the object. - unless ( defined $self->passphrase ) { + if ( !defined $self->passphrase ) { die "passphrase is not set"; } @@ -240,15 +240,15 @@ sub decode_secret { @_, }; - unless ( Scalar::Util::blessed($self) ) { + if ( !Scalar::Util::blessed($self) ) { die "decode_secret must be called as an object method"; } - unless ( $self->id ) { + if ( !$self->id ) { die "decode_secret cannot be run for a nonexistent secret"; } - unless ( defined $arg->{passphrase} ) { + if ( !defined $arg->{passphrase} ) { die "passphrase is required"; } @@ -267,11 +267,11 @@ sub decode_secret { sub delete_secret { my $self = shift; - unless ( Scalar::Util::blessed($self) ) { + if ( !Scalar::Util::blessed($self) ) { die "delete_secret must be called as an object method"; } - unless ( $self->id ) { + if ( !$self->id ) { die "delete_secret cannot be run for a nonexistent secret"; } diff --git a/t/996_perl-tidy.t b/t/996_perl-tidy.t index 4e43aa1..8cf154c 100644 --- a/t/996_perl-tidy.t +++ b/t/996_perl-tidy.t @@ -4,7 +4,7 @@ use warnings; use FindBin; use Test::More; -unless ( $ENV{TEST_AUTHOR} ) { +if ( !$ENV{TEST_AUTHOR} ) { my $msg = 'Author test. Set $ENV{TEST_AUTHOR} to a true value to run.'; plan( skip_all => $msg ); } diff --git a/t/997_perl-critic.t b/t/997_perl-critic.t index 2387bd8..4e52325 100644 --- a/t/997_perl-critic.t +++ b/t/997_perl-critic.t @@ -5,7 +5,7 @@ use FindBin; use File::Spec; use Test::More; -unless ( $ENV{TEST_AUTHOR} ) { +if ( !$ENV{TEST_AUTHOR} ) { my $msg = 'Author test. Set $ENV{TEST_AUTHOR} to a true value to run.'; plan( skip_all => $msg ); } diff --git a/t/998_pod-checker.t b/t/998_pod-checker.t index 503ec4d..a5f1833 100644 --- a/t/998_pod-checker.t +++ b/t/998_pod-checker.t @@ -4,7 +4,7 @@ use warnings; use FindBin; use Test::More; -unless ( $ENV{TEST_AUTHOR} ) { +if ( !$ENV{TEST_AUTHOR} ) { my $msg = 'Author test. Set $ENV{TEST_AUTHOR} to a true value to run.'; plan( skip_all => $msg ); } diff --git a/t/lib/Pasteburn/Test.pm b/t/lib/Pasteburn/Test.pm index 6eb53d9..f1882c9 100644 --- a/t/lib/Pasteburn/Test.pm +++ b/t/lib/Pasteburn/Test.pm @@ -23,7 +23,7 @@ sub import { if ( $args{tests} ) { $class->builder->plan( tests => $args{tests} ) - unless $args{tests} eq 'no_declare'; + if $args{tests} ne 'no_declare'; } elsif ( $args{skip_all} ) { $class->builder->plan( skip_all => $args{skip_all} ); @@ -34,7 +34,7 @@ sub import { CLEANUP => 0, ); - unless ( $args{skip_db} ) { + if ( !$args{skip_db} ) { init_db(); } @@ -86,7 +86,7 @@ sub write_config { %{$config_tiny} = %{$args{config}}; die( "unable to write config\n" ) - unless $config_tiny->write( $rc ); + if !$config_tiny->write( $rc ); return $rc; } @@ -150,12 +150,12 @@ sub create_test_app { ); foreach my $required ( keys %args ) { - unless ( defined $args{$required} ) { + if ( !defined $args{$required} ) { die "$required is required"; } } - unless ( ref $args{config} eq 'HASH' ) { + if ( !ref $args{config} eq 'HASH' ) { die "config must be a hashref"; } diff --git a/t/unit/lib-Pasteburn-Config/_validate.t b/t/unit/lib-Pasteburn-Config/_validate.t index e8c2094..c56c303 100644 --- a/t/unit/lib-Pasteburn-Config/_validate.t +++ b/t/unit/lib-Pasteburn-Config/_validate.t @@ -55,7 +55,7 @@ EXCEPTIONS: { my $stored = delete $config_expected->{$required}{$required_sub_key}; dies_ok { Pasteburn::Config::_validate( $config_expected ) } - "dies if config is missing $required $required_sub_key key"; + "dies if config is missing $required $required_sub_key"; $config_expected->{$required}{$required_sub_key} = $stored; }