Reference   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 82
ccs 24
cts 24
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 4 1
A copy() 0 4 1
A swap() 0 11 1
A getRef() 0 4 1
A reset() 0 5 1
A release() 0 5 1
1
<?php
2
/**
3
 * Part of twLib
4
 * http://www.thomaswhiston.com
5
 * [email protected]
6
 * Created by PhpStorm.
7
 * User: Thomas Whiston
8
 * Date: 03/02/2016
9
 * Time: 18:19
10
 */
11
namespace twhiston\twLib\Reference;
12
13
14
/**
15
 * Class Reference
16
 * A way of wrapping references so that they can be more easily worked with,
17
 * behaviour should be consistent as long as you call the functions to get and set.
18
 * Works with the Stack class to allow you to store a reference history, which is useful for traversing nested arrays
19
 * @package twhiston\twLib\Reference
20
 */
21
class Reference
22
{
23
    /**
24
     * The reference
25
     * @var null
26
     */
27
    private $point;
28
29
    /**
30
     * Reference constructor.
31
     * Pass in the thing you want to get the reference of
32
     * @param null|mixed $ref
33
     */
34 6
    public function __construct(&$ref = null)
35
    {
36 6
        $this->point = &$ref;
37 6
    }
38
39
    /**
40
     * Set data at the location the reference points to
41
     * @param $data
42
     */
43 3
    public function set($data)
44
    {
45 3
        $this->point = $data;
46 3
    }
47
48
    /**
49
     * Return a copy of the data references to by this reference instance
50
     * @return null
51
     */
52 3
    public function copy()
53
    {
54 3
        return $this->point;
55
    }
56
57
    /**
58
     * Swap this reference with another
59
     * @param \twhiston\twLib\Reference\Reference $point
60
     * @return mixed|null
61
     */
62 2
    public function &swap(Reference &$point)
63
    {
64
65 2
        $tmp =  &$this->point;
66 2
        $pr = &$point->getRef();
67 2
        $this->reset($pr);
68 2
        $point->reset($tmp);
69
70 2
        return $tmp;
71
72
    }
73
74
    /**
75
     * Remember that you MUST put & infront of the function call as well to actually get a reference
76
     * This will return the memory location of the object pointed to by $this->point
77
     * @return null|mixed
78
     */
79 4
    public function &getRef()
80
    {
81 4
        return $this->point;
82
    }
83
84
    /**
85
     * Release the current reference and either set this reference to NULL or to &$ref
86
     * @param null $ref
87
     */
88 2
    public function reset(&$ref = null)
89
    {
90 2
        $this->release();
91 2
        $this->point = &$ref;
92 2
    }
93
94
    /**
95
     * Release the reference
96
     */
97 2
    public function release()
98
    {
99 2
        unset($this->point);
100 2
        $this->point = null;
101
    }
102
}