Completed
Push — master ( dc2700...65f918 )
by Martin
22:42 queued 16:21
created

Table   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 86.84%

Importance

Changes 0
Metric Value
wmc 21
lcom 2
cbo 3
dl 0
loc 75
ccs 33
cts 38
cp 0.8684
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A acceptsLines() 0 4 1
A isCode() 0 4 1
A getCaption() 0 8 3
A getHead() 0 8 4
A getBody() 0 8 4
A matchesNextLine() 0 4 1
A handleRemainingContents() 0 3 1
A canContain() 0 4 2
A setCaption() 0 11 3
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-table-extension package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 * (c) Webuni s.r.o. <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Webuni\CommonMark\TableExtension;
14
15
use League\CommonMark\Block\Element\AbstractBlock;
16
use League\CommonMark\ContextInterface;
17
use League\CommonMark\Cursor;
18
19
class Table extends AbstractBlock
20
{
21
    private $parser;
22
23 16
    public function __construct(\Closure $parser)
24
    {
25 16
        parent::__construct();
26 16
        $this->appendChild(new TableRows(TableRows::TYPE_HEAD));
27 16
        $this->appendChild(new TableRows(TableRows::TYPE_BODY));
28 16
        $this->parser = $parser;
29 16
    }
30
31
    public function canContain(AbstractBlock $block)
32
    {
33
        return $block instanceof TableRows || $block instanceof TableCaption;
34
    }
35
36 16
    public function acceptsLines()
37
    {
38 16
        return true;
39
    }
40
41 16
    public function isCode()
42
    {
43 16
        return false;
44
    }
45
46 2
    public function setCaption(TableCaption $caption = null)
47
    {
48 2
        $node = $this->getCaption();
49 2
        if ($node instanceof TableCaption) {
50
            $node->detach();
51
        }
52
53 2
        if ($caption instanceof TableCaption) {
54 2
            $this->prependChild($caption);
55
        }
56 2
    }
57
58 6
    public function getCaption()
59
    {
60 6
        foreach ($this->children() as $child) {
61 6
            if ($child instanceof TableCaption) {
62 6
                return $child;
63
            }
64
        }
65 6
    }
66
67 16
    public function getHead()
68
    {
69 16
        foreach ($this->children() as $child) {
70 16
            if ($child instanceof TableRows && $child->isHead()) {
71 16
                return $child;
72
            }
73
        }
74
    }
75
76 16
    public function getBody()
77
    {
78 16
        foreach ($this->children() as $child) {
79 16
            if ($child instanceof TableRows && $child->isBody()) {
80 16
                return $child;
81
            }
82
        }
83
    }
84
85 16
    public function matchesNextLine(Cursor $cursor)
86
    {
87 16
        return call_user_func($this->parser, $cursor);
88
    }
89
90 16
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
91
    {
92 16
    }
93
}
94