Issues (12)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Traits/Rateable.php (4 issues)

Labels
Severity

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
namespace Yoeunes\Rateable\Traits;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Eloquent\Builder;
7
use Illuminate\Database\Query\JoinClause;
8
use Yoeunes\Rateable\Builders\RatingBuilder;
9
use Yoeunes\Rateable\Builders\RatingQueryBuilder;
10
use Yoeunes\Rateable\Exceptions\InvalidRatingValue;
11
12
trait Rateable
13
{
14
    /**
15
     * This model has many ratings.
16
     *
17
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
18
     */
19
    public function ratings()
20
    {
21
        return $this->morphMany(config('rateable.rating'), 'rateable');
0 ignored issues
show
It seems like morphMany() 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...
22
    }
23
24
    public function averageRating()
25
    {
26
        return $this->ratings()->avg('value');
27
    }
28
29
    public function countRating()
30
    {
31
        return $this->ratings()->count();
32
    }
33
34
    public function totalRating()
35
    {
36
        return $this->ratings()->sum('value');
37
    }
38
39
    public function averageRatingForUser(int $user_id)
40
    {
41
        return $this->ratings()->where('user_id', $user_id)->avg('value');
42
    }
43
44
    public function totalRatingForUser(int $user_id)
45
    {
46
        return $this->ratings()->where('user_id', $user_id)->sum('value');
47
    }
48
49
    public function countRatingForUser(int $user_id)
50
    {
51
        return $this->ratings()->where('user_id', $user_id)->count();
52
    }
53
54
    public function ratingPercentage()
55
    {
56
        $max = config('rateable.max_rating');
57
58
        $quantity = $this->ratings()->count();
59
60
        $total = $this->totalRating();
61
62
        return ($quantity * $max) > 0 ? $total / (($quantity * $max) / 100) : 0;
63
    }
64
65
    public function positiveRatingCount()
66
    {
67
        return $this->ratings()->where('value', '>=', '0')->count();
68
    }
69
70
    public function positiveRatingTotal()
71
    {
72
        return $this->ratings()->where('value', '>=', '0')->sum('value');
73
    }
74
75
    public function negativeRatingCount()
76
    {
77
        return $this->ratings()->where('value', '<', '0')->count();
78
    }
79
80
    public function negativeRatingTotal()
81
    {
82
        return $this->ratings()->where('value', '<', '0')->sum('value');
83
    }
84
85
    public function isRated()
86
    {
87
        return $this->ratings()->exists();
88
    }
89
90
    public function isRatedBy(int $user_id)
91
    {
92
        return $this->ratings()->where('user_id', $user_id)->exists();
93
    }
94
95
    /**
96
     * to order by average_rating.
97
     *
98
     * add protected $appends = [ 'average_rating' ]; to your model
99
     *
100
     * Lesson::all()->sortBy('average_rating')
101
     * Lesson::with('relatedModel')->get()->sortBy('average_rating')
102
     * Lesson::where('status', 'published')->get()->sortBy('average_rating')
103
     *
104
     * @return mixed
105
     */
106
    public function getAverageRatingAttribute()
107
    {
108
        return $this->averageRating();
109
    }
110
111
    public function scopeOrderByAverageRating(Builder $query, string $direction = 'asc')
112
    {
113
        return $query
114
            ->leftJoin('ratings', function (JoinClause $join) {
115
                $join
116
                    ->on('ratings.rateable_id', $this->getTable() . '.id')
0 ignored issues
show
It seems like getTable() 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...
117
                    ->where('ratings.rateable_type', morph_type(__CLASS__));
118
            })
119
            ->addSelect(DB::raw('AVG(ratings.value) as average_rating'))
120
            ->groupBy($this->getTable(). '.id')
0 ignored issues
show
It seems like getTable() 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...
121
            ->orderBy('average_rating', $direction);
122
    }
123
124
    public function deleteRating(int $rating_id)
125
    {
126
        return $this->ratings()->where('id', $rating_id)->delete();
127
    }
128
129
    public function resetRating()
130
    {
131
        return $this->ratings()->delete();
132
    }
133
134
    public function deleteRatingsForUser(int $user_id)
135
    {
136
        return $this->ratings()->where('user_id', $user_id)->delete();
137
    }
138
139
    /**
140
     * @param int $user_id
141
     * @param int $value
142
     *
143
     * @return int
144
     *
145
     * @throws \Throwable
146
     */
147
    public function updateRatingForUser(int $user_id, int $value)
148
    {
149
        throw_if($value < config('rateable.min_rating') || $value > config('rateable.max_rating'), InvalidRatingValue::class, 'Invalid rating value');
150
151
        return $this->ratings()->where('user_id', $user_id)->update(['value' => $value]);
152
    }
153
154
    /**
155
     * @param int $rating_id
156
     * @param int $value
157
     *
158
     * @return int
159
     *
160
     * @throws \Throwable
161
     */
162
    public function updateRating(int $rating_id, int $value)
163
    {
164
        throw_if($value < config('rateable.min_rating') || $value > config('rateable.max_rating'), InvalidRatingValue::class, 'Invalid rating value');
165
166
        return $this->ratings()->where('id', $rating_id)->update(['value' => $value]);
167
    }
168
169
    /**
170
     * @return RatingBuilder
171
     *
172
     * @throws \Throwable
173
     */
174
    public function getRatingBuilder()
175
    {
176
        return (new RatingBuilder())
177
            ->rateable($this);
178
    }
179
180
    public function raters()
181
    {
182
        return $this->morphToMany(config('rateable.user'), 'rateable', 'ratings');
0 ignored issues
show
It seems like morphToMany() 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...
183
    }
184
185
    public function countRatingsByDate($from = null, $to = null)
186
    {
187
        $query = $this->ratings();
188
189
        if (! empty($from) && empty($to)) {
190
            $query->where('created_at', '>=', date_transformer($from));
191
        } elseif (empty($from) && ! empty($to)) {
192
            $query->where('created_at', '<=', date_transformer($to));
193
        } elseif (! empty($from) && ! empty($to)) {
194
            $query->whereBetween('created_at', [date_transformer($from), date_transformer($to)]);
195
        }
196
197
        return $query->sum('value');
198
    }
199
200
    public function getRatingQueryBuilder()
201
    {
202
        return new RatingQueryBuilder($this->ratings());
203
    }
204
}
205