Conditions | 11 |
Paths | 6 |
Total Lines | 95 |
Code Lines | 51 |
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 |
||
262 | public static function link($text) |
||
263 | { |
||
264 | /* |
||
265 | * Regular expression bits used by htmlEscapeAndLinkUrls() to match URLs. |
||
266 | */ |
||
267 | $rexScheme = 'https?://'; |
||
268 | // $rexScheme = "$rexScheme|ftp://"; // Uncomment this line to allow FTP addresses. |
||
269 | $rexDomain = '(?:[-a-zA-Z0-9]{1,63}\.)+[a-zA-Z][-a-zA-Z0-9]{1,62}'; |
||
270 | $rexIp = '(?:[1-9][0-9]{0,2}\.|0\.){3}(?:[1-9][0-9]{0,2}|0)'; |
||
271 | $rexPort = '(:[0-9]{1,5})?'; |
||
272 | $rexPath = '(/[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]*?)?'; |
||
273 | $rexQuery = '(\?[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?'; |
||
274 | $rexFragment = '(#[!$-/0-9:;=@_\':;!a-zA-Z\x7f-\xff]+?)?'; |
||
275 | $rexUsername = '[^]\\\\\x00-\x20\"(),:-<>[\x7f-\xff]{1,64}'; |
||
276 | $rexPassword = $rexUsername; // allow the same characters as in the username |
||
277 | $rexUrl = "($rexScheme)?(?:($rexUsername)(:$rexPassword)?@)?($rexDomain|$rexIp)($rexPort$rexPath$rexQuery$rexFragment)"; |
||
278 | $rexTrailPunct = "[)'?.!,;:]"; // valid URL characters which are not part of the URL if they appear at the very end |
||
279 | $rexNonUrl = "[^-_$+.!*'(),;/?:@=&a-zA-Z0-9]"; // characters that should never appear in a URL |
||
280 | $rexUrlLinker = "{\\b$rexUrl(?=$rexTrailPunct*($rexNonUrl|$))}"; |
||
281 | |||
282 | /** |
||
283 | * $validTlds is an associative array mapping valid TLDs to the value true. |
||
284 | * Since the set of valid TLDs is not static, this array should be updated |
||
285 | * from time to time. |
||
286 | * |
||
287 | * List source: http://data.iana.org/TLD/tlds-alpha-by-domain.txt |
||
288 | * Version 2015062200, last updated mon jun 22 07:07:02 2015 utc |
||
289 | */ |
||
290 | $validTlds = array_fill_keys(explode(' ', |
||
291 | '.abb .abbott .abogado .ac .academy .accenture .accountant .accountants .active .actor .ad .ads .adult .ae .aeg .aero .af .afl .ag .agency .ai .aig .airforce .al .allfinanz .alsace .am .amsterdam .an .android .ao .apartments .aq .aquarelle .ar .archi .army .arpa .as .asia .associates .at .attorney .au .auction .audio .auto .autos .aw .ax .axa .az .azure .ba .band .bank .bar .barclaycard .barclays .bargains .bauhaus .bayern .bb .bbc .bbva .bd .be .beer .berlin .best .bf .bg .bh .bharti .bi .bible .bid .bike .bing .bingo .bio .biz .bj .black .blackfriday .bloomberg .blue .bm .bmw .bn .bnpparibas .bo .boats .bond .boo .boutique .br .bridgestone .broker .brother .brussels .bs .bt .budapest .build .builders .business .buzz .bv .bw .by .bz .bzh .ca .cab .cafe .cal .camera .camp .cancerresearch .canon .capetown .capital .caravan .cards .care .career .careers .cars .cartier .casa .cash .casino .cat .catering .cbn .cc .cd .center .ceo .cern .cf .cfa .cfd .cg .ch .channel .chat .cheap .chloe .christmas .chrome .church .ci .cisco .citic .city .ck .cl .claims .cleaning .click .clinic .clothing .club .cm .cn .co .coach .codes .coffee .college .cologne .com .community .company .computer .condos .construction .consulting .contractors .cooking .cool .coop .corsica .country .coupons .courses .cr .credit .creditcard .cricket .crown .crs .cruises .cu .cuisinella .cv .cw .cx .cy .cymru .cyou .cz .dabur .dad .dance .date .dating .datsun .day .dclk .de .deals .degree .delivery .democrat .dental .dentist .desi .design .dev .diamonds .diet .digital .direct .directory .discount .dj .dk .dm .dnp .do .docs .dog .doha .domains .doosan .download .drive .durban .dvag .dz .earth .eat .ec .edu .education .ee .eg .email .emerck .energy .engineer .engineering .enterprises .epson .equipment .er .erni .es .esq .estate .et .eu .eurovision .eus .events .everbank .exchange .expert .exposed .express .fail .faith .fan .fans .farm .fashion .feedback .fi .film .finance .financial .firmdale .fish .fishing .fit .fitness .fj .fk .flights .florist .flowers .flsmidth .fly .fm .fo .foo .football .forex .forsale .foundation .fr .frl .frogans .fund .furniture .futbol .fyi .ga .gal .gallery .garden .gb .gbiz .gd .gdn .ge .gent .genting .gf .gg .ggee .gh .gi .gift .gifts .gives .gl .glass .gle .global .globo .gm .gmail .gmo .gmx .gn .gold .goldpoint .golf .goo .goog .google .gop .gov .gp .gq .gr .graphics .gratis .green .gripe .gs .gt .gu .guge .guide .guitars .guru .gw .gy .hamburg .hangout .haus .healthcare .help .here .hermes .hiphop .hitachi .hiv .hk .hm .hn .hockey .holdings .holiday .homedepot .homes .honda .horse .host .hosting .hotmail .house .how .hr .ht .hu .ibm .icbc .icu .id .ie .ifm .il .im .immo .immobilien .in .industries .infiniti .info .ing .ink .institute .insure .int .international .investments .io .iq .ir .irish .is .it .iwc .java .jcb .je .jetzt .jewelry .jlc .jll .jm .jo .jobs .joburg .jp .juegos .kaufen .kddi .ke .kg .kh .ki .kim .kitchen .kiwi .km .kn .koeln .komatsu .kp .kr .krd .kred .kw .ky .kyoto .kz .la .lacaixa .land .lasalle .lat .latrobe .lawyer .lb .lc .lds .lease .leclerc .legal .lgbt .li .liaison .lidl .life .lighting .limited .limo .link .lk .loan .loans .lol .london .lotte .lotto .love .lr .ls .lt .ltda .lu .lupin .luxe .luxury .lv .ly .ma .madrid .maif .maison .management .mango .market .marketing .markets .marriott .mba .mc .md .me .media .meet .melbourne .meme .memorial .men .menu .mg .mh .miami .microsoft .mil .mini .mk .ml .mm .mma .mn .mo .mobi .moda .moe .monash .money .montblanc .mormon .mortgage .moscow .motorcycles .mov .movie .mp .mq .mr .ms .mt .mtn .mtpc .mu .museum .mv .mw .mx .my .mz .na .nadex .nagoya .name .navy .nc .ne .nec .net .network .neustar .new .news .nexus .nf .ng .ngo .nhk .ni .nico .ninja .nissan .nl .no .np .nr .nra .nrw .ntt .nu .nyc .nz .okinawa .om .one .ong .onl .online .ooo .oracle .org .organic .osaka .otsuka .ovh .pa .page .panerai .paris .partners .parts .party .pe .pf .pg .ph .pharmacy .philips .photo .photography .photos .physio .piaget .pics .pictet .pictures .pink .pizza .pk .pl .place .play .plumbing .plus .pm .pn .pohl .poker .porn .post .pr .praxi .press .pro .prod .productions .prof .properties .property .ps .pt .pub .pw .py .qa .qpon .quebec .racing .re .realtor .recipes .red .redstone .rehab .reise .reisen .reit .ren .rent .rentals .repair .report .republican .rest .restaurant .review .reviews .rich .rio .rip .ro .rocks .rodeo .rs .rsvp .ru .ruhr .run .rw .ryukyu .sa .saarland .sale .samsung .sandvik .sandvikcoromant .sap .sarl .saxo .sb .sc .sca .scb .schmidt .scholarships .school .schule .schwarz .science .scot .sd .se .seat .sener .services .sew .sex .sexy .sg .sh .shiksha .shoes .show .shriram .si .singles .site .sj .sk .ski .sky .sl .sm .sn .sncf .so .soccer .social .software .sohu .solar .solutions .sony .soy .space .spiegel .spreadbetting .sr .st .statoil .study .style .su .sucks .supplies .supply .support .surf .surgery .suzuki .sv .swiss .sx .sy .sydney .systems .sz .taipei .tatar .tattoo .tax .taxi .tc .td .team .tech .technology .tel .temasek .tennis .tf .tg .th .thd .theater .tickets .tienda .tips .tires .tirol .tj .tk .tl .tm .tn .to .today .tokyo .tools .top .toray .toshiba .tours .town .toys .tr .trade .trading .training .travel .trust .tt .tui .tv .tw .tz .ua .ug .uk .university .uno .uol .us .uy .uz .va .vacations .vc .ve .vegas .ventures .versicherung .vet .vg .vi .viajes .video .villas .vision .vlaanderen .vn .vodka .vote .voting .voto .voyage .vu .wales .walter .wang .watch .webcam .website .wed .wedding .weir .wf .whoswho .wien .wiki .williamhill .win .windows .wme .work .works .world .ws .wtc .wtf .xbox .xerox .xin .xn--1qqw23a .xn--30rr7y .xn--3bst00m .xn--3ds443g .xn--3e0b707e .xn--45brj9c .xn--45q11c .xn--4gbrim .xn--55qw42g .xn--55qx5d .xn--6frz82g .xn--6qq986b3xl .xn--80adxhks .xn--80ao21a .xn--80asehdb .xn--80aswg .xn--90a3ac .xn--90ais .xn--9et52u .xn--b4w605ferd .xn--c1avg .xn--cg4bki .xn--clchc0ea0b2g2a9gcd .xn--czr694b .xn--czrs0t .xn--czru2d .xn--d1acj3b .xn--d1alf .xn--estv75g .xn--fiq228c5hs .xn--fiq64b .xn--fiqs8s .xn--fiqz9s .xn--fjq720a .xn--flw351e .xn--fpcrj9c3d .xn--fzc2c9e2c .xn--gecrj9c .xn--h2brj9c .xn--hxt814e .xn--i1b6b1a6a2e .xn--imr513n .xn--io0a7i .xn--j1amh .xn--j6w193g .xn--kcrx77d1x4a .xn--kprw13d .xn--kpry57d .xn--kput3i .xn--l1acc .xn--lgbbat1ad8j .xn--mgb9awbf .xn--mgba3a4f16a .xn--mgbaam7a8h .xn--mgbab2bd .xn--mgbayh7gpa .xn--mgbbh1a71e .xn--mgbc0a9azcg .xn--mgberp4a5d4ar .xn--mgbpl2fh .xn--mgbx4cd0ab .xn--mxtq1m .xn--ngbc5azd .xn--node .xn--nqv7f .xn--nqv7fs00ema .xn--nyqy26a .xn--o3cw4h .xn--ogbpf8fl .xn--p1acf .xn--p1ai .xn--pgbs0dh .xn--q9jyb4c .xn--qcka1pmc .xn--rhqv96g .xn--s9brj9c .xn--ses554g .xn--unup4y .xn--vermgensberater-ctb .xn--vermgensberatung-pwb .xn--vhquv .xn--vuq861b .xn--wgbh1c .xn--wgbl6a .xn--xhq521b .xn--xkc2al3hye2a .xn--xkc2dl3a5ee0h .xn--y9a3aq .xn--yfro4i67o .xn--ygbi2ammx .xn--zfr164b .xxx .xyz .yachts .yandex .ye .yodobashi .yoga .yokohama .youtube .yt .za .zip .zm .zone .zuerich .zw'), |
||
292 | true); |
||
293 | |||
294 | $html = ''; |
||
295 | |||
296 | $position = 0; |
||
297 | while (preg_match($rexUrlLinker, $text, $match, PREG_OFFSET_CAPTURE, $position)) { |
||
298 | list($url, $urlPosition) = $match[0]; |
||
299 | |||
300 | // Add the text leading up to the URL. |
||
301 | $html .= substr($text, $position, $urlPosition - $position); |
||
302 | |||
303 | $scheme = $match[1][0]; |
||
304 | $username = $match[2][0]; |
||
305 | $password = $match[3][0]; |
||
306 | $domain = $match[4][0]; |
||
307 | $afterDomain = $match[5][0]; // everything following the domain |
||
308 | $port = $match[6][0]; |
||
309 | $path = $match[7][0]; |
||
310 | |||
311 | // Check that the TLD is valid or that $domain is an IP address. |
||
312 | $tld = strtolower(strrchr($domain, '.')); |
||
313 | if (preg_match('{^\.[0-9]{1,3}$}', $tld) || isset($validTlds[$tld])) { |
||
314 | // Do not permit implicit scheme if a password is specified, as |
||
315 | // this causes too many errors (e.g. "my email:[email protected]"). |
||
316 | if (!$scheme && $password) { |
||
317 | $html .= $username; |
||
318 | |||
319 | // Continue text parsing at the ':' following the "username". |
||
320 | $position = $urlPosition + strlen($username); |
||
321 | continue; |
||
322 | } |
||
323 | |||
324 | if (!$scheme && $username && !$password && !$afterDomain) { |
||
325 | // Looks like an email address. |
||
326 | $completeUrl = "mailto:$url"; |
||
327 | $linkText = $url; |
||
328 | } else { |
||
329 | // Prepend http:// if no scheme is specified |
||
330 | $completeUrl = $scheme ? $url : "http://$url"; |
||
331 | $linkText = "$domain$port$path"; |
||
332 | } |
||
333 | |||
334 | $linkHtml = '<a href="'.htmlspecialchars($completeUrl).'" target="_blank">' |
||
335 | .$linkText |
||
336 | .'</a>'; |
||
337 | |||
338 | // Cheap e-mail obfuscation to trick the dumbest mail harvesters. |
||
339 | $linkHtml = str_replace('@', '@', $linkHtml); |
||
340 | |||
341 | // Add the hyperlink. |
||
342 | $html .= $linkHtml; |
||
343 | } else { |
||
344 | // Not a valid URL. |
||
345 | $html .= $url; |
||
346 | } |
||
347 | |||
348 | // Continue text parsing from after the URL. |
||
349 | $position = $urlPosition + strlen($url); |
||
350 | } |
||
351 | |||
352 | // Add the remainder of the text. |
||
353 | $html .= substr($text, $position); |
||
354 | |||
355 | return $html; |
||
356 | } |
||
357 | |||
563 |
The break statement is not necessary if it is preceded for example by a return statement:
If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.