Conditions | 2 |
Paths | 1 |
Total Lines | 56 |
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 |
||
93 | "@$name", |
||
94 | false |
||
95 | ); |
||
96 | $string = str_replace("@$name", $link, $string); |
||
97 | } |
||
98 | |||
99 | return $string; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * Autolinks URLs not surrounded by explicit URL-tags for user-convenience. |
||
104 | * |
||
105 | * @param string $string The text to be parsed for URLs. |
||
106 | * @return string The text with URLs linked. |
||
107 | */ |
||
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 | ); |
||
192 |