Completed
Push — work-fleets ( 0f036f...867546 )
by SuperNova.WS
06:49
created

GlobalContainer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 31
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 60
ccs 0
cts 23
cp 0
crap 2
rs 9.5555

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
7
/**
8
 * Class GlobalContainer
9
 *
10
 * Used to describe internal structures of container
11
 *
12
 * @package Pimple
13
 *
14
 * @property \debug                  $debug
15
 * @property \db_mysql               $db
16
 * @property \classCache             $cache
17
 * @property \classConfig            $config
18
 * @property \classLocale            $localePlayer
19
 * @property string                  $buddyClass
20
 * @property \Buddy\BuddyModel       $buddyModel
21
 * @property \V2Unit\V2UnitModel     $unitModel
22
 * @property \V2Unit\V2UnitContainer $unitContainer
23
 * @property \DbQueryConstructor     $query
24
 * @property \DbRowDirectOperator    $dbRowOperator
25
 * @property \SnDbCachedOperator     $cacheOperator - really DB record operator. But let it be
26
 * @property string                  $snCacheClass
27
 * @property \SnCache                $snCache
28
 */
29
class GlobalContainer extends ContainerPlus {
30
31
  public function __construct(array $values = array()) {
32
    parent::__construct($values);
33
34
    $gc = $this;
35
36
    // Default db
37
    $gc->db = function ($c) {
38
      classSupernova::$db = $db = new \db_mysql($c);
39
      $db->sn_db_connect();
40
41
      return $db;
42
    };
43
44
    $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...
45
      return new \debug();
46
    };
47
48
    $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...
49
      return new \classCache(classSupernova::$cache_prefix);
50
    };
51
52
    $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...
53
      return new \classConfig(classSupernova::$cache_prefix);
54
    };
55
56
    $gc->localePlayer = function (GlobalContainer $c) {
57
      return new \classLocale($c->config->server_locale_log_usage);
58
    };
59
60
    $gc->dbRowOperator = function ($c) {
61
      return new \DbRowDirectOperator($c);
0 ignored issues
show
Unused Code introduced by
The call to DbRowDirectOperator::__construct() has too many arguments starting with $c.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
    };
63
64
    $gc->query = $gc->factory(function (GlobalContainer $c) {
65
      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...
66
    });
67
68
    $gc->cacheOperator = function (GlobalContainer $gc) {
69
      return new \SnDbCachedOperator($gc);
70
    };
71
72
    $gc->snCacheClass = 'SnCache';
73
    $gc->snCache = function (GlobalContainer $gc) {
74
      return $gc->db->snCache;
75
    };
76
77
78
    $gc->buddyClass = 'Buddy\BuddyModel';
79
    $gc->buddyModel = function (GlobalContainer $c) {
80
      return new $c->buddyClass($c);
81
    };
82
83
    $gc->unitModel = function (GlobalContainer $c) {
84
      return new \V2Unit\V2UnitModel($c);
85
    };
86
    $gc->unitContainer = $gc->factory(function (GlobalContainer $c) {
87
      return new \V2Unit\V2UnitContainer($c);
88
    });
89
90
  }
91
92
}
93