Passed
Push — v7 ( 968c70...8c8f4f )
by Georges
03:25 queued 01:50
created

ConfigurationOption   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 466
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 466
rs 7.9487
c 0
b 0
f 0
wmc 52

33 Methods

Rating   Name   Duplication   Size   Complexity  
A getFallback() 0 3 1
A setDefaultKeyHashFunction() 0 7 3
A isIgnoreSymfonyNotice() 0 3 1
A setFallbackConfig() 0 11 4
A setCompressData() 0 4 1
A setLimitedMemoryByObject() 0 4 1
A getFallbackConfig() 0 3 1
A setItemDetailedDate() 0 4 1
A setCacheSlamsTimeout() 0 4 1
A setDefaultTtl() 0 4 1
A isValidOption() 0 3 1
A getDefaultKeyHashFunction() 0 3 1
A getOption() 0 4 1
A isPreventCacheSlams() 0 3 1
A getCacheSlamsTimeout() 0 3 1
A getDefaultChmod() 0 3 1
B setCacheFileExtension() 0 24 3
A getPath() 0 3 1
A isAutoTmpFallback() 0 3 1
A isItemDetailedDate() 0 3 1
A getDefaultTtl() 0 3 1
A getDefaultFileNameHashFunction() 0 3 1
A getCacheFileExtension() 0 3 1
A isCompressData() 0 3 1
A setPath() 0 4 1
A setDefaultChmod() 0 4 1
A setAutoTmpFallback() 0 4 1
A setPreventCacheSlams() 0 4 1
A setFallback() 0 4 1
C __construct() 0 49 10
A getLimitedMemoryByObject() 0 3 1
A setDefaultFileNameHashFunction() 0 7 3
A setIgnoreSymfonyNotice() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like ConfigurationOption often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ConfigurationOption, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Geolim4
5
 * Date: 10/02/2018
6
 * Time: 18:45
7
 */
