Entry   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 259
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 75.47%

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 3
dl 0
loc 259
ccs 40
cts 53
cp 0.7547
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentCacheEnabled() 0 4 1
A setContentCacheEnabled() 0 4 1
B __construct() 0 23 6
A getCHM() 0 4 1
A getPath() 0 4 1
A getContentSectionIndex() 0 4 1
A getOffset() 0 4 1
A getLength() 0 4 1
A isDirectory() 0 4 1
A isFile() 0 4 1
A isSpecialFile() 0 4 1
A isMetaData() 0 4 1
A getType() 0 4 1
A getContents() 0 16 5
1
<?php
2
3
namespace CHMLib;
4
5
use Exception;
6
7
/**
8
 * Represent an entry (file/directory) contained in a CHM file.
9
 */
10
class Entry
11
{
12
    /**
13
     * Is the content cache enabled?
14
     *
15
     * @var bool
16
     */
17
    protected static $contentCacheEnabled = false;
18
19
    /**
20
     * Is the content cache enabled?
21
     *
22
     * @return bool
23
     */
24 491
    public static function getContentCacheEnabled()
25
    {
26 491
        return static::$contentCacheEnabled;
27
    }
28
29
    /**
30
     * Enable/disable the content cache.
31
     *
32
     * @param bool $enabled
33
     */
34
    public static function setContentCacheEnabled($enabled)
35
    {
36
        static::$contentCacheEnabled = (bool) $enabled;
37
    }
38
39
    /**
40
     * Entry type: directory.
41
     *
42
     * @var int
43
     */
44
    const TYPE_DIRECTORY = 0x1;
45
46
    /**
47
     * Entry type: normal file.
48
     *
49
     * @var int
50
     */
51
    const TYPE_FILE = 0x2;
52
53
    /**
54
     * Entry type: special file.
55
     *
56
     * @var int
57
     */
58
    const TYPE_SPECIAL_FILE = 0x4;
59
60
    /**
61
     * Entry type: meta data.
62
     *
63
     * @var int
64
     */
65
    const TYPE_METADATA = 0x8;
66
67
    /**
68
     * The parent CHM file.
69
     *
70
     * @var \CHMLib\CHM
71
     */
72
    protected $chm;
73
74
    /**
75
     * The path of this entry.
76
     *
77
     * @var string
78
     */
79
    protected $path;
80
81
    /**
82
     * The index of the content section that contains the data of this entry.
83
     *
84
     * @var int
85
     */
86
    protected $contentSectionIndex;
87
88
    /**
89
     * The offset of the entry data from the beginning of the content section this entry is in, after the section has been decompressed (if appropriate).
90
     *
91
     * @var int
92
     */
93
    protected $offset;
94
95
    /**
96
     * The length of the entry data after decompression (if appropriate).
97
     *
98
     * @var int
99
     */
100
    protected $length;
101
102
    /**
103
     * The type of this entry (one of the static::TYPE_... constants).
104
     *
105
     * @var int
106
     */
107
    protected $type;
108
109
    /**
110
     * The previously read contents of this entry.
111
     *
112
     * @var string|null
113
     */
114
    protected $cachedContents;
115
116
    /**
117
     * Initializes the instance.
118
     *
119
     * @param \CHMLib\CHM $chm The parent CHM file.
120
     */
121 4
    public function __construct(CHM $chm)
122
    {
123 4
        $reader = $chm->getReader();
124 4
        $this->chm = $chm;
125 4
        $stringLength = $reader->readCompressedUInt32();
126 4
        $this->path = $reader->readString($stringLength);
127 4
        $this->contentSectionIndex = $reader->readCompressedUInt32();
0 ignored issues
show
Documentation Bug introduced by
It seems like $reader->readCompressedUInt32() can also be of type double. However, the property $contentSectionIndex is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
128 4
        $this->offset = $reader->readCompressedUInt32();
0 ignored issues
show
Documentation Bug introduced by
It seems like $reader->readCompressedUInt32() can also be of type double. However, the property $offset is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
129 4
        $this->length = $reader->readCompressedUInt32();
0 ignored issues
show
Documentation Bug introduced by
It seems like $reader->readCompressedUInt32() can also be of type double. However, the property $length is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
130 4
        $pathLength = strlen($this->path);
131 4
        if (substr($this->path, -1) === '/') {
132 4
            $this->type = static::TYPE_DIRECTORY;
133 4
        } elseif ($this->path[0] === '/') {
134 4
            if ($pathLength > 1 && ($this->path[1] === '#' || $this->path[1] === '$')) {
135 4
                $this->type = static::TYPE_SPECIAL_FILE;
136
            } else {
137 4
                $this->type = static::TYPE_FILE;
138
            }
139
        } else {
140 4
            $this->type = static::TYPE_METADATA;
141
        }
142 4
        $this->cachedContents = null;
143 4
    }
144
145
    /**
146
     * Get the parent CHM file.
147
     *
148
     * @return \CHMLib\CHM
149
     */
150
    public function getCHM()
151
    {
152
        return $this->chm;
153
    }
154
155
    /**
156
     * Get the path of this entry.
157
     *
158
     * @return string
159
     */
160 7
    public function getPath()
161
    {
162 7
        return $this->path;
163
    }
164
165
    /**
166
     * Get the index of the content section that contains the data of this entry.
167
     *
168
     * @return int
169
     */
170 4
    public function getContentSectionIndex()
171
    {
172 4
        return $this->contentSectionIndex;
173
    }
174
175
    /**
176
     * Get the offset from the beginning of the content section this entry is in, after the section has been decompressed (if appropriate).
177
     *
178
     * @return int
179
     */
180 4
    public function getOffset()
181
    {
182 4
        return $this->offset;
183
    }
184
185
    /**
186
     * Get the length of the entry data after decompression (if appropriate).
187
     *
188
     * @return int
189
     */
190 4
    public function getLength()
191
    {
192 4
        return $this->length;
193
    }
194
195
    /**
196
     * Is this a directory entry?
197
     *
198
     * @return bool
199
     */
200
    public function isDirectory()
201
    {
202
        return (bool) ($this->type === static::TYPE_DIRECTORY);
203
    }
204
205
    /**
206
     * Is this a normal file entry?
207
     *
208
     * @return bool
209
     */
210 1
    public function isFile()
211
    {
212 1
        return (bool) ($this->type === static::TYPE_FILE);
213
    }
214
215
    /**
216
     * Is this a special file entry?
217
     *
218
     * @return bool
219
     */
220
    public function isSpecialFile()
221
    {
222
        return (bool) ($this->type === static::TYPE_SPECIAL_FILE);
223
    }
224
225
    /**
226
     * Is this a meta-data entry?
227
     *
228
     * @return bool
229
     */
230
    public function isMetaData()
231
    {
232
        return (bool) ($this->type === static::TYPE_METADATA);
233
    }
234
235
    /**
236
     * Get the type of this entry (one of the static::TYPE_... constants).
237
     *
238
     * @return int
239
     */
240 3
    public function getType()
241
    {
242 3
        return $this->type;
243
    }
244
245
    /**
246
     * Get the contents of this entry.
247
     *
248
     * @throws \Exception Throws an Exception in case of errors.
249
     *
250
     * @return string
251
     */
252 491
    public function getContents()
253
    {
254 491
        $cacheEnabled = static::getContentCacheEnabled();
255 491
        if ($cacheEnabled && $this->cachedContents !== null) {
256
            $result = $this->cachedContents;
257
        } else {
258 491
            $section = $this->chm->getSectionByIndex($this->contentSectionIndex);
259 491
            if ($section === null) {
260
                throw new Exception("The CHM file does not contain a data section with index {$this->contentSectionIndex}");
261
            }
262 491
            $result = $section->getContents($this->offset, $this->length);
263 491
            $this->cachedContents = $cacheEnabled ? $result : null;
264
        }
265
266 491
        return $result;
267
    }
268
}
269