StarCraft   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 147
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 16
c 6
b 0
f 2
lcom 1
cbo 7
dl 14
loc 147
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getProfile() 7 7 1
A getProfileLadders() 7 7 1
A getProfileMatches() 0 13 2
A getLadder() 0 11 2
A getAchievements() 0 15 2
B _restructureCategories() 0 30 5
A getRewards() 0 16 3

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
namespace Jleagle\BattleNet;
3
4
use Jleagle\BattleNet\Request\AbstractBattleNet;
5
use Jleagle\BattleNet\Responses\StarCraft\AchievementResponse;
6
use Jleagle\BattleNet\Responses\StarCraft\LadderMemberResponse;
7
use Jleagle\BattleNet\Responses\StarCraft\LaddersResponse;
8
use Jleagle\BattleNet\Responses\StarCraft\MatchHistoryResponse;
9
use Jleagle\BattleNet\Responses\StarCraft\ProfileResponse;
10
use Jleagle\BattleNet\Responses\StarCraft\RewardResponse;
11
12
class StarCraft extends AbstractBattleNet
13
{
14
  private $_path = 'sc2';
15
16
  /**
17
   * @param int    $id
18
   * @param int    $region
19
   * @param string $name
20
   *
21
   * @return ProfileResponse
22
   */
23 View Code Duplication
  public function getProfile($id, $region, $name)
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...
24
  {
25
    $data = $this->_get(
26
      $this->_path . '/profile/' . $id . '/' . $region . '/' . $name . '/'
27
    );
28
    return new ProfileResponse($data);
29
  }
30
31
  /**
32
   * @param int    $id
33
   * @param int    $region
34
   * @param string $name
35
   *
36
   * @return LaddersResponse
37
   */
38 View Code Duplication
  public function getProfileLadders($id, $region, $name)
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...
39
  {
40
    $data = $this->_get(
41
      $this->_path . '/profile/' . $id . '/' . $region . '/' . $name . '/ladders'
42
    );
43
    return new LaddersResponse($data);
44
  }
45
46
  /**
47
   * @param int    $id
48
   * @param int    $region
49
   * @param string $name
50
   *
51
   * @return MatchHistoryResponse[]
52
   */
53
  public function getProfileMatches($id, $region, $name)
54
  {
55
    $data = $this->_get(
56
      $this->_path . '/profile/' . $id . '/' . $region . '/' . $name . '/matches'
57
    );
58
59
    $return = [];
60
    foreach($data['matches'] as $match)
61
    {
62
      $return[] = new MatchHistoryResponse($match);
63
    }
64
    return $return;
65
  }
66
67
  /**
68
   * @param string $ladderId
69
   *
70
   * @return LadderMemberResponse[]
71
   */
72
  public function getLadder($ladderId) // todo, make enum
73
  {
74
    $data = $this->_get($this->_path . '/ladder/' . $ladderId);
75
76
    $return = [];
77
    foreach($data['ladderMembers'] as $member)
78
    {
79
      $return[] = new LadderMemberResponse($member);
80
    }
81
    return $return;
82
  }
83
84
  /**
85
   * @return AchievementResponse[]
86
   */
87
  public function getAchievements()
88
  {
89
    $data = $this->_get($this->_path . '/data/achievements');
90
    $categories = $this->_restructureCategories($data['categories']);
91
92
    $achievements = [];
93
    foreach($data['achievements'] as $key => $achievement)
94
    {
95
      $categoryId = $achievement['categoryId'];
96
      $achievement['category'] = $categories[$categoryId];
97
      unset($achievement['categoryId']);
98
      $achievements[] = new AchievementResponse($achievement);
99
    }
100
    return $achievements;
101
  }
102
103
  /**
104
   * @param array $categories
105
   *
106
   * @return array
107
   */
108
  private function _restructureCategories(array $categories)
109
  {
110
    $return = [];
111
    foreach($categories as $catKey => $cat)
112
    {
113
      $catId = $cat['categoryId'];
114
      $return[$catId] = [
115
        'title'                 => $cat['title'],
116
        'categoryId'            => $cat['categoryId'],
117
        'featuredAchievementId' => $cat['featuredAchievementId']
118
      ];
119
120
      if(isset($cat['children']) && is_array($cat['children']))
121
      {
122
        foreach($cat['children'] as $subCatKey => $subCat)
123
        {
124
          $subCatId = $subCat['categoryId'];
125
          $return[$subCatId] = [
126
            'title'                       => $subCat['title'],
127
            'categoryId'                  => $subCat['categoryId'],
128
            'featuredAchievementId'       => $subCat['featuredAchievementId'],
129
            'parentTitle'                 => $cat['title'],
130
            'parentCategoryId'            => $cat['categoryId'],
131
            'parentFeaturedAchievementId' => $cat['featuredAchievementId']
132
          ];
133
        }
134
      }
135
    }
136
    return $return;
137
  }
138
139
  /**
140
   * @return RewardResponse[]
141
   */
142
  public function getRewards()
143
  {
144
    $data = $this->_get($this->_path . '/data/rewards');
145
146
    $return = [];
147
    foreach($data as $type => $rewards)
148
    {
149
      foreach($rewards as $reward)
150
      {
151
        $reward['type'] = $type;
152
        $return[] = new RewardResponse($reward);
153
      }
154
    }
155
156
    return $return;
157
  }
158
}
159