Failed Conditions
Push — psr2 ( ffc2cc...de2261 )
by Andreas
05:28
created

StateStack   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCurrent() 0 4 1
A enter() 0 4 1
A leave() 0 8 2
1
<?php
2
/**
3
 * Lexer adapted from Simple Test: http://sourceforge.net/projects/simpletest/
4
 * For an intro to the Lexer see:
5
 * https://web.archive.org/web/20120125041816/http://www.phppatterns.com/docs/develop/simple_test_lexer_notes
6
 *
7
 * @author Marcus Baker http://www.lastcraft.com
8
 */
9
10
namespace dokuwiki\Lexer;
11
12
/**
13
 * States for a stack machine.
14
 */
15
class StateStack
16
{
17
    protected $stack;
18
19
    /**
20
     * Constructor. Starts in named state.
21
     * @param string $start        Starting state name.
22
     */
23
    public function __construct($start)
24
    {
25
        $this->stack = array($start);
26
    }
27
28
    /**
29
     * Accessor for current state.
30
     * @return string       State.
31
     */
32
    public function getCurrent()
33
    {
34
        return $this->stack[count($this->stack) - 1];
35
    }
36
37
    /**
38
     * Adds a state to the stack and sets it to be the current state.
39
     *
40
     * @param string $state        New state.
41
     */
42
    public function enter($state)
43
    {
44
        array_push($this->stack, $state);
45
    }
46
47
    /**
48
     * Leaves the current state and reverts
49
     * to the previous one.
50
     * @return boolean    False if we drop off the bottom of the list.
51
     */
52
    public function leave()
53
    {
54
        if (count($this->stack) == 1) {
55
            return false;
56
        }
57
        array_pop($this->stack);
58
        return true;
59
    }
60
}
61