• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wickedOne / gitlab-perl-helpers / 9930703181

14 Jul 2024 09:03PM UTC coverage: 100.0%. Remained the same
9930703181

push

github

web-flow
regex fixes (#30)

- fixed regex bug which caused the script to step out of the teardown
  too early.

2 of 2 new or added lines in 1 file covered. (100.0%)

842 of 842 relevant lines covered (100.0%)

21.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/lib/GPH/Composer.pm
1
package GPH::Composer;
2

3
use strict;
2✔
4
use warnings FATAL => 'all';
2✔
5

6
sub new {
7
    my ($class, %args) = @_;
16✔
8

9
    exists($args{classmap}) or die "$!";
16✔
10

11
    my $self = {};
15✔
12

13
    bless $self, $class;
15✔
14

15
    $self->parseClassMap($args{classmap});
15✔
16

17
    return $self;
14✔
18
}
19

20
sub getClassMap {
21
    my $self = shift;
1✔
22

23
    return \$self->{classmap};
1✔
24
}
25

26
sub match {
27
    my ($self, $class, @paths) = @_;
52✔
28

29
    if (not defined $self->{classmap}->{$class}) {
52✔
30
        return 0;
5✔
31
    }
32

33
    foreach (@paths) {
47✔
34
        return 1 if $self->{classmap}->{$class} =~ /^[\/]?$_.*$/;
71✔
35
    }
36

37
    return 0;
28✔
38
}
39

40
sub getNamespaces {
41
    my ($self, @paths) = @_;
1✔
42
    my (%reversed, @result);
1✔
43
    %reversed = reverse %{$self->{classmap}};
1✔
44

45
    foreach my $path (@paths) {
1✔
46
        $path = '/' . $path if rindex $path, '/', 0;
3✔
47

48
        next if !defined $reversed{$path};
3✔
49
        push(@result, $reversed{$path});
2✔
50
    }
51

52
    return(@result);
1✔
53
};
54

55
sub parseClassMap {
56
    my ($self, $path) = @_;
15✔
57

58
    open(my $fh, '<', $path) or die "can't open classmap file $!";
15✔
59

60
    my @lines = <$fh>;
14✔
61

62
    close($fh);
14✔
63

64
    for my $line (@lines) {
14✔
65
        next unless $line =~ /\$baseDir\s\./;
840✔
66

67
        my ($class, $code_path) = split(/ => \$baseDir \. /, $line);
252✔
68
        $self->{classmap}{strip($class)} = strip($code_path);
252✔
69
    }
70
}
71

72
sub strip {
73
    my ($line) = @_;
504✔
74

75
    $line =~ s/^\s+|\s+$//g;
504✔
76
    $line =~ s/[',]//g;
504✔
77
    $line =~ s/\\\\/\\/g;
504✔
78

79
    return $line;
504✔
80
}
81

82
1;
83

84
__END__
85

86
=head1 NAME
87

88
GPH::Composer - parses and matches paths with a L<Composer|https://getcomposer.org/> classmap
89

90
=head1 SYNOPSIS
91

92
    use GPH::Composer;
93

94
    my $composer = GPH::PHPMD->new((
95
        classmap => './vendor/composer/autoload_classmap.php',
96
    ));
97

98
    print $composer->match('App\Service\Provider\FooProvider.php', ['/src/Service/Provider/']);
99

100
=head1 METHODS
101

102
=over 4
103

104
=item C<< -E<gt>new(%args) >>
105

106
the C<new> method creates a new GPH::Composer instance. it takes a hash of options, valid option keys include:
107

108
=over
109

110
=item classmap B<(required)>
111

112
path to classmap file
113

114
=back
115

116
=item C<< -E<gt>match($class, @paths) >>
117

118
matches a FQCN to the classmap limited by a collection of paths. returns C<1> on hit C<0> on miss.
119

120
=item C<< -E<gt>getClassMap() >>
121

122
returns reference to the parsed classmap hash.
123

124
=item C<< -E<gt>getNamespaces(@paths)  >>
125

126
returns a list of namespaces for given paths.
127

128
=item C<< -E<gt>parseClassMap() >> B<(internal)>
129

130
parses the classmap file with relevant paths (vendor dir is ignored) and stores it in a hash map.
131

132
=item C<< -E<gt>strip($line) >> B<(internal)>
133

134
cleanup leading & trailing spaces, escaped backslashes and quotes.
135

136
=back
137

138
=head1 AUTHOR
139

140
the GPH::Composer module was written by wicliff wolda <wicliff.wolda@gmail.com>
141

142
=head1 COPYRIGHT AND LICENSE
143

144
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
145

146
=cut
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc