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

EventReferenceParameter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
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