ImportedFile::getLastModified()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ILess
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace ILess;
11
12
use ILess\Exception\Exception;
13
use ILess\Node\RulesetNode;
14
use ILess\Util\Serializer;
15
16
/**
17
 * Import.
18
 */
19
final class ImportedFile implements \Serializable
20
{
21
    /**
22
     * The absolute path or URL.
23
     *
24
     * @var string
25
     */
26
    protected $path;
27
28
    /**
29
     * The content of the file.
30
     *
31
     * @var string
32
     */
33
    protected $content;
34
35
    /**
36
     * The ruleset.
37
     *
38
     * @var RulesetNode
39
     */
40
    protected $ruleset;
41
42
    /**
43
     * Error exception.
44
     *
45
     * @var Exception
46
     */
47
    protected $error;
48
49
    /**
50
     * Last modification of the file as Unix timestamp.
51
     *
52
     * @var int
53
     */
54
    protected $lastModified;
55
56
    /**
57
     * Constructor.
58
     *
59
     * @param string $path The absolute path or URL
60
     * @param string $content The content of the local or remote file
61
     * @param int $lastModified The last modification time
62
     */
63
    public function __construct($path, $content, $lastModified)
64
    {
65
        $this->path = $path;
66
        $this->content = Util::normalizeLineFeeds($content);
67
        $this->lastModified = $lastModified;
68
    }
69
70
    /**
71
     * Sets the ruleset.
72
     *
73
     * @param string|RulesetNode|null $ruleset
74
     *
75
     * @return ImportedFile
76
     */
77
    public function setRuleset($ruleset)
78
    {
79
        $this->ruleset = $ruleset;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ruleset can also be of type string. However, the property $ruleset is declared as type object<ILess\Node\RulesetNode>. 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...
80
81
        return $this;
82
    }
83
84
    /**
85
     * Returns the ruleset.
86
     *
87
     * @return RulesetNode|string|null
88
     */
89
    public function getRuleset()
90
    {
91
        return $this->ruleset;
92
    }
93
94
    /**
95
     * Sets an error.
96
     *
97
     * @param \Exception $error
98
     *
99
     * @return ImportedFile
100
     */
101
    public function setError(\Exception $error)
102
    {
103
        $this->error = $error;
0 ignored issues
show
Documentation Bug introduced by
$error is of type object<Exception>, but the property $error was declared to be of type object<ILess\Exception\Exception>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
104
105
        return $this;
106
    }
107
108
    /**
109
     * Returns the error.
110
     *
111
     * @return \Exception
112
     */
113
    public function getError()
114
    {
115
        return $this->error;
116
    }
117
118
    /**
119
     * Returns the path or URL.
120
     *
121
     * @return string
122
     */
123
    public function getPath()
124
    {
125
        return $this->path;
126
    }
127
128
    /**
129
     * Returns the content of the local or remote file.
130
     *
131
     * @return string
132
     */
133
    public function getContent()
134
    {
135
        return $this->content;
136
    }
137
138
    /**
139
     * Is virtual?
140
     *
141
     * @return bool
142
     */
143
    public function getLastModified()
144
    {
145
        return $this->lastModified;
146
    }
147
148
    public function serialize()
149
    {
150
        return Serializer::serialize([
151
            $this->path,
152
            $this->lastModified,
153
            base64_encode($this->content),
154
            // we cannot include the parsed ruleset, it contains circular references
155
            // Is there any way to handle it?
156
        ]);
157
    }
158
159
    public function unserialize($serialized)
160
    {
161
        list($this->path, $this->lastModified, $this->content) = Serializer::unserialize($serialized);
162
163
        $this->content = base64_decode($this->content);
164
    }
165
}
166