Completed
Push — master ( cc2765...340ff8 )
by Richard
08:19
created

SymbolTable   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A symbols() 0 5 2
A add_symbol() 0 8 2
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 *
5
 * Copyright (c) 2016 Richard Klees <[email protected]>
6
 * 
7
 * This software is licensed under The MIT License. You should have received
8
 * a copy of the license along with the code.
9
 */
10
11
namespace Lechimp\Dicto\Definition;
12
13
/**
14
 * The symbol table knows all symbols we could construct.
15
 */
16
class SymbolTable {
17
    /**
18
     * @var Symbol[]
19
     */
20
    protected $symbols = array();
21
22
    /**
23
     * Generator over the symbols the SymbolTable knows.
24
     *
25
     * @return Generator
26
     */
27 37
    public function symbols() {
28 37
        foreach($this->symbols as $symbol) {
29 36
            yield $symbol;
30 36
        }
31 2
    }
32
33
    /**
34
     * Add a symbol to the table.
35
     *
36
     * @param   string  $regexp
37
     * @param   int     $binding_power
38
     * @throws  \InvalidArgumentException if %$regexp% is not a regexp
39
     * @throws  \LogicException if there already is a symbol with that $regexp.
40
     * @return  Symbol
41
     */
42 39
    public function add_symbol($regexp, $binding_power = 0) {
43 39
        if (array_key_exists($regexp, $this->symbols)) {
44 1
            throw new \LogicException("Symbol for regexp $regexp already exists.");
45
        }
46 39
        $s = new Symbol($regexp, $binding_power);
47 39
        $this->symbols[$regexp] = $s;
48 39
        return $s;
49
    }
50
}
51
52