Completed
Push — master ( 517e4f...f67525 )
by Florent
02:24
created

Deflate::uncompress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Compression;
13
14
/**
15
 * This class implements the compression algorithm DEF (defalte)
16
 * This compression algorithm is part of the specification.
17
 */
18
class Deflate implements CompressionInterface
19
{
20
    /**
21
     * @var int
22
     */
23
    protected $compression_level = -1;
24
25
    /**
26
     * Deflate constructor.
27
     *
28
     * @param int $compression_level
29
     */
30
    public function __construct($compression_level = -1)
31
    {
32
        if (!is_numeric($compression_level) || $compression_level < -1 || $compression_level > 9) {
33
            throw new \InvalidArgumentException('The level of compression can be given as 0 for no compression up to 9 for maximum compression. If -1 given, the default compression level will be the default compression level of the zlib library.');
34
        }
35
36
        $this->compression_level = $compression_level;
0 ignored issues
show
Documentation Bug introduced by
It seems like $compression_level can also be of type double or string. However, the property $compression_level 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...
37
    }
38
39
    /**
40
     * @return int
41
     */
42
    private function getCompressionLevel()
43
    {
44
        return $this->compression_level;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getMethodName()
51
    {
52
        return 'DEF';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function compress($data)
59
    {
60
        return gzdeflate($data, $this->getCompressionLevel());
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function uncompress($data)
67
    {
68
        return gzinflate($data);
69
    }
70
}
71