CallbackTrait::getCallback()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 1
f 0
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