Conditions | 2 |
Paths | 1 |
Total Lines | 60 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 |
||
63 | protected function modifyFilecontent($csscontent, $cssfilepath) |
||
64 | { |
||
65 | // Remove the path to docroot from full filepath and remove filename from cssfilepath |
||
66 | $relativeCssPath = dirname(preg_replace("#^".$this->docrootpath."#", "", $cssfilepath)); |
||
67 | |||
68 | // Use # insted of / in regexpress! |
||
69 | // Search for every url() expr in css files only if the start with ./ |
||
70 | // Don't matter if url in " or not |
||
71 | $csscontent = preg_replace_callback( |
||
72 | '#url\("?\./([^"]+)"?\)#i', |
||
73 | function ($matches) use ($relativeCssPath) |
||
74 | { |
||
75 | // $matches[1] contain first subpattern |
||
76 | return 'url("/'.$relativeCssPath.'/'.$matches[1].'")'; |
||
77 | }, |
||
78 | $csscontent |
||
79 | ); |
||
80 | |||
81 | // Now search for path with ../ sequences |
||
82 | $csscontent = preg_replace_callback( |
||
83 | '#url\(("|\')?(../){1,20}([^"\']+)("|\')?\)#i', |
||
84 | function ($matches) use ($relativeCssPath) |
||
85 | { |
||
86 | // $matches[0] contains whole matching pattern |
||
87 | // $matches[2] contains ../ subpattern !! but only one time !! |
||
88 | // $matches[3] contain path subpattern |
||
89 | |||
90 | // Now count only how much ../ in the begining of the string to avoid counting of ../ in the middle |
||
91 | |||
92 | // Where is the first . |
||
93 | $posFirstDot = strpos($matches[0], "."); |
||
94 | |||
95 | // Number of char that countain only ./ from the beginning of the string |
||
96 | $charCount = strspn($matches[0], "./", $posFirstDot); |
||
97 | |||
98 | // Cut the first part of the string |
||
99 | $pathstring = substr($matches[0], $posFirstDot, $charCount); |
||
100 | |||
101 | // Count the ../ |
||
102 | $pathdepth = substr_count($pathstring, '../'); |
||
103 | |||
104 | // Add starting slash, in case all folders has to be replaced, so the last can match in for loop |
||
105 | // later the / is need because we transform the relativ to absolut path, so we need a starting slash |
||
106 | $relativeCssPath = "/".$relativeCssPath; |
||
107 | |||
108 | // Remove all folder that dots stand for |
||
109 | for ($i=0; $i<$pathdepth; $i++) { |
||
110 | // find last occurrence of / in csspath, remove folder depth from last to first |
||
111 | $lastSlashPos = strrpos($relativeCssPath, "/"); |
||
112 | // remove the last folder now, substr replace everything to the end of the string as default |
||
113 | $relativeCssPath = substr_replace($relativeCssPath, "", $lastSlashPos); |
||
114 | } |
||
115 | |||
116 | return 'url("'.$relativeCssPath.'/'.$matches[3].'")'; |
||
117 | }, |
||
118 | $csscontent |
||
119 | ); |
||
120 | |||
121 | // return css content |
||
122 | return $csscontent; |
||
123 | } |
||
124 | } |