8
9
namespace Phpfastcache\Config;
10
11
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException;
12
use Phpfastcache\Exceptions\PhpfastcacheInvalidConfigurationException;
13
use Phpfastcache\Util\ArrayObject;
14
15
class ConfigurationOption extends ArrayObject
16
{
17
    /**
18
     * @var bool
19
     */
20
    protected $itemDetailedDate = false;
21
22
    /**
23
     * @var bool
24
     */
25
    protected $autoTmpFallback = false;
26
27
    /**
28
     * @var bool
29
     * @deprecated Do not use this option anymore
30
     */
31
    protected $ignoreSymfonyNotice = false;
32
33
    /**
34
     * @var int
35
     */
36
    protected $defaultTtl = 900;
37
38
    /**
39
     * @var string|Callable
40
     */
41
    protected $defaultKeyHashFunction = 'md5';
42
43
    /**
44
     * @var string|Callable
45
     */
46
    protected $defaultFileNameHashFunction = 'md5';
47
48
    /**
49
     * @var int
50
     */
51
    protected $defaultChmod = 0777;
52
53
    /**
54
     * @var string
55
     */
56
    protected $path = '';
57
58
    /**
59
     * @var string
60
     */
61
    protected $fallback = '';
62
63
    /**
64
     * @var \Phpfastcache\Config\ConfigurationOption
65
     */
66
    protected $fallbackConfig;
67
68
    /**
69
     * @var int
70
     */
71
    protected $limitedMemoryByObject = 4096;
72
73
    /**
74
     * @var bool
75
     */
76
    protected $compressData = false;
77
78
    /**
79
     * @var bool
80
     */
81
    protected $preventCacheSlams = false;
82
83
    /**
84
     * @var int
85
     */
86
    protected $cacheSlamsTimeout = 15;
87
88
    /**
89
     * @var string
90
     */
91
    protected $cacheFileExtension = 'txt';
92
93
    /**
94
     * @param $args
95
     * ArrayObject constructor.
96
     */
97
    public function __construct(...$args)
98
    {
99
        parent::__construct(...$args);
100
        $array =& $this->getArray();
101
102
        /**
103
         * Detect unwanted keys and throw an exception.
104
         * No more kidding now, it's 21th century.
105
         */
106
        if(array_diff_key($array, get_object_vars($this))){
107
            throw new PhpfastcacheInvalidConfigurationException(\sprintf(
108
              'Invalid option(s) for the config %s: %s',
109
              static::class,
110
              implode(', ',  array_keys(array_diff_key($array, get_object_vars($this))))
111
            ));
112
        }
113
114
        foreach (get_object_vars($this) as $property => $value) {
115
116
            if(array_key_exists($property, $array)){
117
                $this->$property = &$array[ $property ];
118
            }else{
119
                $array[ $property ] = &$this->$property;
120
            }
121
        }
122
123
        foreach (get_class_methods($this) as $method) {
124
            if(strpos($method, 'set') === 0){
125
                $value = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $value is dead and can be removed.
Loading history...
126
                try{
127
                    /**
128
                     * We use property instead of getter
129
                     * because of is/get conditions and
130
                     * to allow us to retrieve the value
131
                     * in catch statement bloc
132
                     */
133
                    $value = $this->{lcfirst(substr($method, 3))};
134
                    $this->{$method}($value);
135
                }catch(\TypeError $e){
136
                    $typeHintGot = \is_object($value) ? \get_class($value) : \gettype($value);
137
                    $reflectionMethod = new \ReflectionMethod($this, $method);
138
                    $parameter = $reflectionMethod->getParameters()[0] ?? null;
139
                    $typeHintExpected = ($parameter instanceof \ReflectionParameter ? ($parameter->getType() === 'object' ? $parameter->getClass() : $parameter->getType()) : 'Unknown type');
140
141
                    throw new PhpfastcacheInvalidConfigurationException(\sprintf(
142
                      'Invalid type hint found for "%s", expected "%s" got "%s"',
143
                      lcfirst(substr($method, 3)),
144
                      $typeHintExpected,
145
                      $typeHintGot
146
                    ));
147
                }
148
            }
149
        }
150
    }
151
152
    /**
153
     * @param string $optionName
154
     * @return mixed|null
155
     * @deprecated Use ->getOptionName() instead
156
     */
157
    public function getOption(string $optionName)
158
    {
159
        \trigger_error(\sprintf('Method "%s" is deprecated, use "getOptionName()" instead', __METHOD__), E_USER_DEPRECATED);
160
        return $this->$optionName ?? null;
161
    }
162
163
    /**
164
     * @param string $optionName
165
     * @return mixed|null
166
     */
167
    public function isValidOption(string $optionName)
168
    {
169
        return property_exists($this, $optionName);
170
    }
171
172
    /**
173
     * @return bool
174
     */
175
    public function isItemDetailedDate(): bool
176
    {
177
        return $this->itemDetailedDate;
178
    }
179
180
    /**
181
     * @param bool $itemDetailedDate
182
     * @return ConfigurationOption
183
     */
184
    public function setItemDetailedDate(bool $itemDetailedDate): self
185
    {
186
        $this->itemDetailedDate = $itemDetailedDate;
187
        return $this;
188
    }
189
190
    /**
191
     * @return bool
192
     */
193
    public function isAutoTmpFallback(): bool
194
    {
195
        return $this->autoTmpFallback;
196
    }
197
198
    /**
199
     * @param bool $autoTmpFallback
200
     * @return ConfigurationOption
201
     */
202
    public function setAutoTmpFallback(bool $autoTmpFallback): self
203
    {
204
        $this->autoTmpFallback = $autoTmpFallback;
205
        return $this;
206
    }
207
208
    /**
209
     * @return bool
210
     * @deprecated As of V7
211
     */
212
    public function isIgnoreSymfonyNotice(): bool
213
    {
214
        return $this->ignoreSymfonyNotice;
0 ignored issues
show
Deprecated Code introduced by
The property Phpfastcache\Config\Conf...n::$ignoreSymfonyNotice has been deprecated: Do not use this option anymore ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

214
        return /** @scrutinizer ignore-deprecated */ $this->ignoreSymfonyNotice;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
215
    }
216
217
    /**
218
     * @param bool $ignoreSymfonyNotice
219
     * @return ConfigurationOption
220
     * @deprecated As of V7
221
     */
222
    public function setIgnoreSymfonyNotice(bool $ignoreSymfonyNotice): self
223
    {
224
        if($ignoreSymfonyNotice){
225
            \trigger_error('Configuration option "ignoreSymfonyNotice" is deprecated as of the V7', E_USER_DEPRECATED);
226
        }
227
        $this->ignoreSymfonyNotice = $ignoreSymfonyNotice;
0 ignored issues
show
Deprecated Code introduced by
The property Phpfastcache\Config\Conf...n::$ignoreSymfonyNotice has been deprecated: Do not use this option anymore ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

227
        /** @scrutinizer ignore-deprecated */ $this->ignoreSymfonyNotice = $ignoreSymfonyNotice;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
228
        return $this;
229
    }
230
231
    /**
232
     * @return int
233
     */
234
    public function getDefaultTtl(): int
235
    {
236
        return $this->defaultTtl;
237
    }
238
239
    /**
240
     * @param int $defaultTtl
241
     * @return ConfigurationOption
242
     */
243
    public function setDefaultTtl(int $defaultTtl): self
244
    {
245
        $this->defaultTtl = $defaultTtl;
246
        return $this;
247
    }
248
249
    /**
250
     * @return Callable|string
251
     */
252
    public function getDefaultKeyHashFunction()
253
    {
254
        return $this->defaultKeyHashFunction;
255
    }
256
257
    /**
258
     * @param Callable|string $defaultKeyHashFunction
259
     * @return ConfigurationOption
260
     * @throws  PhpfastcacheInvalidConfigurationException
261
     */
262
    public function setDefaultKeyHashFunction($defaultKeyHashFunction)
263
    {
264
        if (!\function_exists($defaultKeyHashFunction) || !\is_callable($defaultKeyHashFunction)) {
0 ignored issues
show
Bug introduced by
It seems like $defaultKeyHashFunction can also be of type callable; however, parameter $function_name of function_exists() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

264
        if (!\function_exists(/** @scrutinizer ignore-type */ $defaultKeyHashFunction) || !\is_callable($defaultKeyHashFunction)) {
Loading history...
265
            throw new PhpfastcacheInvalidConfigurationException('defaultKeyHashFunction must be a valid function name string');
266
        }
267
        $this->defaultKeyHashFunction = $defaultKeyHashFunction;
268
        return $this;
269
    }
270
271
    /**
272
     * @return Callable|string
273
     */
274
    public function getDefaultFileNameHashFunction()
275
    {
276
        return $this->defaultFileNameHashFunction;
277
    }
278
279
    /**
280
     * @param Callable|string $defaultKeyHashFunction
281
     * @return ConfigurationOption
282
     * @throws  PhpfastcacheInvalidConfigurationException
283
     */
284
    public function setDefaultFileNameHashFunction($defaultFileNameHashFunction)
285
    {
286
        if (!\function_exists($defaultFileNameHashFunction) || !\is_callable($defaultFileNameHashFunction)) {
287
            throw new PhpfastcacheInvalidConfigurationException('defaultFileNameHashFunction must be a valid function name string');
288
        }
289
        $this->defaultFileNameHashFunction = $defaultFileNameHashFunction;
290
        return $this;
291
    }
292
293
    /**
294
     * @return int
295
     */
296
    public function getDefaultChmod(): int
297
    {
298
        return $this->defaultChmod;
299
    }
300
301
    /**
302
     * @param int $defaultChmod
303
     * @return ConfigurationOption
304
     */
305
    public function setDefaultChmod(int $defaultChmod): self
306
    {
307
        $this->defaultChmod = $defaultChmod;
308
        return $this;
309
    }
310
311
    /**
312
     * @return string
313
     */
314
    public function getPath(): string
315
    {
316
        return $this->path;
317
    }
318
319
    /**
320
     * @param string $path
321
     * @return ConfigurationOption
322
     */
323
    public function setPath(string $path): self
324
    {
325
        $this->path = $path;
326
        return $this;
327
    }
328
329
    /**
330
     * @return bool|string
331
     */
332
    public function getFallback()
333
    {
334
        return $this->fallback;
335
    }
336
337
    /**
338
     * @param string $fallback
339
     * @return ConfigurationOption
340
     */
341
    public function setFallback(string $fallback): self
342
    {
343
        $this->fallback = $fallback;
344
        return $this;
345
    }
346
347
    /**
348
     * @return \Phpfastcache\Config\ConfigurationOption|null
349
     */
350
    public function getFallbackConfig()
351
    {
352
        return $this->fallbackConfig;
353
    }
354
355
    /**
356
     * @param \Phpfastcache\Config\ConfigurationOption|null $fallbackConfig
357
     * @return ConfigurationOption
358
     */
359
    public function setFallbackConfig($fallbackConfig): self
360
    {
361
        if($fallbackConfig !== null && !($fallbackConfig instanceof self)){
0 ignored issues
show
introduced by
$fallbackConfig is always a sub-type of self.
Loading history...
362
            throw new PhpfastcacheInvalidArgumentException(\sprintf(
363
              'Invalid argument "%s" for %s',
364
              gettype($fallbackConfig) === 'object' ? get_class($fallbackConfig) : gettype($fallbackConfig),
365
              __METHOD__
366
            ));
367
        }
368
        $this->fallbackConfig = $fallbackConfig;
369
        return $this;
370
    }
371
372
    /**
373
     * @return int
374
     */
375
    public function getLimitedMemoryByObject(): int
376
    {
377
        return $this->limitedMemoryByObject;
378
    }
379
380
    /**
381
     * @param int $limitedMemoryByObject
382
     * @return ConfigurationOption
383
     */
384
    public function setLimitedMemoryByObject(int $limitedMemoryByObject): self
385
    {
386
        $this->limitedMemoryByObject = $limitedMemoryByObject;
387
        return $this;
388
    }
389
390
    /**
391
     * @return bool
392
     */
393
    public function isCompressData(): bool
394
    {
395
        return $this->compressData;
396
    }
397
398
    /**
399
     * @param bool $compressData
400
     * @return ConfigurationOption
401
     */
402
    public function setCompressData(bool $compressData): self
403
    {
404
        $this->compressData = $compressData;
405
        return $this;
406
    }
407
408
    /**
409
     * @return bool
410
     */
411
    public function isPreventCacheSlams(): bool
412
    {
413
        return $this->preventCacheSlams;
414
    }
415
416
    /**
417
     * @param bool $preventCacheSlams
418
     * @return ConfigurationOption
419
     */
420
    public function setPreventCacheSlams(bool $preventCacheSlams): self
421
    {
422
        $this->preventCacheSlams = $preventCacheSlams;
423
        return $this;
424
    }
425
426
    /**
427
     * @return int
428
     */
429
    public function getCacheSlamsTimeout(): int
430
    {
431
        return $this->cacheSlamsTimeout;
432
    }
433
434
    /**
435
     * @param int $cacheSlamsTimeout
436
     * @return ConfigurationOption
437
     */
438
    public function setCacheSlamsTimeout(int $cacheSlamsTimeout): self
439
    {
440
        $this->cacheSlamsTimeout = $cacheSlamsTimeout;
441
        return $this;
442
    }
443
444
    /**
445
     * @return string
446
     */
447
    public function getCacheFileExtension(): string
448
    {
449
        return $this->cacheFileExtension;
450
    }
451
452
    /**
453
     * @param string $cacheFileExtension
454
     * @return ConfigurationOption
455
     * @throws PhpfastcacheInvalidConfigurationException
456
     */
457
    public function setCacheFileExtension(string $cacheFileExtension): self
458
    {
459
        /**
460
         * Feel free to propose your own one
461
         * by opening a pull request :)
462
         */
463
        static $safeFileExtensions = [
464
          'txt',
465
          'cache',
466
          'db',
467
          'pfc',
468
        ];
469
470
        if (\strpos($cacheFileExtension, '.') !== false) {
471
            throw new PhpfastcacheInvalidConfigurationException('cacheFileExtension cannot contain a dot "."');
472
        }
473
        if (!\in_array($cacheFileExtension, $safeFileExtensions, true)) {
474
            throw new PhpfastcacheInvalidConfigurationException(
475
              "Extension \"{$cacheFileExtension}\" is not safe, currently allowed extension names: " . \implode(', ', $safeFileExtensions)
476
            );
477
        }
478
479
        $this->cacheFileExtension = $cacheFileExtension;
480
        return $this;
481
    }
482
}