Completed
Push — refactor-parsing ( adbb6b...fbe6de )
by Colin
08:21 queued 07:01
created

StringContainerHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 41
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getChildText() 0 13 5
A isOneOf() 0 10 3
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\Node;
13
14
final class StringContainerHelper
15
{
16
    /**
17
     * Extract text literals from all descendant nodes
18
     *
19
     * @param Node          $node         Parent node
20
     * @param array<string> $excludeTypes Optional list of node class types to exclude
21
     *
22
     * @return string Concatenated literals
23
     */
24 57
    public static function getChildText(Node $node, array $excludeTypes = []): string
25
    {
26 57
        $text = '';
27
28 57
        $walker = $node->walker();
29 57
        while ($event = $walker->next()) {
30 57
            if ($event->isEntering() && ($child = $event->getNode()) instanceof StringContainerInterface && !self::isOneOf($child, $excludeTypes)) {
31 57
                $text .= $child->getLiteral();
32
            }
33
        }
34
35 57
        return $text;
36
    }
37
38
    /**
39
     * @param object   $object
40
     * @param string[] $classesOrInterfacesToCheck
41
     *
42
     * @return bool
43
     */
44 57
    private static function isOneOf(object $object, array $classesOrInterfacesToCheck): bool
45
    {
46 57
        foreach ($classesOrInterfacesToCheck as $type) {
47 57
            if ($object instanceof $type) {
48
                return true;
49
            }
50
        }
51
52 57
        return false;
53
    }
54
}
55