Issues (9)

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/Voteable.php (7 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\Voteable\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Illuminate\Database\Query\JoinClause;
8
use Illuminate\Support\Facades\DB;
9
use Yoeunes\Voteable\VoteBuilder;
10
use Yoeunes\Voteable\VoteQueryBuilder;
11
12
trait Voteable
13
{
14
    /**
15
     * This model has many votes.
16
     *
17
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
18
     */
19
    public function votes()
20
    {
21
        return $this->morphMany(config('voteable.vote'), 'voteable');
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 votesCount()
25
    {
26
        return $this->votes()->sum('amount');
27
    }
28
29
    /**
30
     * @return mixed
31
     */
32
    public function upVotesCount()
33
    {
34
        return $this->votes()->where('amount', '>=', 0)->sum('amount');
35
    }
36
37
    /**
38
     * @return mixed
39
     */
40
    public function downVotesCount()
41
    {
42
        return $this->votes()->where('amount', '<', 0)->sum('amount');
43
    }
44
45
    public function isVoted()
46
    {
47
        return $this->votes()->exists();
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function isUpVoted()
54
    {
55
        return $this->votes()->where('amount', '>=', 0)->exists();
56
    }
57
58
    /**
59
     * @return bool
60
     */
61
    public function isDownVoted()
62
    {
63
        return $this->votes()->where('amount', '<', 0)->exists();
64
    }
65
66
    public function isVotedByUser(int $user_id)
67
    {
68
        return $this->votes()->where('user_id', $user_id)->exists();
69
    }
70
71
    /**
72
     * @param int $user_id
73
     *
74
     * @return bool
75
     */
76
    public function isUpVotedByUser(int $user_id)
77
    {
78
        return $this->votes()->where('amount', '>=', 0)->where('user_id', $user_id)->exists();
79
    }
80
81
    /**
82
     * @param int $user_id
83
     *
84
     * @return bool
85
     */
86
    public function isDownVotedByUser(int $user_id)
87
    {
88
        return $this->votes()->where('amount', '<', 0)->where('user_id', $user_id)->exists();
89
    }
90
91
    /**
92
     * @param Builder $query
93
     * @param string $direction
94
     * @param string $type
0 ignored issues
show
There is no parameter named $type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
95
     *
96
     * @return Builder
97
     */
98
    public function scopeOrderByVotes(Builder $query, string $direction = 'asc')
99
    {
100
        return $query
101
            ->leftJoin('votes', function (JoinClause $join) {
102
                $join
103
                    ->on('votes.voteable_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...
104
                    ->where('votes.voteable_type', in_array(__CLASS__, Relation::morphMap()) ? array_search(__CLASS__, Relation::morphMap()) : __CLASS__);
105
            })
106
            ->addSelect(DB::raw('SUM(votes.value) as count_votes'))
107
            ->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...
108
            ->orderBy('count_votes', $direction);
109
    }
110
111
    /**
112
     * @param Builder $query
113
     * @param string $direction
114
     * @param string $type
115
     *
116
     * @return Builder
117
     */
118
    public function scopeOrderByUpVotes(Builder $query, string $direction = 'asc', string $type = '>=')
119
    {
120
        return $query
121
            ->leftJoin('votes', function (JoinClause $join) {
122
                $join
123
                    ->on('votes.voteable_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...
124
                    ->where('votes.voteable_type', in_array(__CLASS__, Relation::morphMap()) ? array_search(__CLASS__, Relation::morphMap()) : __CLASS__);
125
            })
126
            ->where('amount', $type, 0)
127
            ->addSelect(DB::raw('SUM(votes.value) as count_votes'))
128
            ->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...
129
            ->orderBy('count_votes', $direction);
130
    }
131
132
    /**
133
     * @param Builder $query
134
     * @param string $direction
135
     *
136
     * @return Builder
137
     */
138
    public function scopeOrderByDownVotes(Builder $query, string $direction = 'asc')
139
    {
140
        return $this->scopeOrderByUpVotes($query, $direction, '<');
141
    }
142
143
    /**
144
     * @param int $vote_id
145
     *
146
     * @return mixed
147
     */
148
    public function cancelVote(int $vote_id)
149
    {
150
        return $this->votes()->where('id', $vote_id)->delete();
151
    }
152
153
    /**
154
     * @return mixed
155
     */
156
    public function resetVotes()
157
    {
158
        return $this->votes()->delete();
159
    }
160
161
    /**
162
     * @param int $user_id
163
     *
164
     * @return mixed
165
     */
166
    public function cancelVotesForUser(int $user_id)
167
    {
168
        return $this->votes()->where('user_id', $user_id)->delete();
169
    }
170
171
    /**
172
     * @param int $user_id
173
     * @param int $amount
174
     *
175
     * @return int
176
     */
177
    public function updateVotesForUser(int $user_id, int $amount)
178
    {
179
        return $this->votes()->where('user_id', $user_id)->update(['amount' => $amount]);
180
    }
181
182
    /**
183
     * @param int $vote_id
184
     * @param int $amount
185
     *
186
     * @return int
187
     */
188
    public function updateVote(int $vote_id, int $amount)
189
    {
190
        return $this->votes()->where('id', $vote_id)->update(['amount' => $amount]);
191
    }
192
193
    /**
194
     * @return VoteBuilder
195
     *
196
     * @throws \Throwable
197
     */
198
    public function getVoteBuilder()
199
    {
200
        return (new VoteBuilder())
201
            ->voteable($this);
202
    }
203
204
    /**
205
     * @return VoteQueryBuilder
206
     */
207
    public function getVoteQueryBuilder()
208
    {
209
        return new VoteQueryBuilder($this->votes());
210
    }
211
212
    public function voters()
213
    {
214
        return $this->morphToMany(config('voteable.user'), 'voteable', 'votes');
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...
215
    }
216
217
    public function countVotesByDate($from = null, $to = null)
218
    {
219
        $query = $this->votes();
220
221
        if (! empty($from) && empty($to)) {
222
            $query->where('created_at', '>=', date_transformer($from));
223
        } elseif (empty($from) && ! empty($to)) {
224
            $query->where('created_at', '<=', date_transformer($to));
225
        } elseif (! empty($from) && ! empty($to)) {
226
            $query->whereBetween('created_at', [date_transformer($from), date_transformer($to)]);
227
        }
228
229
        return $query->sum('amount');
230
    }
231
}
232