ObjectSnapshot   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 11
Bugs 1 Features 1
Metric Value
wmc 7
c 11
b 1
f 1
lcom 1
cbo 1
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 24 5
A isComparable() 0 8 2
1
<?php
2
/**
3
 * This file is part of the Totem package
4
 *
5
 * For the full copyright and license information, please view the LICENSE file
6
 * that was distributed with this source code.
7
 *
8
 * @copyright Baptiste Clavié <[email protected]>
9
 * @license   http://www.opensource.org/licenses/MIT-License MIT License
10
 */
11
12
namespace Totem\Snapshot;
13
14
use ReflectionObject;
15
use InvalidArgumentException;
16
17
use Totem\AbstractSnapshot;
18
19
/**
20
 * Represents a snapshot of an object at a given time
21
 *
22
 * @author Baptiste Clavié <[email protected]>
23
 */
24
class ObjectSnapshot extends AbstractSnapshot
25
{
26
    /** @var string object's hash */
27
    protected $oid;
28
29
    /**
30
     * Build this snapshot.
31
     *
32
     * @param object $object Object to fix at the current moment
33
     *
34
     * @throws InvalidArgumentException If this is not an object
35
     */
36
    public function __construct($object)
37
    {
38
        if (!is_object($object)) {
39
            throw new InvalidArgumentException(sprintf('Object expected, had "%s"', gettype($object)));
40
        }
41
42
        $this->raw = $object;
43
        $this->oid = spl_object_hash($object);
44
        $refl      = new ReflectionObject($object);
45
46
        if (method_exists('\\ReflectionObject', 'isCloneable') && $refl->isCloneable()) {
47
            $this->raw = clone $object;
48
        }
49
50
        /** @var \ReflectionProperty $reflProperty */
51
        foreach ($refl->getProperties() as $reflProperty) {
52
            $reflProperty->setAccessible(true);
53
            $value = $reflProperty->getValue($object);
54
55
            $this->data[$reflProperty->getName()] = $value;
56
        }
57
58
        parent::normalize();
59
    }
60
61
    /** {@inheritDoc} */
62
    public function isComparable(AbstractSnapshot $snapshot)
63
    {
64
        if (!parent::isComparable($snapshot)) {
65
            return false;
66
        }
67
68
        return $snapshot->oid === $this->oid;
69
    }
70
}
71
72