SetByClosure   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 46
ccs 10
cts 10
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 10 2
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\Internal\AccessorStrategy;
10
11
use Closure;
12
use Tebru\Gson\Internal\SetterStrategy;
13
14
/**
15
 * Class SetByClosure
16
 *
17
 * This class contains public properties to improve performance.
18
 *
19
 * @author Nate Brunette <[email protected]>
20
 */
21
final class SetByClosure implements SetterStrategy
22
{
23
    /**
24
     * @var string
25
     */
26
    public $propertyName;
27
28
    /**
29
     * @var string
30
     */
31
    public $className;
32
33
    /**
34
     * @var Closure
35
     */
36
    public $setter;
37
38
    /**
39
     * Constructor
40
     *
41
     * @param string $propertyName
42
     * @param string $className
43
     */
44 4
    public function __construct(string $propertyName, string $className)
45
    {
46 4
        $this->propertyName = $propertyName;
47 4
        $this->className = $className;
48 4
    }
49
50
    /**
51
     * Set object value by binding a closure to the class
52
     *
53
     * @param object $object
54
     * @param mixed $value
55
     * @return void
56
     */
57 3
    public function set($object, $value): void
58
    {
59 3
        if (null === $this->setter) {
60
            $this->setter = Closure::bind(static function ($object, $value, string $propertyName) {
61 3
                $object->{$propertyName} = $value;
62 3
            }, null, $this->className);
63
        }
64
65 3
        $setter = $this->setter;
66 3
        $setter($object, $value, $this->propertyName);
67 3
    }
68
}
69