Completed
Push — master ( ac13d3...9f68b4 )
by
unknown
01:57
created

ForumReply::author()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Taskforcedev\LaravelForum\Models;
4
5
use \DB;
6
use \Validator;
7
use Taskforcedev\LaravelForum\Helpers\UserHelper;
8
9
/**
10
 * Class ForumReply
11
 *
12
 * @property string  $body      Post body.
13
 * @property integer $post_id   Post ID.
14
 * @property integer $author_id Author ID.
15
 *
16
 * @package Taskforcedev\LaravelForum\Models
17
 */
18
class ForumReply extends AbstractModel
19
{
20
    protected $table = 'forum_post_replies';
21
22
    protected $hidden = [];
23
    protected $fillable = ['body', 'post_id', 'author_id'];
24
25
    /**
26
     * Eloquent Relation.
27
     * @return mixed
28
     */
29
    public function author()
30
    {
31
        $userHelper = new UserHelper();
32
        $model = $userHelper->getUserModel();
33
        return $this->belongsTo($model);
0 ignored issues
show
Bug introduced by
It seems like $model defined by $userHelper->getUserModel() on line 32 can also be of type boolean; however, Illuminate\Database\Eloquent\Model::belongsTo() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34
    }
35
36
    /**
37
     * Validates model data.
38
     * @param array $data The data to validate.
39
     *
40
     * @return boolean
41
     */
42 View Code Duplication
    public static function valid($data)
43
    {
44
        $rules = [
45
            'body' => 'required|min:3',
46
            'post_id' => 'required|min:1|integer|exists:forum_posts,id',
47
            'author_id' => 'required|min:1|integer|exists:users,id'
48
        ];
49
        $validator = Validator::make($data, $rules);
50
51
        if ($validator->passes()) {
52
            return true;
53
        }
54
        return false;
55
    }
56
57
    /**
58
     * Gets the number of replies from the database.
59
     * @return mixed
60
     */
61
    public static function getReplyCount()
62
    {
63
        $results = DB::select('select COUNT(*) as count from forum_post_replies');
64
        return $results[0]->count;
65
    }
66
}
67