Commits

Burak Gürsoy committed ad6f603

Perl::Critic refactoring.

  • Participants
  • Parent commits db583b6

Comments (0)

Files changed (6)

 use strict;
+use warnings;
 use lib qw( builder );
 use Build;
 
 my $mb = Build->new;
 $mb->change_versions(1);
-$mb->copyright_first_year( 2007 );
+$mb->copyright_first_year( '2007' );
 $mb->add_pod_author_copyright_license(1);
 $mb->create_build_script;
+
+1;
 
 Time zone is GMT+2.
 
+0.30 Fri Oct  2 06:45:20 2009
+    => Perl::Critic refactoring.
+
 0.24 Thu Sep  3 01:57:43 2009
     => Pod & distro fixes.
 
 
 COPYRIGHT AND LICENCE
 
-Copyright (C) 2007 Burak Gürsoy <burak@cpan.org>. All rights reserved.
+Copyright (C) 2007-2009 Burak Gürsoy <burak@cpan.org>. All rights reserved.
 
 This library is free software; you can redistribute it and/or modify 
-it under the same terms as Perl itself, either Perl version 5.8.8 or, 
+it under the same terms as Perl itself, either Perl version 5.10.1 or, 
 at your option, any later version of Perl 5 you may have available.
 {
     module_name    => 'PHP::Session::DBI',
     requires       => {
-         'PHP::Session' => '0.26',
-         'DBI'          => 0,
+        'PHP::Session' => '0.26',
+        'DBI'          => 0,
+        ( $] < 5.006 ? ( 'warnings::compat' => 0 ) : ()),
     },
     recommends     => {
          'DBD::mysql'   => 0,

File lib/PHP/Session/DBI.pm

 package PHP::Session::DBI;
 use strict;
+use warnings;
 use vars qw($VERSION);
 use base qw(PHP::Session);
 use Carp qw(croak);
 
-$VERSION = '0.24';
+$VERSION = '0.30';
 
 sub new {
    my($class, $sid, $opt) = @_;
-   croak "OPTIONS must be present and must be a HASHref" if ! _ishash($opt);
-   my $db_handle = delete($opt->{db_handle}) || croak "db_handle option is missing";
-   my $db_table  = delete($opt->{db_table})  || croak "db_table option is missing";
-   my $db_schema = delete($opt->{db_schema}) || croak "db_schema option is missing";
+   croak 'OPTIONS must be present and must be a HASHref' if ! _ishash($opt);
+   my $db_handle = delete($opt->{db_handle}) || croak 'db_handle option is missing';
+   my $db_table  = delete($opt->{db_table})  || croak 'db_table option is missing';
+   my $db_schema = delete($opt->{db_schema}) || croak 'db_schema option is missing';
    my $self      = $class->SUPER::new($sid, $opt);
    # inject our keys into session object
    $self->{db_handle}    = $db_handle;
    return $self;
 }
 
-sub dbh { shift->{db_handle} }
+sub dbh { return shift->{db_handle} }
 
 sub save {
    my $self    = shift;
-   my $encoded = $self->encode($self->{_data}) || '';
+   my $encoded = $self->encode($self->{_data}) || q{};
    my %schema  = $self->_db_schema;
    my($SQL, @params);
    if ($self->{_db_create}) {
-      $SQL = qq(
+      $SQL = <<"SQL";
          INSERT INTO $self->{db_table}
                 ( $schema{date}, $schema{data}, $schema{id} )
          VALUES (             ?,             ?,           ? )
-      );
+SQL
       @params = (time, $encoded, $self->id);
    }
    else {
       if ($schema{update_date}) {
-         $SQL = qq(
+         $SQL = <<"SQL";
             UPDATE $self->{db_table}
             SET    $schema{date} = ?,
                    $schema{data} = ?
             WHERE  $schema{id}   = ?
-         );
+SQL
          @params = (time, $encoded, $self->id);
       }
       else {
-         $SQL = qq(
+         $SQL = <<"SQL";
             UPDATE $self->{db_table}
             SET    $schema{data} = ?
             WHERE  $schema{id}   = ?
-         );
+SQL
          @params = ($encoded, $self->id);
       }
    }
    $self->dbh->do( $SQL, undef, @params )
-      or croak( "Can't update database: " . $self->dbh->errstr );
+      or croak( q{Can't update database: } . $self->dbh->errstr );
    $self->{_changed} = 0; # init
+   return;
 }
 
 sub destroy {
    my $self   = shift;
    my %schema = $self->_db_schema;
    my $SQL    = qq(DELETE FROM $self->{db_table} WHERE $schema{id} = ?);
-   $self->dbh->do( $SQL, undef, $self->id )
-      || croak("Can't delete session from database: " . $self->dbh->errstr);
+   return $self->dbh->do( $SQL, undef, $self->id )
+            || croak( q{Can't delete session from database: } . $self->dbh->errstr);
 }
 
 # private methods
 
-sub _ishash { $_[0] && ref($_[0]) && ref($_[0]) eq 'HASH' }
+sub _ishash {
+   my $x = shift;
+   return $x && ref $x && ref $x eq 'HASH';
+}
 
 sub _db_schema {
    my $self = shift;
-   my $test = $self->{db_schema} || croak("Database session are enabled, but db_schema is missing");
-   croak "db_schema must be a HASHref" if ! _ishash($test);
-   $test->{id}   || croak("id parameter in db_schema is missing");
-   $test->{data} || croak("data parameter in db_schema is missing");
-   $test->{date} || croak("date parameter in db_schema is missing");
+   my $test = $self->{db_schema} || croak('Database session are enabled, but db_schema is missing');
+   croak 'db_schema must be a HASHref' if ! _ishash($test);
+   $test->{id}   || croak('id parameter in db_schema is missing');
+   $test->{data} || croak('data parameter in db_schema is missing');
+   $test->{date} || croak('date parameter in db_schema is missing');
    return %{ $test };
 }
 
    my $cont = $self->_slurp_content;
    if (!$cont && !$self->{create}) {
       $self->{_db_create} = 0;
-      my $error = $self->dbh ? $self->dbh->errstr : '';
-      my $id    = $self->id || "<unknown sid>";
+      my $error = $self->dbh ? $self->dbh->errstr : q{};
+      my $id    = $self->id || '<unknown sid>';
       # $cont might be empty string, if this is a fresh session
       # in that case, _last_update must have a value
       if (! $self->{_last_update}) {
-         croak("DBH($id)", ": ", $error || "entry does not exist in the database");
+         croak("DBH($id)", ': ', $error || 'entry does not exist in the database');
       }
    }
    $self->{_changed}++ if !$cont;
    $self->{_data} = $self->decode($cont);
+   return;
 }
 
 sub _slurp_content {
    my $sid    = $self->id || return;
    my %schema = $self->_db_schema;
    my $SQL    = qq(SELECT * FROM $self->{db_table} WHERE $schema{id} = ?);
-   my $sth    = $self->dbh->prepare($SQL) || croak("sth error: "         . $self->dbh->errstr);
-      $sth->execute( $sid )               || croak("sth execute error: " . $self->dbh->errstr);
+   my $sth    = $self->dbh->prepare($SQL) || croak('sth error: '         . $self->dbh->errstr);
+      $sth->execute( $sid )               || croak('sth execute error: ' . $self->dbh->errstr);
    # SID does not exist
    my $session = $sth->fetchrow_hashref || do { $self->{_db_create}++; return };
       $sth->finish;

File t/03-simple.t

 #!/usr/bin/env perl -w
 # Simple test. Just try to use the module.
 use strict;
+use warnings;
 use Test::More qw( no_plan );
 BEGIN {
     use_ok('PHP::Session::DBI');