Matcher::match()   C
last analyzed

Complexity

Conditions 11
Paths 60

Size

Total Lines 90
Code Lines 49

Duplication

Lines 16
Ratio 17.78 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
c 6
b 1
f 0
dl 16
loc 90
rs 5.2653
cc 11
eloc 49
nc 60
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Openl10n\Cli\File;
4
5
use Symfony\Component\Finder\Finder;
6
use Symfony\Component\Finder\Glob;
7
8
class Matcher
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $pattern;
14
15
    /**
16
     * @param string $pattern
17
     */
18
    public function __construct($pattern)
19
    {
20
        $this->pattern = $pattern;
21
    }
22
23
    public function match($inDir)
24
    {
25
        $results = [];
26
27
        // Pop out placeholders from given pattern
28
        preg_match_all('/<(?P<placeholder>\w+)>/', $this->pattern, $matches);
29
        $placeholders = $matches['placeholder'];
30
31
        // Replace placeholder by a valid string
32
        foreach ($placeholders as $placeholder) {
33
            $this->pattern = str_replace(
34
                "<${placeholder}>",
35
                "___${placeholder}_placeholder___",
36
                $this->pattern
37
            );
38
        }
39
40
        // Transform pattern to regex
41
        $regex = Glob::toRegex($this->pattern);
42
        $regex = trim($regex, '#');
43
44
        // Englobe every part of the regex into a matching pattern.
45
        // Then build final regex by concatening parts & placeholders.
46
        // Be sure to make copy of placeholders.
47
        $counter = 0;
48
        $parts = preg_split('/___\w+_placeholder___/', $regex);
49
        $placeholderStack = $placeholders;
50
51
        $regex = '';
52 View Code Duplication
        while ($part = array_shift($parts)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
            $counter++;
54
            $regex .= "(?P<___part_${counter}___>${part})";
55
56
            if (null !== $placeholder = array_shift($placeholderStack)) {
57
                $regex .= "(?P<${placeholder}>\w+)";
58
            }
59
        }
60
61
        // Re-add regex delimiters
62
        $regex = '#'.$regex.'#';
63
64
        // Find files
65
        $finder = new Finder();
66
        $finder->in($inDir)->path($regex);
67
        foreach ($finder->files() as $file) {
68
69
            // Replacing Windows backslashes by linux slashes
70
            $relativePathnameSanitized = str_replace("\\", "/", $file->getRelativePathname());
71
72
            if (!preg_match($regex, $relativePathnameSanitized, $matches)) {
73
                // Should not happen, but it better to check
74
                continue;
75
            }
76
77
            $parts = [];
78
            $attributes = [];
79
            foreach ($matches as $key => $match) {
80
                if (!is_string($key)) {
81
                    continue;
82
                }
83
84
                if (preg_match('/___part_\d+___/', $key)) {
85
                    $parts[] = $match;
86
                } else {
87
                    $attributes[$key] = $match;
88
                }
89
            }
90
91
92
            $filePattern = '';
93
            $placeholderStack = $placeholders; // make copy of placeholders
94
95 View Code Duplication
            do {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
                $part = array_shift($parts);
97
                $filePattern .= $part;
98
99
                if (null !== $placeholder = array_shift($placeholderStack)) {
100
                    $filePattern .= "<${placeholder}>";
101
                }
102
            } while(null !== $part);
103
104
            $results[] = new FileInfo(
105
                $inDir,
106
                new Pattern($filePattern),
107
                $attributes
108
            );
109
        }
110
111
        return $results;
112
    }
113
}
114