Passed
Pull Request — master (#838)
by Georges
02:41
created

EventReferenceParameter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getParameterValue() 0 3 1
A __construct() 0 5 1
A __invoke() 0 3 1
A setParameterValue() 0 13 3
1
<?php
2
/**
3
 *
4
 * This file is part of Phpfastcache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files.
9
 *
10
 * @author Georges.L (Geolim4)  <[email protected]>
11
 * @author Contributors  https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors
12
 */
13
14
declare(strict_types=1);
15
16
namespace Phpfastcache\Event;
17
18
use Phpfastcache\Exceptions\PhpfastcacheInvalidTypeException;
19
20
class EventReferenceParameter
21
{
22
    protected mixed $parameter;
23
24
    public function __construct(
25
        mixed &$parameter,
26
        protected bool $allowTypeChange = false
27
    ) {
28
        $this->parameter = &$parameter;
29
    }
30
31
    public function getParameterValue(): mixed
32
    {
33
        return $this->parameter;
34
    }
35
36
    /**
37
     * @throws PhpfastcacheInvalidTypeException
38
     */
39
    public function setParameterValue(mixed $newValue): void
40
    {
41
        if (!$this->allowTypeChange) {
42
            $currentType = \gettype($this->parameter);
43
            $newType = \gettype($newValue);
44
            if ($newType !== $currentType) {
45
                throw new PhpfastcacheInvalidTypeException(
46
                    \sprintf('You tried to change the variable type from "%s" to "%s" which is not allowed.', $currentType, $newType)
47
                );
48
            }
49
        }
50
51
        $this->parameter = $newValue;
52
    }
53
54
    public function __invoke(): mixed
55
    {
56
        return $this->getParameterValue();
57
    }
58
}
59