Passed
Push — main ( 201c2b...35e5a4 )
by Sugeng
03:27
created

LocalAction::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use Yii;
6
use yii\base\Action;
7
use yii\helpers\Json;
8
use yii\web\NotFoundHttpException;
9
10
/**
11
 * ErrorAction displays application errors using a specified view.
12
 *
13
 * To use ErrorAction, you need to do the following steps:
14
 *
15
 * First, declare an action of ErrorAction type in the `actions()` method of your `SiteController`
16
 * class (or whatever controller you prefer), like the following:
17
 *
18
 * ```php
19
 * public function actions()
20
 * {
21
 *     return [
22
 *         'error' => ['class' => 'yii\web\ErrorAction'],
23
 *     ];
24
 * }
25
 * ```
26
 *
27
 * Then, create a view file for this action. If the route of your error action is `site/error`, then
28
 * the view file should be `views/site/error.php`. In this view file, the following variables are available:
29
 *
30
 * - `$name`: the error name
31
 * - `$message`: the error message
32
 * - `$exception`: the exception being handled
33
 *
34
 * Finally, configure the "errorHandler" application component as follows,
35
 *
36
 * ```php
37
 * 'errorHandler' => [
38
 *     'errorAction' => 'site/error',
39
 * ]
40
 * ```
41
 *
42
 * @author Qiang Xue <[email protected]>
43
 * @author Dmitry Naumenko <[email protected]>
44
 * @since 2.0
45
 */
46
class LocalAction extends Action
47
{
48
    /**
49
     * @var string flysystem component
50
     */
51
    public $component = 'fs';
52
53
    /**
54
     * @var LocalComponent
55
     */
56
    protected $filesystem;
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function init()
62
    {
63
        $this->filesystem = Yii::$app->get($this->component);
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->get($this->component) can also be of type mixed. However, the property $filesystem is declared as type diecoding\flysystem\LocalComponent. 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...
64
    }
65
66
    /**
67
     * Runs the action.
68
     *
69
     * @param string $data
70
     * @return string result content
71
     * @throws NotFoundHttpException if data not valid
72
     */
73
    public function run($data)
74
    {
75
        try {
76
            $params = Json::decode($this->filesystem->decrypt($data));
77
78
            $now      = time();
79
            $filePath = (string) $params['filePath'];
80
            $expires  = $params['expires'];
81
82
            if ($expires !== null && (int) $expires < $now) {
83
                $filePath = false;
84
            }
85
        } catch (\Throwable $th) {
86
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
87
        }
88
89
        $normalizePath = '/' . $this->filesystem->normalizePath($filePath);
0 ignored issues
show
Bug introduced by
It seems like $filePath can also be of type false; however, parameter $path of diecoding\flysystem\Abst...ponent::normalizePath() does only seem to accept string, 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

89
        $normalizePath = '/' . $this->filesystem->normalizePath(/** @scrutinizer ignore-type */ $filePath);
Loading history...
90
        if ($filePath === false || !$this->filesystem->fileExists($normalizePath)) {
91
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
92
        }
93
94
        $filePath = $this->filesystem->path . $normalizePath;
95
96
        return Yii::$app->getResponse()->sendFile($filePath, $normalizePath, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return Yii::app->getResp...rray('inline' => true)) also could return the type yii\console\Response|yii\web\Response which is incompatible with the documented return type string.
Loading history...
97
            'inline' => true,
98
        ]);
99
    }
100
}
101