Passed
Push — master ( 308d02...325a65 )
by Christian
02:19
created

LinkConverter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A callback() 0 6 1
A convert() 0 6 1
1
<?php declare(strict_types=1);
2
3
namespace Cocotte\Console\Documentation;
4
5
final class LinkConverter
6
{
7
8
    /**
9
     * https://stackoverflow.com/a/10002262/1476197
10
     * And I added a look behind assertion to not match already converted urls.
11
     */
12
    const PATTERN = '#(?<!\[|\()(?xi)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|'.
13
    '\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))#i';
14
15
    public function convert($string): string
16
    {
17
        return preg_replace_callback(
18
            self::PATTERN,
19
            $this->callback(),
20
            $string
21
        );
22
    }
23
24
    private function callback(): \Closure
25
    {
26
        return function ($matches) {
27
            $input = $matches[0];
28
29
            return "[$input]($input)";
30
        };
31
    }
32
}