Completed
Push — master ( e1e275...3d6629 )
by Martin
20:32
created

Table   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 21
c 5
b 0
f 2
lcom 2
cbo 3
dl 0
loc 75
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A acceptsLines() 0 4 1
A isCode() 0 4 1
A canContain() 0 4 2
A setCaption() 0 11 3
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
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
    public function __construct(\Closure $parser)
24
    {
25
        parent::__construct();
26
        $this->appendChild(new TableRows(TableRows::TYPE_HEAD));
27
        $this->appendChild(new TableRows(TableRows::TYPE_BODY));
28
        $this->parser = $parser;
29
    }
30
31
    public function canContain(AbstractBlock $block)
32
    {
33
        return $block instanceof TableRows || $block instanceof TableCaption;
34
    }
35
36
    public function acceptsLines()
37
    {
38
        return true;
39
    }
40
41
    public function isCode()
42
    {
43
        return false;
44
    }
45
46
    public function setCaption(TableCaption $caption = null)
47
    {
48
        $node = $this->getCaption();
49
        if ($node instanceof TableCaption) {
50
            $node->detach();
51
        }
52
53
        if ($caption instanceof TableCaption) {
54
            $this->prependChild($caption);
55
        }
56
    }
57
58
    public function getCaption()
59
    {
60
        foreach ($this->children() as $child) {
61
            if ($child instanceof TableCaption) {
62
                return $child;
63
            }
64
        }
65
    }
66
67
    public function getHead()
68
    {
69
        foreach ($this->children() as $child) {
70
            if ($child instanceof TableRows && $child->isHead()) {
71
                return $child;
72
            }
73
        }
74
    }
75
76
    public function getBody()
77
    {
78
        foreach ($this->children() as $child) {
79
            if ($child instanceof TableRows && $child->isBody()) {
80
                return $child;
81
            }
82
        }
83
    }
84
85
    public function matchesNextLine(Cursor $cursor)
86
    {
87
        return call_user_func($this->parser, $cursor);
88
    }
89
90
    public function handleRemainingContents(ContextInterface $context, Cursor $cursor)
91
    {
92
    }
93
}
94