Passed
Push — 1.10.x ( e22269...acc912 )
by
unknown
49:56
created

HTML_Table_Storage::updateAllAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4
/**
5
 * Storage class for HTML::Table data
6
 *
7
 * This class stores data for tables built with HTML_Table. When having
8
 * more than one instance, it can be used for grouping the table into the
9
 * parts <thead>...</thead>, <tfoot>...</tfoot> and <tbody>...</tbody>.
10
 *
11
 * PHP versions 4 and 5
12
 *
13
 * LICENSE:
14
 *
15
 * Copyright (c) 2005-2007, Adam Daniel <[email protected]>,
16
 *                          Bertrand Mansion <[email protected]>,
17
 *                          Mark Wiesemann <[email protected]>
18
 * All rights reserved.
19
 *
20
 * Redistribution and use in source and binary forms, with or without
21
 * modification, are permitted provided that the following conditions
22
 * are met:
23
 *
24
 *    * Redistributions of source code must retain the above copyright
25
 *      notice, this list of conditions and the following disclaimer.
26
 *    * Redistributions in binary form must reproduce the above copyright
27
 *      notice, this list of conditions and the following disclaimer in the
28
 *      documentation and/or other materials provided with the distribution.
29
 *    * The names of the authors may not be used to endorse or promote products
30
 *      derived from this software without specific prior written permission.
31
 *
32
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
34
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
35
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
36
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
40
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
 *
44
 * @category   HTML
45
 * @package    HTML_Table
46
 * @author     Adam Daniel <[email protected]>
47
 * @author     Bertrand Mansion <[email protected]>
48
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
49
 * @version    CVS: $Id: Storage.php,v 1.16 2007/04/29 16:31:06 wiesemann Exp $
50
 * @link       http://pear.php.net/package/HTML_Table
51
 */
52
53
/**
54
 * Storage class for HTML::Table data
55
 *
56
 * This class stores data for tables built with HTML_Table. When having
57
 * more than one instance, it can be used for grouping the table into the
58
 * parts <thead>...</thead>, <tfoot>...</tfoot> and <tbody>...</tbody>.
59
 *
60
 * @category   HTML
61
 * @package    HTML_Table
62
 * @author     Adam Daniel <[email protected]>
63
 * @author     Bertrand Mansion <[email protected]>
64
 * @author     Mark Wiesemann <[email protected]>
65
 * @copyright  2005-2006 The PHP Group
66
 * @license    http://www.opensource.org/licenses/bsd-license.php New BSD License
67
 * @version    Release: @package_version@
68
 * @link       http://pear.php.net/package/HTML_Table
69
 */
