Completed
Branch development (b1b115)
by Johannes
10:28
created

TableCell::render()   F

Complexity

Conditions 23
Paths 1577

Size

Total Lines 153

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 153
rs 0
cc 23
nc 1577
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @package dompdf
4
 * @link    http://dompdf.github.com/
5
 * @author  Benj Carson <[email protected]>
6
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7
 */
8
namespace Dompdf\Renderer;
9
10
use Dompdf\Frame;
11
use Dompdf\FrameDecorator\Table;
12
13
/**
14
 * Renders table cells
15
 *
16
 * @package dompdf
17
 */
18
class TableCell extends Block
19
{
20
21
    /**
22
     * @param Frame $frame
23
     */
24
    function render(Frame $frame)
25
    {
26
        $style = $frame->get_style();
27
28
        if (trim($frame->get_node()->nodeValue) === "" && $style->empty_cells === "hide") {
29
            return;
30
        }
31
32
        $this->_set_opacity($frame->get_opacity($style->opacity));
33
        list($x, $y, $w, $h) = $frame->get_border_box();
34
35
        // Draw our background, border and content
36
        if (($bg = $style->background_color) !== "transparent") {
37
            $this->_canvas->filled_rectangle($x, $y, (float)$w, (float)$h, $bg);
38
        }
39
40
        if (($url = $style->background_image) && $url !== "none") {
41
            $this->_background_image($url, $x, $y, $w, $h, $style);
42
        }
43
44
        $table = Table::find_parent_table($frame);
45
46
        if ($table->get_style()->border_collapse !== "collapse") {
47
            $this->_render_border($frame);
48
            $this->_render_outline($frame);
49
            return;
50
        }
51
52
        // The collapsed case is slightly complicated...
53
        // @todo Add support for outlines here
54
55
        $cellmap = $table->get_cellmap();
56
        $cells = $cellmap->get_spanned_cells($frame);
57
58
        if (is_null($cells)) {
59
            return;
60
        }
61
62
        $num_rows = $cellmap->get_num_rows();
63
        $num_cols = $cellmap->get_num_cols();
64
65
        // Determine the top row spanned by this cell
66
        $i = $cells["rows"][0];
67
        $top_row = $cellmap->get_row($i);
68
69
        // Determine if this cell borders on the bottom of the table.  If so,
70
        // then we draw its bottom border.  Otherwise the next row down will
71
        // draw its top border instead.
72
        if (in_array($num_rows - 1, $cells["rows"])) {
73
            $draw_bottom = true;
74
            $bottom_row = $cellmap->get_row($num_rows - 1);
75
        } else {
76
            $draw_bottom = false;
77
        }
78
79
        // Draw the horizontal borders
80
        foreach ($cells["columns"] as $j) {
81
            $bp = $cellmap->get_border_properties($i, $j);
82
83
            $y = $top_row["y"] - $bp["top"]["width"] / 2;
84
85
            $col = $cellmap->get_column($j);
86
            $x = $col["x"] - $bp["left"]["width"] / 2;
87
            $w = $col["used-width"] + ($bp["left"]["width"] + $bp["right"]["width"]) / 2;
88
89
            if ($bp["top"]["style"] !== "none" && $bp["top"]["width"] > 0) {
90
                $widths = array(
91
                    (float)$bp["top"]["width"],
92
                    (float)$bp["right"]["width"],
93
                    (float)$bp["bottom"]["width"],
94
                    (float)$bp["left"]["width"]
95
                );
96
                $method = "_border_" . $bp["top"]["style"];
97
                $this->$method($x, $y, $w, $bp["top"]["color"], $widths, "top", "square");
98
            }
99
100
            if ($draw_bottom) {
101
                $bp = $cellmap->get_border_properties($num_rows - 1, $j);
102
                if ($bp["bottom"]["style"] === "none" || $bp["bottom"]["width"] <= 0) {
103
                    continue;
104
                }
105
106
                $y = $bottom_row["y"] + $bottom_row["height"] + $bp["bottom"]["width"] / 2;
107
108
                $widths = array(
109
                    (float)$bp["top"]["width"],
110
                    (float)$bp["right"]["width"],
111
                    (float)$bp["bottom"]["width"],
112
                    (float)$bp["left"]["width"]
113
                );
114
                $method = "_border_" . $bp["bottom"]["style"];
115
                $this->$method($x, $y, $w, $bp["bottom"]["color"], $widths, "bottom", "square");
116
117
            }
118
        }
119
120
        $j = $cells["columns"][0];
121
122
        $left_col = $cellmap->get_column($j);
123
124
        if (in_array($num_cols - 1, $cells["columns"])) {
125
            $draw_right = true;
126
            $right_col = $cellmap->get_column($num_cols - 1);
127
        } else {
128
            $draw_right = false;
129
        }
130
131
        // Draw the vertical borders
132
        foreach ($cells["rows"] as $i) {
133
            $bp = $cellmap->get_border_properties($i, $j);
134
135
            $x = $left_col["x"] - $bp["left"]["width"] / 2;
136
137
            $row = $cellmap->get_row($i);
138
139
            $y = $row["y"] - $bp["top"]["width"] / 2;
140
            $h = $row["height"] + ($bp["top"]["width"] + $bp["bottom"]["width"]) / 2;
141
142
            if ($bp["left"]["style"] !== "none" && $bp["left"]["width"] > 0) {
143
                $widths = array(
144
                    (float)$bp["top"]["width"],
145
                    (float)$bp["right"]["width"],
146
                    (float)$bp["bottom"]["width"],
147
                    (float)$bp["left"]["width"]
148
                );
149
150
                $method = "_border_" . $bp["left"]["style"];
151
                $this->$method($x, $y, $h, $bp["left"]["color"], $widths, "left", "square");
152
            }
153
154
            if ($draw_right) {
155
                $bp = $cellmap->get_border_properties($i, $num_cols - 1);
156
                if ($bp["right"]["style"] === "none" || $bp["right"]["width"] <= 0) {
157
                    continue;
158
                }
159
160
                $x = $right_col["x"] + $right_col["used-width"] + $bp["right"]["width"] / 2;
161
162
                $widths = array(
163
                    (float)$bp["top"]["width"],
164
                    (float)$bp["right"]["width"],
165
                    (float)$bp["bottom"]["width"],
166
                    (float)$bp["left"]["width"]
167
                );
168
169
                $method = "_border_" . $bp["right"]["style"];
170
                $this->$method($x, $y, $h, $bp["right"]["color"], $widths, "right", "square");
171
            }
172
        }
173
174
        $id = $frame->get_node()->getAttribute("id");
175
        if (strlen($id) > 0)  {
176
            $this->_canvas->add_named_dest($id);
177
        }
178
    }
179
}
180