line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::PRT::Command::AddUse; |
2
|
2
|
|
|
2
|
|
2380
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
88
|
|
3
|
2
|
|
|
2
|
|
13
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
68
|
|
4
|
2
|
|
|
2
|
|
1190
|
use PPI; |
|
2
|
|
|
|
|
170337
|
|
|
2
|
|
|
|
|
698
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# Internal command to add use statement to file |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub new { |
9
|
23
|
|
|
23
|
0
|
15258
|
my ($class) = @_; |
10
|
23
|
|
|
|
|
150
|
bless { |
11
|
|
|
|
|
|
|
namespace => undef, |
12
|
|
|
|
|
|
|
}, $class; |
13
|
|
|
|
|
|
|
} |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# register a method |
16
|
|
|
|
|
|
|
# arguments: |
17
|
|
|
|
|
|
|
# $namespace: package name to use |
18
|
|
|
|
|
|
|
sub register { |
19
|
22
|
|
|
22
|
0
|
61
|
my ($self, $namespace) = @_; |
20
|
|
|
|
|
|
|
|
21
|
22
|
|
|
|
|
87
|
$self->{namespace} = $namespace; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub namespace { |
25
|
68
|
|
|
68
|
0
|
1194
|
my ($self) = @_; |
26
|
|
|
|
|
|
|
|
27
|
68
|
|
|
|
|
420
|
$self->{namespace}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# refactor a file |
31
|
|
|
|
|
|
|
# argumensts: |
32
|
|
|
|
|
|
|
# $file: filename for refactoring |
33
|
|
|
|
|
|
|
sub execute { |
34
|
22
|
|
|
22
|
0
|
10268
|
my ($self, $file) = @_; |
35
|
|
|
|
|
|
|
|
36
|
22
|
|
|
|
|
157
|
my $document = PPI::Document->new($file); |
37
|
22
|
50
|
|
|
|
145073
|
return unless $document; |
38
|
|
|
|
|
|
|
|
39
|
22
|
|
|
|
|
56
|
my $used = 0; |
40
|
22
|
|
|
|
|
49
|
my $last_use; |
41
|
22
|
|
|
|
|
106
|
my $include_statements = $document->find('PPI::Statement::Include'); |
42
|
|
|
|
|
|
|
|
43
|
22
|
100
|
|
|
|
52604
|
if ($include_statements) { |
44
|
17
|
|
|
|
|
55
|
for my $statement (@$include_statements) { |
45
|
49
|
50
|
|
|
|
206
|
next unless defined $statement->module; |
46
|
49
|
100
|
|
|
|
1343
|
if ($statement->module eq $self->namespace) { |
47
|
5
|
|
|
|
|
11
|
$used++; |
48
|
|
|
|
|
|
|
} |
49
|
49
|
|
|
|
|
120
|
$last_use = $statement; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
22
|
100
|
|
|
|
95
|
return if $used; |
53
|
|
|
|
|
|
|
|
54
|
17
|
|
33
|
|
|
760
|
my $insert_to = $last_use || $document->find_first('PPI::Statement::Package') || $document->find_first('PPI::Statement'); |
55
|
|
|
|
|
|
|
|
56
|
17
|
|
|
|
|
1307
|
my $tokens_to_insert = PPI::Document->new(\"\nuse @{[ $self->namespace ]};"); |
|
17
|
|
|
|
|
59
|
|
57
|
17
|
|
|
|
|
16552
|
$insert_to->add_element($tokens_to_insert); |
58
|
17
|
|
|
|
|
376
|
$document->save($file); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
1; |