70
class HTML_Table_Storage extends HTML_Common {
71
72
    /**
73
     * Value to insert into empty cells
74
     * @var    string
75
     * @access private
76
     */
77
    var $_autoFill = '&nbsp;';
78
79
    /**
80
     * Automatically adds a new row or column if a given row or column index
81
     * does not exist
82
     * @var    bool
83
     * @access private
84
     */
85
    var $_autoGrow = true;
86
87
    /**
88
     * Array containing the table structure
89
     * @var     array
90
     * @access  private
91
     */
92
    var $_structure = array();
93
94
    /**
95
     * Number of rows composing in the table
96
     * @var     int
97
     * @access  private
98
     */
99
    var $_rows = 0;
100
101
    /**
102
     * Number of column composing the table
103
     * @var     int
104
     * @access  private
105
     */
106
    var $_cols = 0;
107
108
    /**
109
     * Tracks the level of nested tables
110
     * @var    int
111
     * @access private
112
     */
113
    var $_nestLevel = 0;
114
115
    /**
116
     * Whether to use <thead>, <tfoot> and <tbody> or not
117
     * @var    bool
118
     * @access private
119
     */
120
    var $_useTGroups = false;
121
122
    /**
123
     * Class constructor
124
     * @param    int      $tabOffset
125
     * @param    bool     $useTGroups        Whether to use <thead>, <tfoot> and
126
     *                                       <tbody> or not
127
     * @access   public
128
     */
129
    public function __construct($tabOffset = 0, $useTGroups = false)
130
    {
131
        parent::__construct(null, (int)$tabOffset);
132
        $this->_useTGroups = (boolean)$useTGroups;
133
    }
134
135
    /**
136
     * Sets the useTGroups value
137
     * @param   boolean   $useTGroups
138
     * @access  public
139
     */
140
    public function setUseTGroups($useTGroups)
141
    {
142
        $this->_useTGroups = $useTGroups;
143
    }
144
145
    /**
146
     * Returns the useTGroups value
147
     * @access   public
148
     * @return   boolean
149
     */
150
    public function getUseTGroups()
151
    {
152
        return $this->_useTGroups;
153
    }
154
155
    /**
156
     * Sets the autoFill value
157
     * @param   mixed   $fill
158
     * @access  public
159
     */
160
    public function setAutoFill($fill)
161
    {
162
        $this->_autoFill = $fill;
163
    }
164
165
    /**
166
     * Returns the autoFill value
167
     * @access   public
168
     * @return   mixed
169
     */
170
    public function getAutoFill()
171
    {
172
        return $this->_autoFill;
173
    }
174
175
    /**
176
     * Sets the autoGrow value
177
     * @param    bool   $fill
0 ignored issues
show
Bug introduced by
There is no parameter named $fill. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
178
     * @access   public
179
     */
180
    public function setAutoGrow($grow)
181
    {
182
        $this->_autoGrow = $grow;
183
    }
184
185
    /**
186
     * Returns the autoGrow value
187
     * @access   public
188
     * @return   mixed
189
     */
190
    public function getAutoGrow()
191
    {
192
        return $this->_autoGrow;
193
    }
194
195
    /**
196
     * Sets the number of rows in the table
197
     * @param    int     $rows
198
     * @access   public
199
     */
200
    function setRowCount($rows)
201
    {
202
        $this->_rows = $rows;
203
    }
204
205
    /**
206
     * Sets the number of columns in the table
207
     * @param    int     $cols
208
     * @access   public
209
     */
210
    function setColCount($cols)
211
    {
212
        $this->_cols = $cols;
213
    }
214
215
    /**
216
     * Returns the number of rows in the table
217
     * @access   public
218
     * @return   int
219
     */
220
    function getRowCount()
221
    {
222
        return $this->_rows;
223
    }
224
225
    /**
226
     * Gets the number of columns in the table
227
     *
228
     * If a row index is specified, the count will not take
229
     * the spanned cells into account in the return value.
230
     *
231
     * @param    int    Row index to serve for cols count
232
     * @access   public
233
     * @return   int
234
     */
235
    function getColCount($row = null)
236
    {
237
        if (!is_null($row)) {
238
            $count = 0;
239
            foreach ($this->_structure[$row] as $cell) {
240
                if (is_array($cell)) {
241
                    $count++;
242
                }
243
            }
244
            return $count;
245
        }
246
        return $this->_cols;
247
    }
248
249
    /**
250
     * Sets a rows type 'TH' or 'TD'
251
     * @param    int         $row    Row index
252
     * @param    string      $type   'TH' or 'TD'
253
     * @access   public
254
     */
255
256
    function setRowType($row, $type)
257
    {
258
        for ($counter = 0; $counter < $this->_cols; $counter++) {
259
            $this->_structure[$row][$counter]['type'] = $type;
260
        }
261
    }
262
263
    /**
264
     * Sets a columns type 'TH' or 'TD'
265
     * @param    int         $col    Column index
266
     * @param    string      $type   'TH' or 'TD'
267
     * @access   public
268
     */
269
    function setColType($col, $type)
270
    {
271
        for ($counter = 0; $counter < $this->_rows; $counter++) {
272
            $this->_structure[$counter][$col]['type'] = $type;
273
        }
274
    }
275
276
    /**
277
     * Sets the cell attributes for an existing cell.
278
     *
279
     * If the given indices do not exist and autoGrow is true then the given
280
     * row and/or col is automatically added.  If autoGrow is false then an
281
     * error is returned.
282
     * @param    int        $row         Row index
283
     * @param    int        $col         Column index
284
     * @param    mixed      $attributes  Associative array or string of table
285
     *                                   row attributes
286
     * @access   public
287
     * @throws   PEAR_Error
288
     */
289 View Code Duplication
    function setCellAttributes($row, $col, $attributes)
290
    {
291
        if (   isset($this->_structure[$row][$col])
292
            && $this->_structure[$row][$col] == '__SPANNED__'
293
        ) {
294
            return;
295
        }
296
        $attributes = $this->_parseAttributes($attributes);
297
        $err = $this->_adjustEnds($row, $col, 'setCellAttributes', $attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by $this->_parseAttributes($attributes) on line 296 can also be of type null; however, HTML_Table_Storage::_adjustEnds() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
Are you sure the assignment to $err is correct as $this->_adjustEnds($row,...tributes', $attributes) (which targets HTML_Table_Storage::_adjustEnds()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
298
        if (PEAR::isError($err)) {
299
            return $err;
300
        }
301
        $this->_structure[$row][$col]['attr'] = $attributes;
302
        // Fix use of rowspan/colspan
303
        //$this->_updateSpanGrid($row, $col);
304
    }
305
306
    /**
307
     * Updates the cell attributes passed but leaves other existing attributes
308
     * intact
309
     * @param    int     $row         Row index
310
     * @param    int     $col         Column index
311
     * @param    mixed   $attributes  Associative array or string of table row
312
     *                                attributes
313
     * @access   public
314
     */
315 View Code Duplication
    function updateCellAttributes($row, $col, $attributes)
316
    {
317
        if (   isset($this->_structure[$row][$col])
318
            && $this->_structure[$row][$col] == '__SPANNED__'
319
        ) {
320
            return;
321
        }
322
        $attributes = $this->_parseAttributes($attributes);
323
        $err = $this->_adjustEnds($row, $col, 'updateCellAttributes', $attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by $this->_parseAttributes($attributes) on line 322 can also be of type null; however, HTML_Table_Storage::_adjustEnds() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
Are you sure the assignment to $err is correct as $this->_adjustEnds($row,...tributes', $attributes) (which targets HTML_Table_Storage::_adjustEnds()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
324
        if (PEAR::isError($err)) {
325
            return $err;
326
        }
327
        $this->_updateAttrArray($this->_structure[$row][$col]['attr'], $attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by $this->_parseAttributes($attributes) on line 322 can also be of type null; however, HTML_Common::_updateAttrArray() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
328
        //$this->_updateSpanGrid($row, $col);
329
    }
330
331
    /**
332
     * Returns the attributes for a given cell
333
     * @param    int     $row         Row index
334
     * @param    int     $col         Column index
335
     * @return   array
336
     * @access   public
337
     */
338 View Code Duplication
    function getCellAttributes($row, $col)
339
    {
340
        if (   isset($this->_structure[$row][$col])
341
            && $this->_structure[$row][$col] != '__SPANNED__'
342
        ) {
343
            return $this->_structure[$row][$col]['attr'];
344
        } elseif (!isset($this->_structure[$row][$col])) {
345
            return PEAR::raiseError('Invalid table cell reference[' .
346
                $row . '][' . $col . '] in HTML_Table::getCellAttributes');
347
        }
348
        return;
349
    }
350
351
    /**
352
     * Sets the cell contents for an existing cell
353
     *
354
     * If the given indices do not exist and autoGrow is true then the given
355
     * row and/or col is automatically added.  If autoGrow is false then an
356
     * error is returned.
357
     * @param    int      $row        Row index
358
     * @param    int      $col        Column index
359
     * @param    mixed    $contents   May contain html or any object with a
360
     *                                toHTML() method; if it is an array (with
361
     *                                strings and/or objects), $col will be used
362
     *                                as start offset and the array elements will
363
     *                                be set to this and the following columns
364
     *                                in $row
365
     * @param    string   $type       (optional) Cell type either 'TH' or 'TD'
366
     * @access   public
367
     * @throws   PEAR_Error
368
     */
369
    function setCellContents($row, $col, $contents, $type = 'TD')
370
    {
371
        if (is_array($contents)) {
372
            foreach ($contents as $singleContent) {
373
                $ret = $this->_setSingleCellContents($row, $col, $singleContent,
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->_setSingleCellCon... $singleContent, $type) (which targets HTML_Table_Storage::_setSingleCellContents()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
374
                    $type);
375
                if (PEAR::isError($ret)) {
376
                    return $ret;
377
                }
378
                $col++;
379
            }
380
        } else {
381
            $ret = $this->_setSingleCellContents($row, $col, $contents, $type);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as $this->_setSingleCellCon...$col, $contents, $type) (which targets HTML_Table_Storage::_setSingleCellContents()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
382
            if (PEAR::isError($ret)) {
383
                return $ret;
384
            }
385
        }
386
    }
387
388
    /**
389
     * Sets the cell contents for a single existing cell
390
     *
391
     * If the given indices do not exist and autoGrow is true then the given
392
     * row and/or col is automatically added.  If autoGrow is false then an
393
     * error is returned.
394
     * @param    int      $row        Row index
395
     * @param    int      $col        Column index
396
     * @param    mixed    $contents   May contain html or any object with a
397
     *                                toHTML() method; if it is an array (with
398
     *                                strings and/or objects), $col will be used
399
     *                                as start offset and the array elements will
400
     *                                be set to this and the following columns
401
     *                                in $row
402
     * @param    string   $type       (optional) Cell type either 'TH' or 'TD'
403
     * @access   private
404
     * @throws   PEAR_Error
405
     */
406
    function _setSingleCellContents($row, $col, $contents, $type = 'TD')
407
    {
408
        if (   isset($this->_structure[$row][$col])
409
            && $this->_structure[$row][$col] == '__SPANNED__'
410
        ) {
411
            return;
412
        }
413
        $err = $this->_adjustEnds($row, $col, 'setCellContents');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $err is correct as $this->_adjustEnds($row, $col, 'setCellContents') (which targets HTML_Table_Storage::_adjustEnds()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
414
        if (PEAR::isError($err)) {
415
            return $err;
416
        }
417
        $this->_structure[$row][$col]['contents'] = $contents;
418
        $this->_structure[$row][$col]['type'] = $type;
419
    }
420
421
    /**
422
     * Returns the cell contents for an existing cell
423
     * @param    int        $row    Row index
424
     * @param    int        $col    Column index
425
     * @access   public
426
     * @return   mixed
427
     */
428 View Code Duplication
    function getCellContents($row, $col)
429
    {
430
        if (   isset($this->_structure[$row][$col])
431
            && $this->_structure[$row][$col] == '__SPANNED__'
432
        ) {
433
            return;
434
        }
435
        if (!isset($this->_structure[$row][$col])) {
436
            return PEAR::raiseError('Invalid table cell reference[' .
437
                $row . '][' . $col . '] in HTML_Table::getCellContents');
438
        }
439
        return $this->_structure[$row][$col]['contents'];
440
    }
441
442
    /**
443
     * Sets the contents of a header cell
444
     * @param    int     $row
445
     * @param    int     $col
446
     * @param    mixed   $contents
447
     * @param    mixed   $attributes  Associative array or string of table row
448
     *                                attributes
449
     * @access   public
450
     */
451
    function setHeaderContents($row, $col, $contents, $attributes = null)
452
    {
453
        $this->setCellContents($row, $col, $contents, 'TH');
454
        if (!is_null($attributes)) {
455
            $this->updateCellAttributes($row, $col, $attributes);
456
        }
457
    }
458
459
    /**
460
     * Adds a table row and returns the row identifier
461
     * @param    array    $contents   (optional) Must be a indexed array of valid
462
     *                                           cell contents
463
     * @param    mixed    $attributes (optional) Associative array or string of
464
     *                                           table row attributes. This can
465
     *                                           also be an array of attributes,
466
     *                                           in which case the attributes
467
     *                                           will be repeated in a loop.
468
     * @param    string   $type       (optional) Cell type either 'th' or 'td'
469
     * @param    bool     $inTR                  false if attributes are to be
470
     *                                           applied in TD tags; true if
471
     *                                           attributes are to be applied in
472
     *                                            TR tag
473
     * @return   int
474
     * @access   public
475
     */
476 View Code Duplication
    function addRow($contents = null, $attributes = null, $type = 'td',
477
        $inTR = false)
478
    {
479
        if (isset($contents) && !is_array($contents)) {
480
            return PEAR::raiseError('First parameter to HTML_Table::addRow ' .
481
                'must be an array');
482
        }
483
        if (is_null($contents)) {
484
            $contents = array();
485
        }
486
487
        $type = strtolower($type);
488
        $row = $this->_rows++;
489
        foreach ($contents as $col => $content) {
490
            if ($type == 'td') {
491
                $this->setCellContents($row, $col, $content);
492
            } elseif ($type == 'th') {
493
                $this->setHeaderContents($row, $col, $content);
494
            }
495
        }
496
        $this->setRowAttributes($row, $attributes, $inTR);
497
        return $row;
498
    }
499
500
    /**
501
     * Sets the row attributes for an existing row
502
     * @param    int      $row            Row index
503
     * @param    mixed    $attributes     Associative array or string of table
504
     *                                    row attributes. This can also be an
505
     *                                    array of attributes, in which case the
506
     *                                    attributes will be repeated in a loop.
507
     * @param    bool     $inTR           false if attributes are to be applied
508
     *                                    in TD tags; true if attributes are to
509
     *                                    be applied in TR tag
510
     * @access   public
511
     * @throws   PEAR_Error
512
     */
513 View Code Duplication
    function setRowAttributes($row, $attributes, $inTR = false)
514
    {
515
        if (!$inTR) {
516
            $multiAttr = $this->_isAttributesArray($attributes);
517
            for ($i = 0; $i < $this->_cols; $i++) {
518
                if ($multiAttr) {
519
                    $this->setCellAttributes($row, $i,
520
                        $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
521
                } else {
522
                    $this->setCellAttributes($row, $i, $attributes);
523
                }
524
            }
525
        } else {
526
            $attributes = $this->_parseAttributes($attributes);
527
            $err = $this->_adjustEnds($row, 0, 'setRowAttributes', $attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by $this->_parseAttributes($attributes) on line 526 can also be of type null; however, HTML_Table_Storage::_adjustEnds() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
Are you sure the assignment to $err is correct as $this->_adjustEnds($row,...tributes', $attributes) (which targets HTML_Table_Storage::_adjustEnds()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
528
            if (PEAR::isError($err)) {
529
                return $err;
530
            }
531
            $this->_structure[$row]['attr'] = $attributes;
532
        }
533
    }
534
535
    /**
536
     * Updates the row attributes for an existing row
537
     * @param    int      $row            Row index
538
     * @param    mixed    $attributes     Associative array or string of table
539
     *                                    row attributes
540
     * @param    bool     $inTR           false if attributes are to be applied
541
     *                                    in TD tags; true if attributes are to
542
     *                                    be applied in TR tag
543
     * @access   public
544
     * @throws   PEAR_Error
545
     */
546 View Code Duplication
    function updateRowAttributes($row, $attributes = null, $inTR = false)
547
    {
548
        if (!$inTR) {
549
            $multiAttr = $this->_isAttributesArray($attributes);
550
            for ($i = 0; $i < $this->_cols; $i++) {
551
                if ($multiAttr) {
552
                    $this->updateCellAttributes($row, $i,
553
                        $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
554
                } else {
555
                    $this->updateCellAttributes($row, $i, $attributes);
556
                }
557
            }
558
        } else {
559
            $attributes = $this->_parseAttributes($attributes);
560
            $err = $this->_adjustEnds($row, 0, 'updateRowAttributes', $attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by $this->_parseAttributes($attributes) on line 559 can also be of type null; however, HTML_Table_Storage::_adjustEnds() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Bug introduced by
Are you sure the assignment to $err is correct as $this->_adjustEnds($row,...tributes', $attributes) (which targets HTML_Table_Storage::_adjustEnds()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
561
            if (PEAR::isError($err)) {
562
                return $err;
563
            }
564
            $this->_updateAttrArray($this->_structure[$row]['attr'], $attributes);
0 ignored issues
show
Bug introduced by
It seems like $attributes defined by $this->_parseAttributes($attributes) on line 559 can also be of type null; however, HTML_Common::_updateAttrArray() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
565
        }
566
    }
567
568
    /**
569
     * Returns the attributes for a given row as contained in the TR tag
570
     * @param    int     $row         Row index
571
     * @return   array
572
     * @access   public
573
     */
574
    function getRowAttributes($row)
575
    {
576
        if (isset($this->_structure[$row]['attr'])) {
577
            return $this->_structure[$row]['attr'];
578
        }
579
        return;
580
    }
581
582
    /**
583
     * Alternates the row attributes starting at $start
584
     * @param    int      $start            Row index of row in which alternating
585
     *                                      begins
586
     * @param    mixed    $attributes1      Associative array or string of table
587
     *                                      row attributes
588
     * @param    mixed    $attributes2      Associative array or string of table
589
     *                                      row attributes
590
     * @param    bool     $inTR             false if attributes are to be applied
591
     *                                      in TD tags; true if attributes are to
592
     *                                      be applied in TR tag
593
     * @param    int      $firstAttributes  (optional) Which attributes should be
594
     *                                      applied to the first row, 1 or 2.
595
     * @access   public
596
     */
597
    function altRowAttributes($start, $attributes1, $attributes2, $inTR = false,
598
        $firstAttributes = 1)
599
    {
600
        for ($row = $start; $row < $this->_rows; $row++) {
601
            if (($row + $start + ($firstAttributes - 1)) % 2 == 0) {
602
                $attributes = $attributes1;
603
            } else {
604
                $attributes = $attributes2;
605
            }
606
            $this->updateRowAttributes($row, $attributes, $inTR);
607
        }
608
    }
609
610
    /**
611
     * Adds a table column and returns the column identifier
612
     * @param    array    $contents   (optional) Must be a indexed array of valid
613
     *                                cell contents
614
     * @param    mixed    $attributes (optional) Associative array or string of
615
     *                                table row attributes
616
     * @param    string   $type       (optional) Cell type either 'th' or 'td'
617
     * @return   int
618
     * @access   public
619
     */
620 View Code Duplication
    function addCol($contents = null, $attributes = null, $type = 'td')
621
    {
622
        if (isset($contents) && !is_array($contents)) {
623
            return PEAR::raiseError('First parameter to HTML_Table::addCol ' .
624
                'must be an array');
625
        }
626
        if (is_null($contents)) {
627
            $contents = array();
628
        }
629
630
        $type = strtolower($type);
631
        $col = $this->_cols++;
632
        foreach ($contents as $row => $content) {
633
            if ($type == 'td') {
634
                $this->setCellContents($row, $col, $content);
635
            } elseif ($type == 'th') {
636
                $this->setHeaderContents($row, $col, $content);
637
            }
638
        }
639
        $this->setColAttributes($col, $attributes);
640
        return $col;
641
    }
642
643
    /**
644
     * Sets the column attributes for an existing column
645
     * @param    int      $col            Column index
646
     * @param    mixed    $attributes     (optional) Associative array or string
647
     *                                    of table row attributes
648
     * @access   public
649
     */
650 View Code Duplication
    function setColAttributes($col, $attributes = null)
651
    {
652
        $multiAttr = $this->_isAttributesArray($attributes);
653
        for ($i = 0; $i < $this->_rows; $i++) {
654
            if ($multiAttr) {
655
                $this->setCellAttributes($i, $col,
656
                    $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
657
            } else {
658
                $this->setCellAttributes($i, $col, $attributes);
659
            }
660
        }
661
    }
662
663
    /**
664
     * Updates the column attributes for an existing column
665
     * @param    int      $col            Column index
666
     * @param    mixed    $attributes     (optional) Associative array or string
667
     *                                    of table row attributes
668
     * @access   public
669
     */
670 View Code Duplication
    function updateColAttributes($col, $attributes = null)
671
    {
672
        $multiAttr = $this->_isAttributesArray($attributes);
673
        for ($i = 0; $i < $this->_rows; $i++) {
674
            if ($multiAttr) {
675
                $this->updateCellAttributes($i, $col,
676
                    $attributes[$i - ((ceil(($i + 1) / count($attributes))) - 1) * count($attributes)]);
677
            } else {
678
                $this->updateCellAttributes($i, $col, $attributes);
679
            }
680
        }
681
    }
682
683
    /**
684
     * Sets the attributes for all cells
685
     * @param    mixed    $attributes        (optional) Associative array or
686
     *                                       string of table row attributes
687
     * @access   public
688
     */
689
    function setAllAttributes($attributes = null)
690
    {
691
        for ($i = 0; $i < $this->_rows; $i++) {
692
            $this->setRowAttributes($i, $attributes);
693
        }
694
    }
695
696
    /**
697
     * Updates the attributes for all cells
698
     * @param    mixed    $attributes        (optional) Associative array or
699
     *                                       string of table row attributes
700
     * @access   public
701
     */
702
    function updateAllAttributes($attributes = null)
703
    {
704
        for ($i = 0; $i < $this->_rows; $i++) {
705
            $this->updateRowAttributes($i, $attributes);
706
        }
707
    }
708
709
    /**
710
     * Returns the table rows as HTML
711
     * @access   public
712
     * @return   string
713
     */
714
    function toHtml($tabs = null, $tab = null)
715
    {
716
        $strHtml = '';
717
        if (is_null($tabs)) {
718
            $tabs = $this->_getTabs();
719
        }
720
        if (is_null($tab)) {
721
            $tab = $this->_getTab();
722
        }
723
        $lnEnd = $this->_getLineEnd();
724
        if ($this->_useTGroups) {
725
            $extraTab = $tab;
726
        } else {
727
            $extraTab = '';
728
        }
729
        if ($this->_cols > 0) {
730
            for ($i = 0 ; $i < $this->_rows ; $i++) {
731
                $attr = '';
732
                if (isset($this->_structure[$i]['attr'])) {
733
                    $attr = $this->_getAttrString($this->_structure[$i]['attr']);
734
                }
735
                $strHtml .= $tabs .$tab . $extraTab . '<tr'.$attr.'>' . $lnEnd;
736
                for ($j = 0 ; $j < $this->_cols ; $j++) {
737
                    $attr     = '';
738
                    $contents = '';
739
                    $type     = 'td';
740
                    if (isset($this->_structure[$i][$j]) && $this->_structure[$i][$j] == '__SPANNED__') {
741
                        continue;
742
                    }
743
                    if (isset($this->_structure[$i][$j]['type'])) {
744
                        $type = (strtolower($this->_structure[$i][$j]['type']) == 'th' ? 'th' : 'td');
745
                    }
746 View Code Duplication
                    if (isset($this->_structure[$i][$j]['attr'])) {
747
                        $attr = $this->_structure[$i][$j]['attr'];
748
                    }
749 View Code Duplication
                    if (isset($this->_structure[$i][$j]['contents'])) {
750
                        $contents = $this->_structure[$i][$j]['contents'];
751
                    }
752
753
                    if (is_object($contents)) {
754
                        // changes indent and line end settings on nested tables
755
                        if (is_subclass_of($contents, 'html_common')) {
756
                            $contents->setTab($tab . $extraTab);
757
                            $contents->setTabOffset($this->_tabOffset + 3);
758
                            $contents->_nestLevel = $this->_nestLevel + 1;
759
                            $contents->setLineEnd($this->_getLineEnd());
760
                        }
761
                        if (method_exists($contents, 'toHtml')) {
762
                            $contents = $contents->toHtml();
763
                        } elseif (method_exists($contents, 'toString')) {
764
                            $contents = $contents->toString();
765
                        }
766
                    }
767
                    if (is_array($contents)) {
768
                        $contents = implode(', ', $contents);
769
                    }
770
771
                    $typeContent = $tabs . $tab . $tab . $extraTab . "<$type" . $this->_getAttrString($attr) . '>';
772
773
                    if (empty($contents)) {
774
                        if (isset($this->_autoFill) && $this->_autoFill) {
775
                            $contents = $this->_autoFill;
776
                        }
777
                    } else {
778
                        $typeContent .= $contents;
779
                    }
780
                    $typeContent .= "</$type>" . $lnEnd;
781
782
                    if (!empty($contents)) {
783
                        $strHtml .= $typeContent;
784
                    }
785
786
                }
787
                $strHtml .= $tabs . $tab . $extraTab . '</tr>' . $lnEnd;
788
            }
789
        }
790
        return $strHtml;
791
    }
792
793
    /**
794
     * Checks if rows or columns are spanned
795
     * @param    int        $row            Row index
796
     * @param    int        $col            Column index
797
     * @access   private
798
     */
799
    function _updateSpanGrid($row, $col)
800
    {
801 View Code Duplication
        if (isset($this->_structure[$row][$col]['attr']['colspan'])) {
802
            $colspan = $this->_structure[$row][$col]['attr']['colspan'];
803
        }
804
805 View Code Duplication
        if (isset($this->_structure[$row][$col]['attr']['rowspan'])) {
806
            $rowspan = $this->_structure[$row][$col]['attr']['rowspan'];
807
        }
808
809 View Code Duplication
        if (isset($colspan)) {
810
            for ($j = $col + 1; (($j < $this->_cols) && ($j <= ($col + $colspan - 1))); $j++) {
811
                $this->_structure[$row][$j] = '__SPANNED__';
812
            }
813
        }
814
815 View Code Duplication
        if (isset($rowspan)) {
816
            for ($i = $row + 1; (($i < $this->_rows) && ($i <= ($row + $rowspan - 1))); $i++) {
817
                $this->_structure[$i][$col] = '__SPANNED__';
818
            }
819
        }
820
821
        if (isset($colspan) && isset($rowspan)) {
822
            for ($i = $row + 1; (($i < $this->_rows) && ($i <= ($row + $rowspan - 1))); $i++) {
823
                for ($j = $col + 1; (($j <= $this->_cols) && ($j <= ($col + $colspan - 1))); $j++) {
824
                    $this->_structure[$i][$j] = '__SPANNED__';
825
                }
826
            }
827
        }
828
    }
829
830
    /**
831
     * Adjusts ends (total number of rows and columns)
832
     * @param    int     $row        Row index
833
     * @param    int     $col        Column index
834
     * @param    string  $method     Method name of caller
835
     *                               Used to populate PEAR_Error if thrown.
836
     * @param    array   $attributes Assoc array of attributes
837
     *                               Default is an empty array.
838
     * @access   private
839
     * @throws   PEAR_Error
840
     */
841
    function _adjustEnds($row, $col, $method, $attributes = array())
842
    {
843
        $colspan = isset($attributes['colspan']) ? $attributes['colspan'] : 1;
844
        $rowspan = isset($attributes['rowspan']) ? $attributes['rowspan'] : 1;
845
        if (($row + $rowspan - 1) >= $this->_rows) {
846
            if ($this->_autoGrow) {
847
                $this->_rows = $row + $rowspan;
848
849
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
850
                /*return PEAR::raiseError('Invalid table row reference[' .
851
                    $row . '] in HTML_Table::' . $method);*/
852
            }
853
        }
854
855
        if (($col + $colspan - 1) >= $this->_cols) {
856
            if ($this->_autoGrow) {
857
                $this->_cols = $col + $colspan;
858
            } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
859
                /*return PEAR::raiseError('Invalid table column reference[' .
860
                    $col . '] in HTML_Table::' . $method);*/
861
            }
862
        }
863
    }
864
865
    /**
866
     * Tells if the parameter is an array of attribute arrays/strings
867
     * @param    mixed   $attributes Variable to test
868
     * @access   private
869
     * @return   bool
870
     */
871
    function _isAttributesArray($attributes)
872
    {
873
        if (is_array($attributes) && isset($attributes[0])) {
874
            if (is_array($attributes[0]) || (is_string($attributes[0]) && count($attributes) > 1)) {
875
                return true;
876
            }
877
        }
878
        return false;
879
    }
880
881
}
882
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
883