| Conditions | 6 |
| Paths | 1 |
| Total Lines | 75 |
| Code Lines | 42 |
| 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 | |||
| 124 | /// exclude ignored end chars if paired in URL foo.com/bar_(baz) |
||
| 125 | if (!empty($m['suffix'])) { |
||
| 126 | $ignoredIfNotPaired = [ |
||
| 127 | ['open' => '(', 'close' => ')'], |
||
| 128 | ]; |
||
| 129 | foreach ($ignoredIfNotPaired as $pair) { |
||
| 130 | $isUnpaired = substr_count($m['element'], $pair['open']) > substr_count($m['element'], $pair['close']); |
||
| 131 | if ($isUnpaired) { |
||
| 132 | $m['element'] .= $m['suffix']; |
||
| 133 | unset($m['suffix']); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | /// keep ['element'] and ['suffix'] and include ['prefix']; (array) for phpstan |
||
| 139 | $matches = (array)($m + $matches); |
||
| 140 | |||
| 141 | if (strpos($matches['element'], '://') === false) { |
||
| 142 | $matches['element'] = 'http://' . $matches['element']; |
||
| 143 | } |
||
| 144 | $matches += [ |
||
| 145 | 'prefix' => '', |
||
| 146 | 'suffix' => '', |
||
| 147 | ]; |
||
| 148 | |||
| 149 | $url = $this->_url( |
||
| 150 | $matches['element'], |
||
| 151 | $matches['element'], |
||
| 152 | false, |
||
| 153 | true |
||
| 154 | ); |
||
| 155 | |||
| 156 | return $matches['prefix'] . $url . $matches['suffix']; |
||
| 157 | }; |
||
| 158 | |||
| 159 | //# autolink http://urls |
||
| 160 | $string = preg_replace_callback( |
||
| 161 | "#(?<=^|[\n (])(?P<element>[\w]+?://.*?[^ \"\n\r\t<]*)#is", |
||
| 162 | $replace, |
||
| 163 | $string |
||
| 164 | ); |
||
| 165 | |||
| 166 | //# autolink without http://, i.e. www.foo.bar/baz |
||
| 167 | $string = preg_replace_callback( |
||
| 168 | "#(?P<prefix>^|[\n (])(?P<element>(www|ftp)\.[\w\-]+\.[\w\-.\~]+(?:/[^ \"\t\n\r<]*)?)#is", |
||
| 169 | $replace, |
||
| 170 | $string |
||
| 171 | ); |
||
| 172 | |||
| 173 | //# autolink email |
||
| 174 | $string = preg_replace_callback( |
||
| 175 | "#(?<=^|[\n ])(?P<content>([a-z0-9&\-_.]+?)@([\w\-]+\.([\w\-\.]+\.)*[\w]+))#i", |
||
| 176 | function ($matches) { |
||
| 177 | return $this->_email($matches['content']); |
||
| 178 | }, |
||
| 179 | $string |
||
| 180 | ); |
||
| 181 | |||
| 182 | return $string; |
||
| 183 | } |
||
| 207 |