Completed
Push — master ( 47e90b...cbd9f9 )
by Dmitry
08:29
created

File   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 52
ccs 0
cts 20
cp 0
rs 10
wmc 5
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A events() 0 7 1
A saveUploadedFile() 0 14 4
1
<?php
2
3
/*
4
 * HiPanel core package
5
 *
6
 * @link      https://hipanel.com/
7
 * @package   hipanel-core
8
 * @license   BSD-3-Clause
9
 * @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/)
10
 */
11
12
/**
13
 * Created by PhpStorm.
14
 * User: tofid
15
 * Date: 11.02.15
16
 * Time: 17:59.
17
 */
18
namespace hipanel\behaviors;
19
20
use hipanel\models\File as FileModel;
21
use yii\base\Behavior;
22
use yii\db\BaseActiveRecord;
23
use yii\helpers\FileHelper;
24
use yii\web\UploadedFile;
25
26
class File extends Behavior
27
{
28
    /**
29
     * @var string the attribute which holds the attachment.
30
     */
31
    public $attribute = 'file';
32
33
    /**
34
     * @var array the scenarios in which the behavior will be triggered
35
     */
36
    public $scenarios = [];
37
38
    /**
39
     * @var string the attribute that will receive the file id
40
     */
41
    public $savedAttribute;
42
43
    /**
44
     * @var UploadedFile the uploaded file instance.
45
     */
46
    private $_file;
47
48
    /**
49
     * @return array
50
     */
51
    public function events()
52
    {
53
        return [
54
            BaseActiveRecord::EVENT_BEFORE_INSERT => 'saveUploadedFile',
55
            BaseActiveRecord::EVENT_BEFORE_UPDATE => 'saveUploadedFile',
56
        ];
57
    }
58
59
    /**
60
     * Event handler for beforeSave.
61
     * @param \yii\base\ModelEvent|boolean $event
62
     */
63
    public function saveUploadedFile($event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
64
    {
65
        $model = $this->owner;
66
        $arr_ids = [];
0 ignored issues
show
Unused Code introduced by
$arr_ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
        if (in_array($model->scenario, $this->scenarios, true)) {
68
            $this->_file = UploadedFile::getInstances($model, $this->attribute);
0 ignored issues
show
Compatibility introduced by
$model of type object<yii\base\Component> is not a sub-type of object<yii\base\Model>. It seems like you assume a child class of the class yii\base\Component to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
Documentation Bug introduced by
It seems like \yii\web\UploadedFile::g...odel, $this->attribute) of type array<integer,object<yii\web\UploadedFile>> is incompatible with the declared type object<yii\web\UploadedFile> of property $_file.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
            if (is_array($this->_file) && !empty($this->_file)) {
70
                $this->owner->{$this->savedAttribute} = implode(',', FileModel::fileSave($this->_file));
71
            } else {
72
                // Protect attribute
73
                unset($model->{$this->attribute});
74
            }
75
        }
76
    }
77
}
78