Failed Conditions
Pull Request — master (#6649)
by Marco
140:45 queued 75:42
created

benchUninitializedArrayAccess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Performance\Hydration\Cache;
4
5
use Doctrine\ORM\Internal\Hydration\Cache\LazyPropertyMap;
6
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
7
use stdClass;
8
9
/**
10
 * @BeforeMethods({"init"})
11
 */
12
final class LazyPropertyMapBench
13
{
14
    /**
15
     * @var callable
16
     */
17
    private $initializer;
18
19
    /**
20
     * LazyPropertyMap
21
     */
22
    private $lazyMap;
23
24
    /**
25
     * @var array
26
     */
27
    private $array;
28
29
    /**
30
     * @var int
31
     */
32
    private $currentKey = 0;
33
34
    public function init() : void
35
    {
36
        $this->initializer = function (string $name) : stdClass {
37
            return (object) [$name => true];
38
        };
39
        $this->lazyMap     = new LazyPropertyMap($this->initializer);
40
        $this->array       = [
41
            'initialized' => ($this->initializer)('initialized'),
42
        ];
43
44
        $this->lazyMap->initialized;
0 ignored issues
show
Documentation introduced by
The property initialized does not exist on object<Doctrine\ORM\Inte...\Cache\LazyPropertyMap>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
45
    }
46
47
    public function benchInitializedPropertyAccess() : stdClass
48
    {
49
        $key = 'initialized';
50
51
        return $this->lazyMap->$key;
52
    }
53
54
    public function benchInitializedArrayKeyAccess() : stdClass
55
    {
56
        $key = 'initialized';
57
58
        return $this->array[$key] ?? $this->array[$key] = ($this->initializer)($key);
59
    }
60
61
    public function benchUninitializedPropertyAccess() : stdClass
62
    {
63
        $key = 'key' . $this->currentKey++;
64
65
        return $this->lazyMap->$key;
66
    }
67
68
    public function benchUninitializedArrayAccess() : stdClass
69
    {
70
        $key = 'key' . $this->currentKey++;
71
72
        return $this->array[$key] ?? $this->array[$key] = ($this->initializer)($key);
73
    }
74
}
75