Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

CleanableMemory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 51
ccs 6
cts 6
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A enterProcessing() 0 4 1
A leaveProcessing() 0 4 2
A addFile() 0 9 2
A clearTokenCache() 0 9 2
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2013, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Instrument;
12
13
use TokenReflection\Broker\Backend\Memory;
14
use TokenReflection\ReflectionFile;
15
use TokenReflection\Stream\StreamBase;
16
17
/**
18
 * Special memory backend that keep token stream only for one file
19
 */
20
class CleanableMemory extends Memory
21
{
22
    /**
23
     * Nested level to track hierarchy
24
     *
25
     * @var int
26
     */
27
    protected static $level = 0;
28
29
    /**
30
     * Increments current level
31
     */
32 8
    public static function enterProcessing()
33
    {
34 8
        self::$level++;
35 8
    }
36
37
    /**
38
     * Decrements current level
39
     */
40 8
    public static function leaveProcessing()
41
    {
42 8
        self::$level = self::$level > 0 ? self::$level - 1 : 0;
43 8
    }
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    public function addFile(StreamBase $tokenStream, ReflectionFile $file)
49
    {
50
        if (self::$level <= 1) {
51
            // Clean the old cache only for main classes, allow to bypass cleaning for hierarchy
52
            $this->clearTokenCache();
53
        }
54
55
        return parent::addFile($tokenStream, $file);
56
    }
57
58
    /**
59
     * Clear token stream cache from parent object
60
     */
61
    private function clearTokenCache()
62
    {
63
        static $tokenStreams = null;
64
        if (!$tokenStreams) {
65
            $tokenStreams = new \ReflectionProperty(get_parent_class(), 'tokenStreams');
66
            $tokenStreams->setAccessible(true);
67
        }
68
        $tokenStreams->setValue($this, []);
69
    }
70
}
71