Completed
Push — work-fleets ( d1f99e...fc0000 )
by SuperNova.WS
06:40
created

GlobalContainer::__construct()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 9
Bugs 0 Features 0
Metric Value
cc 1
eloc 40
c 9
b 0
f 0
nc 1
nop 1
dl 0
loc 78
rs 8.9019
ccs 0
cts 27
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 V2Fleet\V2FleetModel;
7
8
/**
9
 * Class GlobalContainer
10
 *
11
 * Used to describe internal structures of container
12
 *
13
 * @property \debug               $debug
14
 * @property \Common\Types        $types
15
 *
16
 * @property \db_mysql            $db
17
 * @property \DbQueryConstructor  $query
18
 * @property \DbRowDirectOperator $dbGlobalRowOperator
19
 * @property \SnDbCachedOperator  $cacheOperator - really DB record operator. But let it be
20
 *
21
 * @property \classCache          $cache
22
 * @property \classConfig         $config
23
 * @property \classLocale         $localePlayer
24
 *
25
 * @property string               $snCacheClass
26
 * @property \SnCache             $snCache
27
 *
28
 * @property string               $buddyClass
29
 * @property \Buddy\BuddyModel    $buddyModel
30
 *
31
 * @property \V2Unit\V2UnitModel  $unitModel
32
 * @property \V2Unit\V2UnitList   $unitList
33
 *
34
 * @property V2FleetModel         $fleetModel
35
 *
36
 *
37
 * @property array                $groupFleet
38
 * @property array                $groupFleetAndMissiles
39
 * @property array                $groupRecyclers
40
 *
41
 * @package Common
42
 */
43
class GlobalContainer extends ContainerPlus {
44
45
  public function __construct(array $values = array()) {
46
    parent::__construct($values);
47
48
    $gc = $this;
49
50
    // Default db
51
    $gc->db = function ($c) {
52
      classSupernova::$db = $db = new \db_mysql($c);
53
54
      return $db;
55
    };
56
57
    $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...
58
      return new \debug();
59
    };
60
61
    $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...
62
      return new \Common\Types();
63
    };
64
65
    $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...
66
      return new \classCache(classSupernova::$cache_prefix);
67
    };
68
69
    $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...
70
      return new \classConfig(classSupernova::$cache_prefix);
71
    };
72
73
    $gc->localePlayer = function (GlobalContainer $c) {
74
      return new \classLocale($c->config->server_locale_log_usage);
75
    };
76
77
    $gc->dbGlobalRowOperator = function (GlobalContainer $c) {
78
      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...
79
    };
80
81
    $gc->query = $gc->factory(function (GlobalContainer $c) {
82
      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...
83
    });
84
85
    $gc->cacheOperator = function (GlobalContainer $gc) {
86
      return new \SnDbCachedOperator($gc);
87
    };
88
89
    $gc->snCacheClass = 'SnCache';
90
    $gc->snCache = function (GlobalContainer $gc) {
91
      return $gc->db->snCache;
92
    };
93
94
    $gc->buddyClass = 'Buddy\BuddyModel';
95
    $gc->buddyModel = function (GlobalContainer $c) {
96
      return new $c->buddyClass($c);
97
    };
98
99
    $gc->unitModel = function (GlobalContainer $c) {
100
      return new \V2Unit\V2UnitModel($c);
101
    };
102
    $gc->unitList = $this->factory(function (GlobalContainer $c) {
103
      return new \V2Unit\V2UnitList($c);
104
    });
105
106
    $gc->fleetModel = function (GlobalContainer $c) {
107
      return new V2FleetModel($c);
108
    };
109
110
    $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...
111
      return sn_get_groups('fleet');
112
    };
113
114
    $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...
115
      return sn_get_groups(array('fleet', GROUP_STR_MISSILES));
116
    };
117
118
    $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...
119
      return sn_get_groups('flt_recyclers');
120
    };
121
122
  }
123
124
}
125