Conditions | 3 |
Paths | 1 |
Total Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
108 | protected function autolink(string $string): string |
||
109 | { |
||
110 | $replace = function (array $matches): string { |
||
111 | // don't link locally |
||
112 | if (strpos($matches['element'], 'file://') !== false) { |
||
113 | return $matches['element']; |
||
114 | } |
||
115 | |||
116 | // exclude punctuation at end of sentence from URLs |
||
117 | $ignoredEndChars = implode('|', [',', '\?', ',', '\.', '\)', '!']); |
||
118 | preg_match( |
||
119 | '/(?P<element>.*?)(?P<suffix>' . $ignoredEndChars . ')?$/', |
||
120 | $matches['element'], |
||
121 | $m |
||
122 | ); |
||
123 | // keep ['element'] and ['suffix'] and include ['prefix']; (array) for phpstan |
||
124 | $matches = (array)($m + $matches); |
||
125 | |||
126 | if (strpos($matches['element'], '://') === false) { |
||
127 | $matches['element'] = 'http://' . $matches['element']; |
||
128 | } |
||
129 | $matches += [ |
||
130 | 'prefix' => '', |
||
131 | 'suffix' => '' |
||
132 | ]; |
||
133 | |||
134 | $url = $this->_url( |
||
135 | $matches['element'], |
||
136 | $matches['element'], |
||
137 | false, |
||
138 | true |
||
139 | ); |
||
140 | |||
141 | return $matches['prefix'] . $url . $matches['suffix']; |
||
142 | }; |
||
143 | |||
144 | //# autolink http://urls |
||
145 | $string = preg_replace_callback( |
||
146 | "#(?<=^|[\n (])(?P<element>[\w]+?://.*?[^ \"\n\r\t<]*)#is", |
||
147 | $replace, |
||
148 | $string |
||
149 | ); |
||
150 | |||
151 | //# autolink without http://, i.e. www.foo.bar/baz |
||
152 | $string = preg_replace_callback( |
||
153 | "#(?P<prefix>^|[\n (])(?P<element>(www|ftp)\.[\w\-]+\.[\w\-.\~]+(?:/[^ \"\t\n\r<]*)?)#is", |
||
154 | $replace, |
||
155 | $string |
||
156 | ); |
||
157 | |||
158 | //# autolink email |
||
159 | $string = preg_replace_callback( |
||
160 | "#(?<=^|[\n ])(?P<content>([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+))#i", |
||
161 | function ($matches) { |
||
162 | return $this->_email($matches['content']); |
||
163 | }, |
||
164 | $string |
||
165 | ); |
||
166 | |||
167 | return $string; |
||
168 | } |
||
169 | |||
192 |