Matcher   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 106
Duplicated Lines 15.09 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 1 Features 1
Metric Value
wmc 12
c 7
b 1
f 1
lcom 1
cbo 4
dl 16
loc 106
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
C match() 16 90 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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