Resource::getVotesAttribute()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
/*
4
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 */
7
8
namespace App\Models;
9
10
use Illuminate\Database\Eloquent\Model;
11
use Spatie\Activitylog\Traits\LogsActivity;
12
13
/**
14
 * Class Resource.
15
 *
16
 * @property int $id
17
 * @property string $type
18
 * @property string $cat
19
 * @property int $user_id
20
 * @property string $title
21
 * @property string $desc_md
22
 * @property string $desc_html
23
 * @property string $content_type
24
 * @property string $content_path
25
 * @property \Carbon\Carbon $created_at
26
 * @property \Carbon\Carbon $updated_at
27
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereId($value)
28
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereType($value)
29
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereCat($value)
30
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereUserId($value)
31
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereTitle($value)
32
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereDescMd($value)
33
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereDescHtml($value)
34
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereContentType($value)
35
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereContentPath($value)
36
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereCreatedAt($value)
37
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Resource whereUpdatedAt($value)
38
 * @mixin \Eloquent
39
 * @property-read \Illuminate\Database\Eloquent\Collection|\Venturecraft\Revisionable\Revision[] $revisionHistory
40
 * @property-read \Illuminate\Database\Eloquent\Collection|\Spatie\Activitylog\Models\Activity[] $activity
41
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Comment[] $comments
42
 * @property-read mixed $votes
43
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\TagRelation[] $tags
44
 * @property-read \App\Models\User $user
45
 */
46
class Resource extends Model
47
{
48
    use \Venturecraft\Revisionable\RevisionableTrait;
49
    use LogsActivity;
50
51
    protected $table = 'resources';
52
53
    public $timestamps = true;
54
55
    protected $fillable = [
56
        'type',
57
        'cat',
58
        'user_id',
59
        'title',
60
        'desc_md',
61
        'desc_html',
62
        'content_type',
63
        'content_path',
64
    ];
65
66
    protected $hidden = [
67
        'votes',
68
    ];
69
70
    protected $guarded = [];
71
    protected $appends = ['votes'];
72
73
    public function user(){
74
        return $this->hasOne('App\Models\User', 'id', 'user_id');
75
    }
76
77 View Code Duplication
    public function getVotesAttribute()
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...
78
    {
79
        $vote['up'] = intval($this->comments()->sum('vote_up'));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vote was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vote = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
80
        $vote['down'] = intval($this->comments()->sum('vote_down'));
81
        $vote['avg'] = @round(($vote['up'] - $vote['down']) / ($vote['up'] + $vote['down']), 2);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
82
        //(voteup - votedown) / (voteup + votedown)
83
        return $vote;
84
    }
85
86
    public function comments()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
87
    {
88
        return $this->hasMany('App\Models\Comment', 'content_id', 'id')->Where('content_type', '=', \DB::raw("'resource'"))->with('user');
89
    }
90
91
    public function tags()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
92
    {
93
        return $this->hasMany('App\Models\TagRelation', 'content_id', 'id')->Where('content_type', '=', \DB::raw("'resource'"))->with('tag');
94
    }
95
}
96