Completed
Push — master ( 008d1c...59e294 )
by vistart
03:42
created

UserProfileSearchTrait.php (6 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
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\user;
14
15
use Yii;
16
use yii\base\Model;
17
use yii\data\ActiveDataProvider;
18
19
/**
20
 * @version 1.0
21
 * @author vistart <[email protected]>
22
 */
23
trait UserProfileSearchTrait
24
{
25
    public $createdFrom;
26
    protected $createdFromInUtc;
27
    public $createdTo;
28
    protected $createdToInUtc;
29
    /**
30
     * 
31
     * @return type
32
     */
33
    public function rules()
34
    {
35
        return [
36
            ['id', 'integer'],
37
            ['nickname', 'string'],
38
            [['createdFrom', 'createdTo'], 'datetime', 'format' => 'yyyy-mm-dd HH:mm:ss'],
39
            [['createdFrom', 'createdTo'], 'gmdate'],
40
        ];
41
    }
42
43
    public function gmdate($attribute, $params, $validator)
0 ignored issues
show
The parameter $params 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...
The parameter $validator 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...
44
    {
45
        if (isset($this->$attribute)) {
46
            $timestamp = strtotime($this->$attribute);
47
            $this->{$attribute . 'InUtc'} = gmdate('Y-m-d H:i:s', $timestamp);
48
        }
49
    }
50
51
    /**
52
     * 
53
     * @return type
54
     */
55
    public function scenarios()
56
    {
57
        // bypass scenarios() implementation in the parent class
58
        return Model::scenarios();
59
    }
60
61
    /**
62
     * 
63
     * @param type $params
64
     * @return ActiveDataProvider
65
     */
66
    public function search($params)
67
    {
68
        $query = static::find();
69
        $dataProvider = new ActiveDataProvider([
70
            'query' => $query,
71
            'pagination' => [
72
                'pageParam' => 'user-page',
73
                'defaultPageSize' => 20,
74
                'pageSizeParam' => 'user-per-page',
75
            ],
76
            'sort' => [
77
                'sortParam' => 'user-sort',
78
            ],
79
        ]);
80
81
        if (!($this->load($params) && $this->validate())) {
0 ignored issues
show
It seems like load() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
It seems like validate() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
            return $dataProvider;
83
        }
84
85
        $query = $query->andFilterWhere([
86
            'LIKE', 'id', $this->id,
0 ignored issues
show
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
87
        ])->andFilterWhere([
88
            'LIKE', 'nickname', $this->nickname,
0 ignored issues
show
The property nickname does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
89
        ])->andFilterWhere([
90
            '>=', 'created_at', $this->createdFromInUtc,
91
        ])->andFilterWhere([
92
            '<=', 'created_at', $this->createdToInUtc,
93
        ]);
94
        $dataProvider->query = $query;
95
        return $dataProvider;
96
    }
97
98
    public function attributeLabels()
99
    {
100
        $attributeLabels = parent::attributeLabels();
101
        $attributeLabels['createdFrom'] = Yii::t('user', 'From');
102
        $attributeLabels['createdTo'] = Yii::t('user', 'To');
103
        return $attributeLabels;
104
    }
105
}
106