Conditions | 11 |
Paths | 36 |
Total Lines | 79 |
Code Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
69 | public static function parseFile(string $file, string $defaultDomain, array &$translations): bool |
||
70 | { |
||
71 | if (!file_exists($file)) { |
||
72 | return false; |
||
73 | } |
||
74 | $content = file_get_contents($file); |
||
75 | if (empty($content)) { |
||
76 | return false; |
||
77 | } |
||
78 | |||
79 | $functions = [ |
||
80 | '__' => 0, // __( string $singular , ... $args ) |
||
81 | '__n' => 0, // __n( string $singular , string $plural , integer $count , ... $args ) |
||
82 | '__d' => 1, // __d( string $domain , string $msg , ... $args ) |
||
83 | '__dn' => 1, // __dn( string $domain , string $singular , string $plural , integer $count , ... $args ) |
||
84 | '__x' => 1, // __x( string $context , string $singular , ... $args ) |
||
85 | '__xn' => 1, // __xn( string $context , string $singular , string $plural , integer $count , ... $args ) |
||
86 | '__dx' => 2, // __dx( string $domain , string $context , string $msg , ... $args ) |
||
87 | '__dxn' => 2, // __dxn( string $domain , string $context , string $singular , string $plural , integer $count , ... $args ) |
||
88 | ]; |
||
89 | |||
90 | // temporarily replace "\'" with "|||||", fixString will replace "|||||" with "\'" |
||
91 | // this fixes wrongly matched data in the following regexp |
||
92 | $content = str_replace("\'", '|||||', $content); |
||
93 | |||
94 | $options = [ |
||
95 | 'open_parenthesis' => preg_quote('('), |
||
96 | 'quote' => preg_quote("'"), |
||
97 | 'double_quote' => preg_quote('"'), |
||
98 | ]; |
||
99 | |||
100 | foreach ($functions as $fname => $singularPosition) { |
||
101 | $capturePath = "'[^']*'"; |
||
102 | $doubleQuoteCapture = str_replace("'", $options['double_quote'], $capturePath); |
||
103 | $quoteCapture = str_replace("'", $options['quote'], $capturePath); |
||
104 | |||
105 | // phpcs:disable |
||
106 | $rgxp = '/' . $fname . '\s*' . $options['open_parenthesis'] . str_repeat('((?:' . $doubleQuoteCapture . ')|(?:' . $quoteCapture . '))\s*[,)]\s*', $singularPosition + 1) . '/'; |
||
107 | // phpcs:enable |
||
108 | |||
109 | $matches = []; |
||
110 | preg_match_all($rgxp, $content, $matches); |
||
111 | |||
112 | $limit = count($matches[0]); |
||
113 | for ($i = 0; $i < $limit; $i++) { |
||
114 | $domain = $defaultDomain; |
||
115 | $ctx = ''; |
||
116 | $str = self::unquoteString($matches[1][$i]); |
||
117 | |||
118 | if (strpos($fname, '__d') === 0) { |
||
119 | $domain = self::unquoteString($matches[1][$i]); |
||
120 | |||
121 | if (strpos($fname, '__dx') === 0) { |
||
122 | $ctx = self::unquoteString($matches[2][$i]); |
||
123 | $str = self::unquoteString($matches[3][$i]); |
||
124 | } else { |
||
125 | $str = self::unquoteString($matches[2][$i]); |
||
126 | } |
||
127 | } elseif (strpos($fname, '__x') === 0) { |
||
128 | $ctx = self::unquoteString($matches[1][$i]); |
||
129 | $str = self::unquoteString($matches[2][$i]); |
||
130 | } |
||
131 | $str = self::fixString($str); |
||
132 | |||
133 | if (!array_key_exists($domain, $translations)) { |
||
134 | $translations[$domain] = []; |
||
135 | } |
||
136 | |||
137 | if (!array_key_exists($str, $translations[$domain])) { |
||
138 | $translations[$domain][$str] = ['']; |
||
139 | } |
||
140 | |||
141 | if (!in_array($ctx, $translations[$domain][$str])) { |
||
142 | $translations[$domain][$str][] = $ctx; |
||
143 | } |
||
144 | } |
||
145 | } |
||
146 | |||
147 | return true; |
||
148 | } |
||
177 |