Modified source files and compiled any and armel versions of packages
[pkg-perl] / deb-src / libperl-critic-perl / libperl-critic-perl-1.088 / lib / Perl / Critic / ProfilePrototype.pm
1 ##############################################################################
2 #      $URL: http://perlcritic.tigris.org/svn/perlcritic/branches/Perl-Critic-With-Param-Validation/lib/Perl/Critic/PolicyListing.pm $
3 #     $Date: 2006-12-13 21:35:21 -0800 (Wed, 13 Dec 2006) $
4 #   $Author: thaljef $
5 # $Revision: 1089 $
6 ##############################################################################
7
8 package Perl::Critic::ProfilePrototype;
9
10 use 5.006001;
11 use strict;
12 use warnings;
13
14 use English qw(-no_match_vars);
15
16 use Perl::Critic::Config qw{};
17 use Perl::Critic::Policy qw{};
18 use Perl::Critic::Utils qw{ :characters };
19 use overload ( q{""} => 'to_string' );
20
21 our $VERSION = '1.088';
22
23 #-----------------------------------------------------------------------------
24
25 sub new {
26     my ($class, %args) = @_;
27     my $self = bless {}, $class;
28
29     my $policies = $args{-policies} || [];
30     $self->{_policies} = [ sort _by_type @{ $policies } ];
31
32     my $comment_out_parameters = $args{'-comment-out-parameters'};
33     if (not defined $comment_out_parameters) {
34         $comment_out_parameters = 1;
35     }
36     $self->{_comment_out_parameters} = $comment_out_parameters;
37
38     my $configuration = $args{'-config'};
39     if (not $configuration) {
40         $configuration = Perl::Critic::Config->new(-profile => $EMPTY);
41     }
42     $self->{_configuration} = $configuration;
43
44
45     return $self;
46 }
47
48 #-----------------------------------------------------------------------------
49
50 sub _get_policies {
51     my ($self) = @_;
52
53     return $self->{_policies};
54 }
55
56 sub _comment_out_parameters {
57     my ($self) = @_;
58
59     return $self->{_comment_out_parameters};
60 }
61
62 sub _configuration {
63     my ($self) = @_;
64
65     return $self->{_configuration};
66 }
67
68 #-----------------------------------------------------------------------------
69
70 sub _line_prefix {
71     my ($self) = @_;
72
73     return $self->_comment_out_parameters() ? q{# } : $EMPTY;
74 }
75
76 #-----------------------------------------------------------------------------
77
78 sub to_string {
79     my ($self) = @_;
80
81     my $prefix = $self->_line_prefix();
82     my $configuration = $self->_configuration();
83
84     my $prototype = "# Globals\n";
85
86     $prototype .= $prefix;
87     $prototype .= q{severity = };
88     $prototype .= $configuration->severity();
89     $prototype .= "\n";
90
91     $prototype .= $prefix;
92     $prototype .= q{force = };
93     $prototype .= $configuration->force();
94     $prototype .= "\n";
95
96     $prototype .= $prefix;
97     $prototype .= q{only = };
98     $prototype .= $configuration->only();
99     $prototype .= "\n";
100
101     $prototype .= $prefix;
102     $prototype .= q{profile-strictness = };
103     $prototype .= $configuration->profile_strictness();
104     $prototype .= "\n";
105
106     $prototype .= $prefix;
107     $prototype .= q{color = };
108     $prototype .= $configuration->color();
109     $prototype .= "\n";
110
111     $prototype .= $prefix;
112     $prototype .= q{top = };
113     $prototype .= $configuration->top();
114     $prototype .= "\n";
115
116     $prototype .= $prefix;
117     $prototype .= q{verbose = };
118     $prototype .= $configuration->verbose();
119     $prototype .= "\n";
120
121     $prototype .= $prefix;
122     $prototype .= q{include = };
123     $prototype .= join $SPACE, $configuration->include();
124     $prototype .= "\n";
125
126     $prototype .= $prefix;
127     $prototype .= q{exclude = };
128     $prototype .= join $SPACE, $configuration->exclude();
129     $prototype .= "\n";
130
131     $prototype .= $prefix;
132     $prototype .= q{single-policy = };
133     $prototype .= join $SPACE, $configuration->single_policy();
134     $prototype .= "\n";
135
136     $prototype .= $prefix;
137     $prototype .= q{theme = };
138     $prototype .= $configuration->theme()->rule();
139     $prototype .= "\n";
140
141     Perl::Critic::Policy::set_format( $self->_proto_format() );
142
143     return $prototype . "\n" . join q{}, map { "$_" } @{ $self->_get_policies() };
144 }
145
146 #-----------------------------------------------------------------------------
147
148 # About "%{\\n%\\x7b# \\x7df\n${prefix}%n = %D\\n}O" below:
149 #
150 # The %0 format for a policy specifies how to format parameters.
151 # For a parameter %f specifies the full description.
152 #
153 # The problem is that both of these need to take options, but String::Format
154 # doesn't allow nesting of {}.  So, to get the option to the %f, the braces
155 # are hex encoded.  I.e., assuming that comment_out_parameters is in effect,
156 # the parameter sees:
157 #
158 #    \n%{# }f\n# %n = %D\n
159
160 sub _proto_format {
161     my ($self) = @_;
162
163     my $prefix = $self->_line_prefix();
164
165     return <<"END_OF_FORMAT";
166 # %a
167 [%p]
168 ${prefix}set_themes                         = %t
169 ${prefix}add_themes                         =
170 ${prefix}severity                           = %s
171 ${prefix}maximum_violations_per_document    = %v
172 %{\\n%\\x7b# \\x7df\\n${prefix}%n = %D\\n}O%{${prefix}Cannot programmatically discover what parameters this policy takes.\\n}U
173
174 END_OF_FORMAT
175
176 }
177
178 #-----------------------------------------------------------------------------
179
180 sub _by_type { return ref $a cmp ref $b }
181
182 1;
183
184 __END__
185
186 =pod
187
188 =head1 NAME
189
190 Perl::Critic::ProfilePrototype - Generate an initial Perl::Critic profile.
191
192 =head1 DESCRIPTION
193
194 This is a helper class that generates a prototype of a L<Perl::Critic> profile
195 (e.g. a F<.perlcriticrc> file. There are no user-serviceable parts here.
196
197 =head1 CONSTRUCTOR
198
199 =over 8
200
201 =item C<< new( -policies => \@POLICY_OBJECTS ) >>
202
203 Returns a reference to a new C<Perl::Critic::ProfilePrototype> object.
204
205 =back
206
207 =head1 METHODS
208
209 =over 8
210
211 =item to_string()
212
213 Returns a string representation of this C<ProfilePrototype>.  See
214 L<"OVERLOADS"> for more information.
215
216 =back
217
218 =head1 OVERLOADS
219
220 When a L<Perl::Critic::ProfilePrototype> is evaluated in string context, it
221 produces a multi-line summary of the policy name, default themes, and default
222 severity for each L<Perl::Critic::Policy> object that was given to the
223 constructor of this C<ProfilePrototype>.  If the Policy supports an additional
224 parameters, they will also be listed (but commented-out).  The format is
225 suitable for use as a F<.perlcriticrc> file.
226
227 =head1 AUTHOR
228
229 Jeffrey Ryan Thalhammer <thaljef@cpan.org>
230
231 =head1 COPYRIGHT
232
233 Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer.  All rights reserved.
234
235 This program is free software; you can redistribute it and/or modify
236 it under the same terms as Perl itself.  The full text of this license
237 can be found in the LICENSE file included with this module.
238
239 =cut
240
241 # Local Variables:
242 #   mode: cperl
243 #   cperl-indent-level: 4
244 #   fill-column: 78
245 #   indent-tabs-mode: nil
246 #   c-indentation-style: bsd
247 # End:
248 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :