Passed
Push — master ( cc7747...d2190f )
by Mihail
10:36
created

FormSettings::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Apps\Model\Admin\Newcontent;
4
5
use Ffcms\Core\Arch\Model;
6
use Apps\ActiveRecord\ContentCategory;
7
use Ffcms\Core\Helper\Serialize;
8
9
/**
10
 * Class FormSettings. New content widget settings business logic
11
 * @package Apps\Model\Admin\Newcontent
12
 */
13
class FormSettings extends Model
14
{
15
    public $categories = array();
16
    public $count;
17
    public $cache = 300;
18
19
    private $_configs;
20
21
    /**
22
     * FormSettings constructor. Pass configs inside from controller
23
     * @param array|null $configs
24
     */
25
    public function __construct(array $configs = null)
26
    {
27
        $this->_configs = $configs;
28
        parent::__construct();
29
    }
30
31
    /**
32
     * Set default values from configs
33
     */
34
    public function before()
35
    {
36
        if ($this->_configs === null) {
37
            return;
38
        }
39
        $this->categories = Serialize::decode($this->_configs['categories']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Ffcms\Core\Helper\Seria..._configs['categories']) can also be of type string. However, the property $categories is declared as type array. 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...
40
        $this->count = (int)$this->_configs['count'];
41
        $this->cache = (int)$this->_configs['cache'];
42
    }
43
44
    /**
45
     * Labels for form display
46
     * @return array
47
     */
48
    public function labels()
49
    {
50
        return [
51
            'categories' => __('Categories'),
52
            'count' => __('Count'),
53
            'cache' => __('Cache')
54
        ];
55
    }
56
57
    /**
58
     * Validation rules
59
     * @return array
60
     */
61 View Code Duplication
    public function rules()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        return [
64
            [['count', 'categories'], 'required'],
65
            [['count', 'cache'], 'int']
66
        ];
67
    }
68
69
    /**
70
     * Get all categories as sorted array
71
     * @return array
72
     */
73
    public function getCategories()
74
    {
75
        return ContentCategory::getSortedCategories();
76
    }
77
}