Completed
Push — work-fleets ( fc0000...961997 )
by SuperNova.WS
59:17
created

GlobalContainer::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 86
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 11
Bugs 0 Features 0
Metric Value
cc 1
eloc 44
c 11
b 0
f 0
nc 1
nop 1
dl 0
loc 86
rs 8.6583
ccs 0
cts 29
cp 0
crap 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Common;
4
5
use \classSupernova;
6
use Planet\PlanetRenderer;
7
use V2Fleet\V2FleetModel;
8
9
/**
10
 * Class GlobalContainer
11
 *
12
 * Used to describe internal structures of container
13
 *
14
 * @property \debug               $debug
15
 * @property \Common\Types        $types
16
 *
17
 * @property \db_mysql            $db
18
 * @property \DbQueryConstructor  $query
19
 * @property \DbRowDirectOperator $dbGlobalRowOperator
20
 * @property \SnDbCachedOperator  $cacheOperator - really DB record operator. But let it be
21
 *
22
 * @property \classCache          $cache
23
 * @property \classConfig         $config
24
 * @property \classLocale         $localePlayer
25
 *
26
 * @property string               $snCacheClass
27
 * @property \SnCache             $snCache
28
 *
29
 * @property string               $buddyClass
30
 * @property \Buddy\BuddyModel    $buddyModel
31
 *
32
 * @property \V2Unit\V2UnitModel  $unitModel
33
 * @property \V2Unit\V2UnitList   $unitList
34
 *
35
 * @property V2FleetModel         $fleetModel
36
 *
37
 * @property PlanetRenderer       $planetRenderer
38
 * @property \FleetRenderer       $fleetRenderer
39
 *
40
 *
41
 * @property array                $groupFleet
42
 * @property array                $groupFleetAndMissiles
43
 * @property array                $groupRecyclers
44
 *
45
 * @package Common
46
 */
47
class GlobalContainer extends ContainerPlus {
48
49
  public function __construct(array $values = array()) {
50
    parent::__construct($values);
51
52
    $gc = $this;
53
54
    // Default db
55
    $gc->db = function ($c) {
56
      classSupernova::$db = $db = new \db_mysql($c);
57
58
      return $db;
59
    };
60
61
    $gc->debug = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
62
      return new \debug();
63
    };
64
65
    $gc->types = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
66
      return new \Common\Types();
67
    };
68
69
    $gc->cache = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
      return new \classCache(classSupernova::$cache_prefix);
71
    };
72
73
    $gc->config = function ($c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
      return new \classConfig(classSupernova::$cache_prefix);
75
    };
76
77
    $gc->localePlayer = function (GlobalContainer $c) {
78
      return new \classLocale($c->config->server_locale_log_usage);
79
    };
80
81
    $gc->dbGlobalRowOperator = function (GlobalContainer $c) {
82
      return new \DbRowDirectOperator($c->db);
0 ignored issues
show
Bug introduced by
It seems like $c->db can also be of type object<Closure>; however, DbRowDirectOperator::__construct() does only seem to accept object<db_mysql>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
83
    };
84
85
    $gc->query = $gc->factory(function (GlobalContainer $c) {
86
      return new \DbQueryConstructor($c->db);
0 ignored issues
show
Bug introduced by
It seems like $c->db can also be of type object<Closure>; however, DbSqlAware::__construct() does only seem to accept object<db_mysql>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
87
    });
88
89
    $gc->cacheOperator = function (GlobalContainer $gc) {
90
      return new \SnDbCachedOperator($gc);
91
    };
92
93
    $gc->snCacheClass = 'SnCache';
94
    $gc->snCache = function (GlobalContainer $gc) {
95
      return $gc->db->snCache;
96
    };
97
98
    $gc->buddyClass = 'Buddy\BuddyModel';
99
    $gc->buddyModel = function (GlobalContainer $c) {
100
      return new $c->buddyClass($c);
101
    };
102
103
    $gc->unitModel = function (GlobalContainer $c) {
104
      return new \V2Unit\V2UnitModel($c);
105
    };
106
    $gc->unitList = $this->factory(function (GlobalContainer $c) {
107
      return new \V2Unit\V2UnitList($c);
108
    });
109
110
    $gc->fleetModel = function (GlobalContainer $c) {
111
      return new V2FleetModel($c);
112
    };
113
114
    $gc->planetRenderer = function (GlobalContainer $c) {
115
      return new PlanetRenderer($c);
116
    };
117
118
    $gc->fleetRenderer = function (GlobalContainer $c) {
119
      return new \FleetRenderer($c);
120
    };
121
122
    $gc->groupFleet = function (GlobalContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
      return sn_get_groups('fleet');
124
    };
125
126
    $gc->groupFleetAndMissiles = function (GlobalContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
      return sn_get_groups(array('fleet', GROUP_STR_MISSILES));
128
    };
129
130
    $gc->groupRecyclers = function (GlobalContainer $c) {
0 ignored issues
show
Unused Code introduced by
The parameter $c is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
      return sn_get_groups('flt_recyclers');
132
    };
133
134
  }
135
136
}
137