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