Completed
Push — master ( c60305...7f3e18 )
by Vincenzo
02:47
created

MatchSimulator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 48.35 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 11
c 3
b 1
f 1
lcom 1
cbo 3
dl 44
loc 91
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A simulateRound() 0 13 2
B simulateCompleteResult() 22 22 4
B simulateSimpleResult() 22 22 4
B simulate() 0 24 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
4
namespace App\Lib\DsManager\Helpers;
5
6
7
use App\Lib\DsManager\Models\Orm\Match;
8
use App\Lib\DsManager\Models\Orm\Match as MatchOrm;
9
use App\Lib\DsManager\Models\Orm\MatchResult;
10
11
/**
12
 * Class MatchSimulator
13
 * @package App\Lib\DsManager\Helpers
14
 */
15
class MatchSimulator
16
{
17
    public static function simulateRound($roundId)
18
    {
19
        $matches = Match::where(
20
            [
21
                'league_round_id' => $roundId
22
            ]
23
        )->get();
24
        $result = [];
25
        foreach ($matches as $match) {
26
            $result[] = self::simulateSimpleResult($match->id)->toArray();
27
        }
28
        return json_encode($result);
29
    }
30
31
    /**
32
     * @param $matchId
33
     * @return mixed
34
     */
35 View Code Duplication
    public static function simulateCompleteResult($matchId)
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...
36
    {
37
        $result = MatchResult::complete()->where(
38
            [
39
                'id' => $matchId
40
            ]
41
        )->first();
42
43
        if (!empty($result)
44
            && !$result->simulated
45
            && self::simulate($matchId) === 1
46
        ) {
47
            $result = MatchResult::complete()
48
                ->where(
49
                    [
50
                        'id' => $matchId
51
                    ]
52
                )->first();
53
        }
54
55
        return $result;
56
    }
57
58 View Code Duplication
    public
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...
59
    static function simulateSimpleResult($matchId)
60
    {
61
        $result = MatchResult::teams()->where(
62
            [
63
                'id' => $matchId
64
            ]
65
        )->first();
66
67
        if (!empty($result)
68
            && !$result->simulated
69
            && self::simulate($matchId) === 1
70
        ) {
71
            $result = MatchResult::teams()
72
                ->where(
73
                    [
74
                        'id' => $matchId
75
                    ]
76
                )->first();
77
        }
78
        return $result;
79
    }
80
81
    private
82
    static function simulate($matchId)
83
    {
84
        $match = \App\Lib\DsManager\Models\Match::fromArray(
85
            MatchOrm::complete()
0 ignored issues
show
Bug introduced by
The method complete() does not exist on App\Lib\DsManager\Models\Orm\Match. Did you maybe mean scopeComplete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
86
                ->where(
87
                    [
88
                        'id' => $matchId
89
                    ]
90
                )->first()->toArray()
91
        );
92
        $matchResult = $match->simulate()->toArray();
93
        $result = MatchResult::where(
94
            [
95
                'id' => $matchId
96
            ]
97
        )->update(
98
            MatchResult::resolveAttributes(
99
                $matchResult,
100
                $matchId
101
            )
102
        );
103
        return $result;
104
    }
105
}