CallbackTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 9
dl 0
loc 43
ccs 11
cts 11
cp 1
rs 10
c 1
b 1
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCallback() 0 3 1
A addCustomCallback() 0 5 1
A processCustomCallbacks() 0 9 3
1
<?php
2
3
namespace Vanderlee\Comprehend\Match\Output;
4
5
trait CallbackTrait
6
{
7
    /**
8
     * List of ordinary callbacks to process.
9
     *
10
     * @var callable[]
11
     */
12
    private $customCallbacks = [];
13
14
    /**
15
     * Add a callback to this match, to be called after parsing is finished and
16
     * only if this match was part of the matched rules.
17
     *
18
     * @param callable $callback
19
     *
20
     * @return $this
21
     */
22 401
    public function addCustomCallback(callable $callback)
23
    {
24 401
        $this->customCallbacks[] = $callback;
25
26 401
        return $this;
27
    }
28
29
    /**
30
     * Handle all registered custom callbacks for this match and any matches
31
     * at deeper levels of this match.
32
     */
33 373
    private function processCustomCallbacks()
34
    {
35
        /** @var self $success */
36 373
        foreach ($this->successes as $success) {
37 241
            $success->processCustomCallbacks();
38
        }
39
40 373
        foreach ($this->customCallbacks as $callback) {
41 371
            $callback();
42
        }
43 373
    }
44
45 373
    public function getCallback()
46
    {
47 373
        $this->processCustomCallbacks();
48 373
    }
49
}
50