Passed
Push — 0.1.x ( dcc4c3...e6e6c9 )
by f
01:33
created

Gzip::gzipStat()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 2
nop 1
dl 0
loc 14
rs 9.9332
c 0
b 0
f 0
1
<?php
2
namespace wapmorgan\UnifiedArchive\Formats;
3
4
use Exception;
5
6
class Gzip extends OneFileFormat
7
{
8
    const FORMAT_SUFFIX = 'gz';
9
10
    /**
11
     * @param string $file GZipped file
12
     * @return array|false Array with 'mtime' and 'size' items
13
     */
14
    public static function gzipStat($file)
15
    {
16
        $fp = fopen($file, 'rb');
17
        if (filesize($file) < 18 || strcmp(fread($fp, 2), "\x1f\x8b")) {
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

17
        if (filesize($file) < 18 || strcmp(fread(/** @scrutinizer ignore-type */ $fp, 2), "\x1f\x8b")) {
Loading history...
18
            return false;  // Not GZIP format (See RFC 1952)
19
        }
20
        $method = fread($fp, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $method is dead and can be removed.
Loading history...
21
        $flags = fread($fp, 1);
0 ignored issues
show
Unused Code introduced by
The assignment to $flags is dead and can be removed.
Loading history...
22
        $stat = unpack('Vmtime', fread($fp, 4));
23
        fseek($fp, -4, SEEK_END);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

23
        fseek(/** @scrutinizer ignore-type */ $fp, -4, SEEK_END);
Loading history...
24
        $stat += unpack('Vsize', fread($fp, 4));
25
        fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
26
27
        return $stat;
28
    }
29
30
    /**
31
     * Gzip constructor.
32
     *
33
     * @param $archiveFileName
34
     *
35
     * @throws \Exception
36
     */
37
    public function __construct($archiveFileName)
38
    {
39
        parent::__construct($archiveFileName);
40
        $stat = static::gzipStat($archiveFileName);
41
        if ($stat === false) {
42
            throw new Exception('Could not open Gzip file');
43
        }
44
        $this->uncompressedSize = $stat['size'];
45
        $this->modificationTime = $stat['mtime'];
46
    }
47
48
    /**
49
     * @param string $fileName
50
     *
51
     * @return string|false
52
     */
53
    public function getFileContent($fileName = null)
54
    {
55
        return gzdecode(file_get_contents($this->fileName));
56
    }
57
58
    /**
59
     * @param string $fileName
60
     *
61
     * @return bool|resource|string
62
     */
63
    public function getFileResource($fileName = null)
64
    {
65
        return gzopen($this->fileName, 'rb');
66
    }
67
68
    /**
69
     * @param $data
70
     *
71
     * @return mixed|string
72
     */
73
    protected static function compressData($data)
74
    {
75
        return gzencode($data);
76
    }
77
}