Completed
Push — master ( 3ff1a5...606db1 )
by Colin
28:59 queued 27:36
created

StringContainerHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 40
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 40
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
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Node;
15
16
final class StringContainerHelper
17
{
18
    /**
19
     * Extract text literals from all descendant nodes
20
     *
21
     * @param Node          $node         Parent node
22
     * @param array<string> $excludeTypes Optional list of node class types to exclude
23
     *
24
     * @return string Concatenated literals
25
     */
26 69
    public static function getChildText(Node $node, array $excludeTypes = []): string
27
    {
28 69
        $text = '';
29
30 69
        $walker = $node->walker();
31 69
        while ($event = $walker->next()) {
32 69
            if ($event->isEntering() && ($child = $event->getNode()) instanceof StringContainerInterface && ! self::isOneOf($child, $excludeTypes)) {
33 69
                $text .= $child->getLiteral();
34
            }
35
        }
36
37 69
        return $text;
38
    }
39
40
    /**
41
     * @param string[] $classesOrInterfacesToCheck
42
     *
43
     * @psalm-pure
44
     */
45 69
    private static function isOneOf(object $object, array $classesOrInterfacesToCheck): bool
46
    {
47 69
        foreach ($classesOrInterfacesToCheck as $type) {
48 33
            if ($object instanceof $type) {
49
                return true;
50
            }
51
        }
52
53 69
        return false;
54
    }
55
}
56