LZWDecode::decode()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 55
rs 7.8235
cc 7
eloc 33
nc 6
nop 1

How to fix   Long Method   

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
//  FPDI - Version 1.2.1
4
//
5
//    Copyright 2004-2008 Setasign - Jan Slabon
6
//
7
//  Licensed under the Apache License, Version 2.0 (the "License");
8
//  you may not use this file except in compliance with the License.
9
//  You may obtain a copy of the License at
10
//
11
//      http://www.apache.org/licenses/LICENSE-2.0
12
//
13
//  Unless required by applicable law or agreed to in writing, software
14
//  distributed under the License is distributed on an "AS IS" BASIS,
15
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
//  See the License for the specific language governing permissions and
17
//  limitations under the License.
18
//
19
20
class LZWDecode {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
21
22
    var $sTable = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sTable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
23
    var $data = null;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $data.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
24
    var $tIdx;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $tIdx.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
25
    var $bitsToGet = 9;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $bitsToGet.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
26
    var $bytePointer;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $bytePointer.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
27
    var $bitPointer;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $bitPointer.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
28
    var $nextData = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $nextData.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
29
    var $nextBits = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $nextBits.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
30
    var $andTable = array(511, 1023, 2047, 4095);
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $andTable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
31
32
    function LZWDecode(&$fpdi) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
33
        $this->fpdi =& $fpdi;
0 ignored issues
show
Bug introduced by
The property fpdi does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34
    }
35
36
    /**
37
     * Method to decode LZW compressed data.
38
     *
39
     * @param string data    The compressed data.
40
     */
41
    function decode($data) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
42
43
        if($data[0] == 0x00 && $data[1] == 0x01) {
44
            $this->fpdi->error("LZW flavour not supported.");
45
        }
46
47
        $this->initsTable();
48
49
        $this->data = $data;
50
51
        // Initialize pointers
52
        $this->bytePointer = 0;
53
        $this->bitPointer = 0;
54
55
        $this->nextData = 0;
56
        $this->nextBits = 0;
57
58
        $oldCode = 0;
59
60
        $string = "";
0 ignored issues
show
Unused Code introduced by
$string is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
        $uncompData = "";
62
63
        while (($code = $this->getNextCode()) != 257) {
64
            if ($code == 256) {
65
                $this->initsTable();
66
                $code = $this->getNextCode();
67
68
                if ($code == 257) {
69
                    break;
70
                }
71
72
                $uncompData .= $this->sTable[$code];
73
                $oldCode = $code;
74
75
            } else {
76
77
                if ($code < $this->tIdx) {
78
                    $string = $this->sTable[$code];
79
                    $uncompData .= $string;
80
81
                    $this->addStringToTable($this->sTable[$oldCode], $string[0]);
82
                    $oldCode = $code;
83
                } else {
84
                    $string = $this->sTable[$oldCode];
85
                    $string = $string.$string[0];
86
                    $uncompData .= $string;
87
88
                    $this->addStringToTable($string);
89
                    $oldCode = $code;
90
                }
91
            }
92
        }
93
        
94
        return $uncompData;
95
    }
96
97
98
    /**
99
     * Initialize the string table.
100
     */
101
    function initsTable() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
102
        $this->sTable = array();
103
104
        for ($i = 0; $i < 256; $i++)
105
            $this->sTable[$i] = chr($i);
106
107
        $this->tIdx = 258;
108
        $this->bitsToGet = 9;
109
    }
110
111
    /**
112
     * Add a new string to the string table.
113
     */
114
    function addStringToTable ($oldString, $newString="") {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
115
        $string = $oldString.$newString;
116
117
        // Add this new String to the table
118
        $this->sTable[$this->tIdx++] = $string;
119
120
        if ($this->tIdx == 511) {
121
            $this->bitsToGet = 10;
122
        } else if ($this->tIdx == 1023) {
123
            $this->bitsToGet = 11;
124
        } else if ($this->tIdx == 2047) {
125
            $this->bitsToGet = 12;
126
        }
127
    }
128
129
    // Returns the next 9, 10, 11 or 12 bits
130
    function getNextCode() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
131
        if ($this->bytePointer == strlen($this->data))
132
            return 257;
133
134
        $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
135
        $this->nextBits += 8;
136
137
        if ($this->nextBits < $this->bitsToGet) {
138
            $this->nextData = ($this->nextData << 8) | (ord($this->data[$this->bytePointer++]) & 0xff);
139
            $this->nextBits += 8;
140
        }
141
142
        $code = ($this->nextData >> ($this->nextBits - $this->bitsToGet)) & $this->andTable[$this->bitsToGet-9];
143
        $this->nextBits -= $this->bitsToGet;
144
145
        return $code;
146
    }
147
}