Completed
Push — 1.5 ( 2f97af...bd34d0 )
by Colin
55:03 queued 53:44
created

CallbackGenerator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 32
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A generateMention() 0 17 6
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Extension\Mention\Generator;
13
14
use League\CommonMark\Extension\Mention\Mention;
15
use League\CommonMark\Inline\Element\AbstractInline;
16
17
final class CallbackGenerator implements MentionGeneratorInterface
18
{
19
    /**
20
     * A callback function which sets the URL on the passed mention and returns the mention, return a new AbstractInline based object or null if the mention is not a match
21
     *
22
     * @var callable(Mention): ?AbstractInline
23
     */
24
    private $callback;
25
26 27
    public function __construct(callable $callback)
27
    {
28 27
        $this->callback = $callback;
29 27
    }
30
31 27
    public function generateMention(Mention $mention): ?AbstractInline
32
    {
33 27
        $result = \call_user_func_array($this->callback, [$mention]);
34 27
        if ($result === null) {
35 3
            return null;
36
        }
37
38 24
        if ($result instanceof AbstractInline && !($result instanceof Mention)) {
39 6
            return $result;
40
        }
41
42 18
        if ($result instanceof Mention && $result->hasUrl()) {
43 9
            return $mention;
44
        }
45
46 9
        throw new \RuntimeException('CallbackGenerator callable must set the URL on the passed mention and return the mention, return a new AbstractInline based object or null if the mention is not a match');
47
    }
48
}
49