Conditions | 11 |
Paths | 60 |
Total Lines | 90 |
Code Lines | 49 |
Lines | 16 |
Ratio | 17.78 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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)) { |
|
|
|||
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 { |
|
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 |
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.