FileRemoveAction::run()   A
last analyzed

Complexity

Conditions 2
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 2
nc 4
nop 1
1
<?php
2
3
namespace yiicod\fileupload\actions;
4
5
use Exception;
6
use Yii;
7
use yii\base\Action;
8
use yii\web\HttpException;
9
use yii\web\Response;
10
use yiicod\base\helpers\LoggerMessage;
11
use yiicod\fileupload\components\RemoveHandler;
12
use yiicod\fileupload\widgets\FileUpload;
13
14
/**
15
 * Class FileRemoveAction
16
 *
17
 * @author Virchenko Maksim <[email protected]>
18
 *
19
 * @package yiicod\fileupload\actions
20
 */
21
class FileRemoveAction extends Action
22
{
23
    /**
24
     * Upload file
25
     *
26
     * @param string $data
27
     *
28
     * @return Response
29
     *
30
     * @throws HttpException
31
     */
32
    public function run(string $data)
33
    {
34
        try {
35
            $payload = FileUpload::decodeServerOptions($data);
36
            $fileHandler = new RemoveHandler();
37
38
            return $this->controller->asJson($fileHandler->remove($payload['uploader'], $payload['userData']));
0 ignored issues
show
Bug introduced by
The method asJson does only exist in yii\web\Controller, but not in yii\base\Controller and yii\console\Controller.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
39
        } catch (Exception $e) {
40
            $response = Yii::$app->response;
41
            $response->format = Response::FORMAT_RAW;
42
            $response->setStatusCode($e->getCode(), $e->getMessage());
43
44
            Yii::error(LoggerMessage::log($e), __METHOD__);
0 ignored issues
show
Documentation introduced by
$e is of type object<Exception>, but the function expects a object<Throwable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
46
            return $response;
47
        }
48
    }
49
}
50