Completed
Push — master ( 944c9c...280a9a )
by Antonio Carlos
03:20 queued 10s
created

Countries::instantiateHelper()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Countries\Package\Services;
4
5
use PragmaRX\Countries\Update\Updater;
6
use IlluminateAgnostic\Str\Support\Str;
7
use PragmaRX\Countries\Package\Support\Base;
8
use PragmaRX\Coollection\Package\Coollection;
9
use PragmaRX\Countries\Package\Data\Repository;
10
use PragmaRX\Countries\Package\Services\Cache\Service as Cache;
11
12
class Countries extends Base
13
{
14
    /**
15
     * Countries repository.
16
     *
17
     * @var Repository
18
     */
19
    protected $repository;
20
21
    /**
22
     * Helper.
23
     *
24
     * @var Helper
25
     */
26
    protected $helper;
27
28
    /**
29
     * Config.
30
     *
31
     * @var Config
32
     */
33
    protected $config;
34
35
    /**
36
     * Cache.
37
     *
38
     * @var Cache
39
     */
40
    protected $cache;
41
42
    /**
43
     * Updater.
44
     *
45
     * @var updater
46
     */
47
    private $updater;
48
49
    /**
50
     * @var Hydrator
51
     */
52
    private $hydrator;
53
54
    /**
55
     * Service constructor.
56
     *
57
     * @param object $config
58
     * @param Cache $cache
59
     * @param Helper $helper
60
     * @param Hydrator $hydrator
61
     * @param Repository $repository
62
     */
63
    public function __construct(
64
        $config = null,
65
        Cache $cache = null,
66
        Helper $helper = null,
67
        Hydrator $hydrator = null,
68
        Repository $repository = null
69
    ) {
70
        $a = new \PragmaRX\Countries\Package\Services\Cache\Service();
0 ignored issues
show
Unused Code introduced by
$a is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
72
        $this->helper = $this->instantiateHelper($helper);
73
74
        $this->config = $this->instantiateConfig($config);
75
76
        $this->cache = $this->instantiateCache($cache);
77
78
        $this->hydrator = $this->instantiateHydrator($hydrator);
79
80
        $this->repository = $this->instantiateRepository($repository);
81
82
        $this->hydrator->setRepository($this->repository);
83
84
        $this->init();
85
    }
86
87
    /**
88
     * Call a method.
89
     *
90
     * @param $name
91
     * @param array $arguments
92
     * @return bool|mixed
93
     */
94
    public function __call($name, array $arguments = [])
95
    {
96
        $result = $this->repository->call($name, $arguments);
97
98
        if ($this->config->get('hydrate.after')) {
99
            $result = $this->repository->hydrate($result);
100
        }
101
102
        return $result;
103
    }
104
105
    private function createCoollectionMacros()
106
    {
107
        $instance = $this;
108
109
        Coollection::macro('hydrate', function ($elements = null) use ($instance) {
110
            return $instance->hydrate($this, $elements);
0 ignored issues
show
Documentation Bug introduced by
The method hydrate does not exist on object<PragmaRX\Countrie...age\Services\Countries>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
111
        });
112
113
        foreach (Hydrator::HYDRATORS as $hydrator) {
114
            $hydrator = 'hydrate'.Str::studly($hydrator);
115
116
            Coollection::macro($hydrator, function () use ($hydrator, $instance) {
117
                return $instance->getRepository()->getHydrator()->{$hydrator}($this);
118
            });
119
        }
120
    }
121
122
    /**
123
     * Get all currencies.
124
     *
125
     * @return Coollection
126
     */
127
    public function currencies()
128
    {
129
        return coollect($this->repository->currencies())->unique()->sort();
130
    }
131
132
    /**
133
     * Cache instance getter.
134
     *
135
     * @return Cache|Config
136
     */
137
    public function getCache()
138
    {
139
        return $this->cache;
140
    }
141
142
    /**
143
     * Get the config instance.
144
     *
145
     * @return Config
146
     */
147
    public function getConfig()
148
    {
149
        return $this->config;
150
    }
151
152
    /**
153
     * Repository getter.
154
     *
155
     * @return Repository
156
     */
157
    public function getRepository()
158
    {
159
        return $this->repository;
160
    }
161
162
    /**
163
     * Initialize class.
164
     */
165
    protected function init()
166
    {
167
        $this->createCoollectionMacros();
168
169
        $this->defineConstants();
170
171
        $this->repository->boot();
172
    }
173
174
    /**
175
     * Instantiate cache.
176
     *
177
     * @param Cache|null $cache
178
     * @return Cache
179
     */
180
    protected function instantiateCache(Cache $cache = null)
181
    {
182
        if (\is_null($this->cache) || ! \is_null($cache)) {
183
            $this->cache = ! \is_null($cache)
184
                ? $cache
185
                : new Cache($this->config);
186
        }
187
188
        return $this->cache;
189
    }
190
191
    /**
192
     * Instantiate config.
193
     *
194
     * @param object|null $config
195
     * @return Config
196
     */
197
    protected function instantiateConfig($config = null)
198
    {
199
        if (\is_null($this->config) || ! \is_null($config)) {
200
            $this->config = ! \is_null($config)
201
                ? $config
202
                : new Config($this->helper);
0 ignored issues
show
Documentation introduced by
$this->helper is of type object<PragmaRX\Countrie...ackage\Services\Helper>, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
203
        }
204
205
        return $this->config;
206
    }
207
208
    /**
209
     * @param Helper|null $helper
210
     * @return Helper
211
     */
212
    protected function instantiateHelper(Helper $helper = null)
213
    {
214
        $this->helper = \is_null($helper)
215
            ? (\is_null($this->helper)
216
                ? $this->helper = new Helper($this->instantiateConfig())
217
                : $this->helper)
218
            : $helper;
219
220
        return $this->helper;
221
    }
222
223
    /**
224
     * Instantiate hydrator.
225
     *
226
     * @param Hydrator|null $hydrator
227
     * @return Hydrator
228
     */
229
    protected function instantiateHydrator(Hydrator $hydrator = null)
230
    {
231
        if (\is_null($this->hydrator) || ! \is_null($hydrator)) {
232
            $this->hydrator = ! \is_null($hydrator)
233
                ? $hydrator
234
                : new Hydrator($this->config);
235
        }
236
237
        return $this->hydrator;
238
    }
239
240
    /**
241
     * @param $repository
242
     * @return Repository
243
     */
244
    protected function instantiateRepository($repository)
245
    {
246
        if (\is_null($repository)) {
247
            $repository = new Repository(
248
                $this->instantiateCache(),
249
                $this->instantiateHydrator(),
250
                $this->instantiateHelper(),
251
                $this->instantiateConfig()
252
            );
253
        }
254
255
        return $repository;
256
    }
257
258
    /**
259
     * @return Updater
260
     */
261
    protected function instantiateUpdater()
262
    {
263
        if (\is_null($this->updater)) {
264
            $this->updater = new Updater($this->config, $this->helper);
0 ignored issues
show
Documentation introduced by
$this->helper is of type object<PragmaRX\Countrie...ackage\Services\Helper>, but the function expects a object<PragmaRX\Countries\Update\Helper>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation Bug introduced by
It seems like new \PragmaRX\Countries\...>config, $this->helper) of type object<PragmaRX\Countries\Update\Updater> is incompatible with the declared type object<PragmaRX\Countrie...ckage\Services\updater> of property $updater.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
265
        }
266
267
        return $this->updater;
268
    }
269
}
270