Issues (121)

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.

app/Tournament.php (4 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
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class Tournament
10
 * @package App
11
 */
12
class Tournament extends Model
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $table = 'tournaments';
18
19
    /**
20
     * @var array
21
     */
22
    protected $fillable = ['name', 'description', 'status', 'type', 'membersType'];
23
24
    /**
25
     * @var bool
26
     */
27
    public $timestamps = true;
28
29
    const MEMBERS_TYPE_SINGLE = 'single';
30
    const MEMBERS_TYPE_DOUBLE = 'double';
31
32
    const TYPE_LEAGUE = 'league';
33
    const TYPE_KNOCK_OUT = 'knock_out';
34
    const TYPE_MULTISTAGE = 'multistage';
35
36
    const STATUS_DRAFT = 'draft';
37
    const STATUS_STARTED = 'started';
38
    const STATUS_COMPLETED = 'completed';
39
    const STATUS_DELETED = 'deleted';
40
41
    const MIN_TEAMS_AMOUNT = 2;
42
43
    /**
44
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
45
     */
46
    public function tournamentTeams()
47
    {
48
        return $this->hasMany(TournamentTeam::class, 'tournamentId');
49
    }
50
51
    /**
52
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
53
     */
54
    public function matches()
55
    {
56
        return $this->hasMany(Match::class, 'tournamentId');
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public static function getAvailableMembersType()
63
    {
64
        return [
65
            self::MEMBERS_TYPE_SINGLE,
66
            self::MEMBERS_TYPE_DOUBLE,
67
        ];
68
    }
69
70
    /**
71
     * @return array
72
     */
73
    public static function getAvailableTypes()
74
    {
75
        return [
76
            self::TYPE_LEAGUE,
77
            self::TYPE_KNOCK_OUT,
78
            self::TYPE_MULTISTAGE
79
        ];
80
    }
81
82
    /**
83
     * @return array
84
     */
85
    public static function getAvailableStatuses()
86
    {
87
        return [
88
            self::STATUS_DRAFT,
89
            self::STATUS_STARTED,
90
            self::STATUS_COMPLETED,
91
            self::STATUS_DELETED
92
        ];
93
    }
94
95
    /**
96
     * Group matches in tournament into pairs
97
     *
98
     * @return Collection
99
     */
100
    public function getPairs()
101
    {
102
        // get pairs of current round matches
103
        $pairs = new Collection();
104
105
        $this->matches->map(function ($match) use ($pairs) {
0 ignored issues
show
The property matches does not exist on object<App\Tournament>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
107
            $pairId = [$match->homeTournamentTeam->id, $match->awayTournamentTeam->id];
108
            sort($pairId);
109
            $pairId = implode('-', $pairId);
110
111
            $pair = $pairs->pull($pairId);
112
113
            if (!$pair) {
114
                $teams = new Collection([
115
                    $match->homeTournamentTeam->with('Team')->get(),
116
                    $match->awayTournamentTeam->with('Team')->get()
117
                ]);
118
119
                $pair = new Collection([
120
                    'id' => $pairId,
121
                    'round' => $match->round,
122
                    'teams' => $teams,
123
                    'matches' => new Collection()
124
                ]);
125
            }
126
127
            $pair->get('matches')->push($match);
128
129
            $pairs->put($pairId, $pair);
130
        });
131
132
        return $pairs;
133
    }
134
135
    private function matchScore($match, $homeTeam, $awayTeam)
136
    {
137
        if (Match::STATUS_FINISHED == $match->status) {
138
            $homeTeam['matches']++;
139
            $awayTeam['matches']++;
140
            $homeTeam['goalsScored'] += $match->homeScore;
141
            $homeTeam['goalsAgainsted'] += $match->awayScore;
142
            $homeTeam['goalsDifference'] = ($homeTeam['goalsScored'] - $homeTeam['goalsAgainsted']);
143
144
            $awayTeam['goalsScored'] += $match->awayScore;
145
            $awayTeam['goalsAgainsted'] += $match->homeScore;
146
            $awayTeam['goalsDifference'] = ($awayTeam['goalsScored'] - $awayTeam['goalsAgainsted']);
147
148
            switch ($match->resultType) {
149 View Code Duplication
                case Match::RESULT_TYPE_HOME_WIN:
0 ignored issues
show
This code seems to be duplicated across 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...
150
                    $homeTeam['wins']++;
151
                    $homeTeam['points'] += Match::POINTS_WIN;
152
                    $awayTeam['losts']++;
153
154
                    break;
155 View Code Duplication
                case Match::RESULT_TYPE_AWAY_WIN:
0 ignored issues
show
This code seems to be duplicated across 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...
156
                    $awayTeam['wins']++;
157
                    $homeTeam['losts']++;
158
                    $awayTeam['points'] += Match::POINTS_WIN;
159
160
                    break;
161 View Code Duplication
                case Match::RESULT_TYPE_DRAW:
0 ignored issues
show
This code seems to be duplicated across 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...
162
                    $homeTeam['draws']++;
163
                    $awayTeam['draws']++;
164
                    $homeTeam['points'] += Match::POINTS_DRAW;
165
                    $awayTeam['points'] += Match::POINTS_DRAW;
166
167
                    break;
168
            }
169
        }
170
171
        return ['homeTeam' => $homeTeam, 'awayTeam' => $awayTeam];
172
    }
173
174
    /**
175
     * @todo Make a refactoring for `pair` entity
176
     * @name getScore
177
     * @param Collection $matches
178
     * @return Collection|static
179
     */
180
    public function getScore(Collection $matches)
181
    {
182
        $score = new Collection();
183
184
        $matches->map(function ($match) use ($score) {
185
186
            $homeTeam = $score->pull($match->homeTournamentTeam->id);
187
            $awayTeam = $score->pull($match->awayTournamentTeam->id);
188
189
            $defaultTeamData = [
190
                'matches' => 0,
191
                'position' => 0,
192
                'wins' => 0,
193
                'draws' => 0,
194
                'losts' => 0,
195
                'points' => 0,
196
                'goalsScored' => 0,
197
                'goalsAgainsted' => 0,
198
                'goalsDifference' => 0,
199
            ];
200
201
            if (!$homeTeam) {
202
                $homeTeam = array_merge(
203
                    [
204
                        'teamId' => $match->homeTournamentTeam->id,
205
                        'name' => $match->homeTournamentTeam->team->name,
206
                    ],
207
                    $defaultTeamData
208
                );
209
            }
210
211
            if (!$awayTeam) {
212
                $awayTeam = array_merge(
213
                    [
214
                        'teamId' => $match->awayTournamentTeam->id,
215
                        'name' => $match->awayTournamentTeam->team->name,
216
                    ],
217
                    $defaultTeamData
218
                );
219
            }
220
221
            $teams =$this->matchScore($match, $homeTeam, $awayTeam);
222
            $score->put($match->homeTournamentTeam->id, $teams['homeTeam']);
223
            $score->put($match->awayTournamentTeam->id, $teams['awayTeam']);
224
        });
225
226
        // sort by points and goal difference
227
        $score = $score->sort(function ($a, $b) {
228
            if ($b['points'] === $a['points']) {
229
                return $b['goalsDifference'] - $a['goalsDifference'];
230
            }
231
232
            return $b['points'] - $a['points'];
233
        });
234
235
        $previousRow = null;
236
        $position = 1;
237
        $score = $score->map(function ($row) use (&$previousRow, &$position) {
238
            if ($previousRow
239
                && $previousRow['points'] > 0
240
                && $previousRow['points'] == $row['points']
241
                && $previousRow['goalsDifference'] == $row['goalsDifference']
242
                && $previousRow['goalsScored'] == $row['goalsScored']
243
            ) {
244
                $row['position'] = $previousRow['position'];
245
            } else {
246
                $row['position'] = $position;
247
            }
248
249
            $position++;
250
251
            $previousRow = $row;
252
253
            return $row;
254
        });
255
256
        // alphabetical sort for teams on the same position
257
        $score = $score->sortBy(function ($team) {
258
            return $team['position'] . '-' . $team['name'];
259
        }, SORT_NUMERIC);
260
261
        return $score;
262
    }
263
264
    /**
265
     * @name getWinner
266
     * @param Collection $matches
267
     * @return mixed
268
     */
269
    public function getWinner(Collection $matches)
270
    {
271
        return $this->getScore($matches)->first();
272
    }
273
274
    /**
275
     * @return int
276
     */
277
    public function getCurrentRound()
278
    {
279
        $matches = $this->matches()->get();
280
281
        if ($matches->count() === 0) {
282
            return 0;
283
        } else {
284
            return $matches->pluck('round')->max();
285
        }
286
    }
287
}
288