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

StringContainerHelper::isOneOf()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3.072
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