Passed
Push — master ( 7c0b30...83b5cd )
by Sebastian
02:33
created

Mailcode_Parser_Statement_Info_Pruner::getTokens()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Statement_Info_Pruner} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Statement_Info_Pruner
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Statement pruning utility: allows applying rules to
16
 * an existing statement's tokens to retrieve only the
17
 * relevant tokens.
18
 *
19
 * @package Mailcode
20
 * @subpackage Parser
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Parser_Statement_Info_Pruner
24
{
25
   /**
26
    * @var Mailcode_Parser_Statement_Info
27
    */
28
    private $info;
29
    
30
   /**
31
    * @var string[]
32
    */
33
    private $exclude = array();
34
    
35
   /**
36
    * @var string[]
37
    */
38
    private $classNames = array();
39
    
40
    public function __construct(Mailcode_Parser_Statement_Info $info)
41
    {
42
        $this->info = $info;
43
    }
44
    
45
    public function excludeToken(Mailcode_Parser_Statement_Tokenizer_Token $token) : Mailcode_Parser_Statement_Info_Pruner
46
    {
47
        $id = $token->getID();
48
        
49
        if(!in_array($id, $this->exclude))
50
        {
51
            $this->exclude[] = $id;
52
        }
53
        
54
        return $this;
55
    }
56
    
57
    public function limitToStringLiterals() : Mailcode_Parser_Statement_Info_Pruner
58
    {
59
        return $this->limitByClassName(Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral::class);
60
    }
61
62
    public function limitToNumbers() : Mailcode_Parser_Statement_Info_Pruner
63
    {
64
        return $this->limitByClassName(Mailcode_Parser_Statement_Tokenizer_Token_Number::class);
65
    }
66
    
67
    public function limitByClassName(string $className) : Mailcode_Parser_Statement_Info_Pruner
68
    {
69
        if(!in_array($className, $this->classNames))
70
        {
71
            $this->classNames[] = $className;
72
        }
73
        
74
        return $this;
75
    }
76
    
77
   /**
78
    * Retrieves all tokens in the statement matching the
79
    * current criteria.
80
    * 
81
    * @return Mailcode_Parser_Statement_Tokenizer_Token[]
82
    */
83
    public function getTokens() : array
84
    {
85
        $tokens = $this->info->getTokens();
86
        $keep = array(); 
87
        
88
        foreach($tokens as $token)
89
        {
90
            if(in_array($token->getID(), $this->exclude))
91
            {
92
                continue;
93
            }
94
            
95
            $keep[] = $token;
96
        }
97
        
98
        return $keep;
99
    }
100
}
101