Completed
Push — master ( 09a7d5...a22bfe )
by Mike
02:34
created

TableNodeRenderer::render()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 1
dl 0
loc 38
rs 8.6897
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of phpDocumentor.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link https://phpdoc.org
12
 */
13
14
namespace phpDocumentor\Guides\NodeRenderers\LaTeX;
15
16
use InvalidArgumentException;
17
use phpDocumentor\Guides\NodeRenderers\NodeRenderer;
18
use phpDocumentor\Guides\NodeRenderers\NodeRendererFactory;
19
use phpDocumentor\Guides\Nodes\Node;
20
use phpDocumentor\Guides\Nodes\SpanNode;
21
use phpDocumentor\Guides\Nodes\TableNode;
22
use function count;
23
use function get_class;
24
use function implode;
25
use function max;
26
27
class TableNodeRenderer implements NodeRenderer
28
{
29
    /** @var NodeRendererFactory */
30
    private $nodeRendererFactory;
31
32
    public function __construct(NodeRendererFactory $nodeRendererFactory)
33
    {
34
        $this->nodeRendererFactory = $nodeRendererFactory;
35
    }
36
37
    public function render(Node $node) : string
38
    {
39
        if ($node instanceof TableNode === false) {
40
            throw new InvalidArgumentException('Invalid node presented');
41
        }
42
43
        $cols = 0;
44
45
        $rows = [];
46
        foreach ($node->getData() as $row) {
47
            $rowTex = '';
48
            $cols = max($cols, count($row->getColumns()));
49
50
            /** @var SpanNode $col */
51
            foreach ($row->getColumns() as $n => $col) {
52
                $rowTex .= $this->nodeRendererFactory->get(get_class($col))->render($col);
0 ignored issues
show
Documentation introduced by
$col is of type object<phpDocumentor\Gui...odes\Table\TableColumn>, but the function expects a object<phpDocumentor\Guides\Nodes\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
53
54
                if ((int) $n + 1 >= count($row->getColumns())) {
55
                    continue;
56
                }
57
58
                $rowTex .= ' & ';
59
            }
60
61
            $rowTex .= ' \\\\' . "\n";
62
            $rows[] = $rowTex;
63
        }
64
65
        $aligns = [];
66
        for ($i = 0; $i < $cols; $i++) {
67
            $aligns[] = 'l';
68
        }
69
70
        $aligns = '|' . implode('|', $aligns) . '|';
71
        $rows = "\\hline\n" . implode("\\hline\n", $rows) . "\\hline\n";
72
73
        return "\\begin{tabular}{" . $aligns . "}\n" . $rows . "\n\\end{tabular}\n";
74
    }
75
}
76