Completed
Push — master ( 389cae...29b40e )
by Richard
06:27
created

CachesReferences   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 64
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A get_reference() 0 23 2
reference() 0 1 ?
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 *
5
 * Copyright (c) 2016, 2015 Richard Klees <[email protected]>
6
 *
7
 * This software is licensed under The MIT License. You should have received
8
 * a copy of the licence along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Indexer;
12
13
use Lechimp\Dicto\Variables\Variable;
14
15
/**
16
 * Provides implementation for Insert::get_reference.
17
 */
18
trait CachesReferences {
19
    /**
20
     * This contains cached reference ids.
21
     *
22
     * @var array|null   string => int 
23
     */
24
    protected $reference_cache = array(); 
25
26
    /**
27
     * Get the id of a reference by either inserting a new reference or reading
28
     * it from the cache.
29
     *
30
     * The implementation should assure that each combination of $type, $name,
31
     * $file and $line is only inserted once.
32
     *
33
     * @param   int             $type
34
     * @param   string          $name
35
     * @param   string          $file       where the entity was referenced
36
     * @param   int             $line       where the entity was referenced
37
     * @return  int                         id of new reference
38
     */
39 16
    public function get_reference($type, $name, $file, $line) {
40 16
        assert('\\Lechimp\\Dicto\\Variables\\Variable::is_type($type)');
41 16
        assert('is_string($name)');
42 16
        assert('is_string($file)');
43 16
        assert('is_int($line)');
44
45
        // caching
46 16
        $key = $type.":".$name.":".$file.":".$line;
47 16
        if (array_key_exists($key, $this->reference_cache)) {
48 13
            return $this->reference_cache[$key];
49
        }
50
51 16
        $ref_id = $this->reference
52 16
            ( $type 
53 16
            , $name 
54 16
            , $file
55 16
            , $line
56 16
            );
57
58 16
        $this->reference_cache[$key] = $ref_id;
59 16
        return $ref_id;
60
61
    }
62
63
    /**
64
     * Record general info about a reference to an entity.
65
     *
66
     * A reference to an entity, buildin or global, i.e. a place where we know there
67
     * should be something we are refering to by name, but can not get hold of the
68
     * source, i.e. a function in a function call or the usage of a global. There
69
     * might be the possibility to dereference the reference to an entity later.
70
     *
71
     * Uses the same range for ids than entity, that is, each id either referers to
72
     * a entity or a reference.
73
     *
74
     * @param   int             $type
75
     * @param   string          $name
76
     * @param   string          $file       where the entity was referenced
77
     * @param   int             $line       where the entity was referenced
78
     * @return  int                         id of new reference
79
     */
80
    abstract public function reference($type, $name, $file, $line);
81
}
82