Module::Checkstyle::Config - Handles configuration directives


Module-Checkstyle documentation Contained in the Module-Checkstyle distribution.

Index


Code Index:

NAME

Top

Module::Checkstyle::Config - Handles configuration directives

SYNOPSIS

Top

    use Module::Checkstyle::Config;
    my $config = Module::Checkstyle::Config->new();
    my $value = $config->get_directive('max-per-file');

DESCRIPTION

Top

METHODS

Top

new ($source)

Constructs a new Module::Checkstyle::Config object. The optional argument $source can be either a reference to a GLOB as in new(\*DATA), a reference to a scalar containing the configuration new(\$config) or a scalar containing a path to a configuration file.

If the $source is ommited it will look for the configuration file located at ~/.module-checkstyle/config.

get_enabled_sections

Returns a list of enabled sections.

get_severity ($check, $directive)

Returns the severity level for a given directive specified by $directive in the section specified by $check. If $check is ommited it will try to figure out what section to read from by investigating the callers package and removing Module::Checkstyle::Check.

get_directive ($check, $directive)

Returns a the directive specified by $directive in the section specified by $check. If $check is ommited it will try to figure out what section to read from by investigating the callers package and removing Module::Checkstyle::Check.

FORMAT

Top

Module::Checkstyle uses the INI file-format for its configuration file. The following example illustrates a sample config file:

 ; this is a sample config
 global-error-level = warn

 [Whitespace]
 after-comma     = true
 after-fat-comma = true

 [Package]
 max-per-file    = 1

Here we have a global configuration diretive, global-error-level, and a few directives applicable to a specified check.

SEVERITY

The directive global-error-level sets the severity of a style violation. If it's ommited it will default to 'warn'.

It is however possible to specify the severity on a per-config basis by prefixing the directives value with either 'warn' or 'error' as in matches-name = error qr/\w+/.

BOOLEAN DIRECTIVES

Some checks expect a boolean value when they read their config. Acceptable booleans are 1, y, yes and true.

REGEXP DIRECTIVES

Checks that matches names such as variable name or subroutine names expect a regular expression in the config.

To specify a regular expression the recommended way is to use the 'qr' operator as in matches-name = qr/\w+/. If you don't want to use another delimiter it is acceptable to specify the regular expression without 'qr' and using // as in matches-name = /\w+/.

SEE ALSO

Top

Config::Tiny Module::Checkstyle


Module-Checkstyle documentation Contained in the Module-Checkstyle distribution.

package Module::Checkstyle::Config;

use strict;
use warnings;
use Carp qw(croak);
use File::HomeDir;

use base qw(Config::Tiny);

sub new {
    my ($class, $file) = @_;

    # If we pass a Module::Checkstyle::Config object
    # we make that our config instead of cloning it.
    if (defined $file) {
        if (UNIVERSAL::isa($file, 'Module::Checkstyle::Config')) {
            return $file;
        }
    }
    
    # Resort to user-default config
    if (!defined $file) {
        $file = File::Spec->catfile(home(), '.module-checkstyle', 'config');
    }

    # Load the config or bail out
    my ($self, $name);
    if (ref $file eq 'GLOB') {
        my $config = join("", <$file>);
        $self = $class->SUPER::read_string($config);
    }
    elsif(ref $file eq 'SCALAR') {
        $self = $class->SUPER::read_string($$file);
    }
    elsif($file && -e $file && -f $file) {
        $self = $class->SUPER::read($file);
        $name = $file;
    }
    else {
        $self = $class->SUPER::new();        
    }

    if (!defined $self) {
        croak 'Failed to load config';
    }
    
    $self->{_}->{'_config-path'} = $name;
    if (!exists $self->{_}->{'global-error-level'} || $self->{_}->{'global-error-level'} !~ /^cricial|error|info|warn$/) {
        $self->{_}->{'global-error-level'} = 'warn';
    }

    $self->_fix();
    return $self;
}

sub _fix {
    my $self = shift;

  FIX_PROPERTIES:
    foreach my $section (keys %{$self}) {
        next FIX_PROPERTIES if $section eq '_';
        foreach my $property (keys %{$self->{$section}}) {
            if ($self->{$section}->{$property} =~ m/^ \s* (?:(critical|error|info|warn)\s+)? (.*?) $/ix) {
                $self->{_}->{_level}->{$section}->{$property} = $1;
                $self->{$section}->{$property} = $2;
            }
        }
    }
}

sub _get_check_and_property {
    my ($self, $check, $property) = @_;

    if (defined $check && !defined $property) {
        $property = $check;
        my $caller = caller(1);
        ($check) = $caller =~ m/^Module::Checkstyle::Check::(.*?)$/;
    }

    if (!defined $check) {
        croak "Can't determine check";
    }
    if (!defined $property) {
        croak "Can't determine property";
    }

    return ($self, $check, $property);
}

sub get_enabled_sections {
    my ($self) = @_;
    my @sections =  grep { $_ ne '_' } keys %$self;
    return @sections;
}

sub get_severity {
    my ($self, $check, $property) = &_get_check_and_property;

    my $level = $self->{_}->{_level}->{$check}->{$property};
    return $level || $self->get_directive('_', 'global-error-level');
}

sub get_directive {
    my ($self, $check, $property) = &_get_check_and_property;
    return $self->{$check}->{$property};
}

1;
__END__