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

LinkConverter::callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
}