Completed
Push — master ( 5d04a1...0cf74a )
by Vincenzo
02:49
created

LeagueFixtureGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 24
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 21 4
1
<?php
2
3
4
namespace App\Lib\DsManager\Helpers;
5
6
class LeagueFixtureGenerator
7
{
8
    public static function generate(array $teams)
9
    {
10
        $numTeams = count($teams);
11
        $numRounds = ($numTeams - 1);
12
        $halfSize = $numTeams / 2;
13
14
        $away = array_splice($teams, $halfSize);
15
        $home = $teams;
16
        $rounds = [];
17
        for ($i = 0; $i < $numRounds ; $i++) {
18
            for ($j = 0; $j < count($home); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
19
                $rounds[$i][$j]["home_team_id"] = $home[$j]['id'];
20
                $rounds[$i][$j]["away_team_id"] = $away[$j]['id'];
21
            }
22
            if (count($home) + count($away) - 1 > 2) {
23
                array_unshift($away, array_shift(array_splice($home, 1, 1)));
0 ignored issues
show
Bug introduced by
array_splice($home, 1, 1) cannot be passed to array_shift() as the parameter $array expects a reference.
Loading history...
24
                array_push($home, array_pop($away));
25
            }
26
        }
27
        return $rounds;
28
    }
29
}