Completed
Push — master ( 028ee3...a146dd )
by Andrii
21:15 queued 14:50
created

src/logic/DebugSessionSaver.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Health monitoring for Yii2 applications
4
 *
5
 * @link      https://github.com/hiqdev/yii2-monitoring
6
 * @package   yii2-monitoring
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\yii2\monitoring\logic;
12
13
use Yii;
14
use yii\helpers\FileHelper;
15
16
/**
17
 * Class DebugSessionSaver is designed to save debug session result to a separate
18
 * directory to prevent valuable debug data from deleting with GC.
19
 */
20
class DebugSessionSaver
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $key;
26
27
    /**
28
     * @var string
29
     */
30
    protected $dataPath;
31
32
    /**
33
     * @var string
34
     */
35
    protected $savedDataPath;
36
37
    /**
38
     * DebugSessionSaver constructor.
39
     * @param array $key
40
     * @param string $dataPath
41
     * @param string $savedDataPath
42
     * @internal param array $options
43
     */
44
    public function __construct($key, $dataPath = '@runtime/debug', $savedDataPath = '@runtime/saved-debug')
45
    {
46
        $this->key = $key;
47
        $this->dataPath = Yii::getAlias($dataPath);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($dataPath) can also be of type boolean. However, the property $dataPath is declared as type string. 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...
48
        $this->savedDataPath = Yii::getAlias($savedDataPath);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Yii::getAlias($savedDataPath) can also be of type boolean. However, the property $savedDataPath is declared as type string. 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...
49
    }
50
51
    /**
52
     * @return bool whether data was saved
53
     */
54
    public function run()
55
    {
56
        return $this->saveDebug();
57
    }
58
59
    /**
60
     * @return bool
61
     */
62
    protected function saveDebug()
63
    {
64
        $filename = "{$this->key}.data";
65
66
        $source = "{$this->dataPath}/$filename";
67
        if (!is_file($source)) {
68
            return false;
69
        }
70
71
        FileHelper::createDirectory($this->savedDataPath);
72
73
        return copy($source, "{$this->savedDataPath}/$filename") !== false;
74
    }
75
}
76