Test Failed
Push — master ( f091df...afe914 )
by Sebastian
03:05
created

limitToOperands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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 limitToOperands() : Mailcode_Parser_Statement_Info_Pruner
63
    {
64
        return $this->limitByClassName(Mailcode_Parser_Statement_Tokenizer_Token_Operand::class);
65
    }
66
67
    public function limitToNumbers() : Mailcode_Parser_Statement_Info_Pruner
68
    {
69
        return $this->limitByClassName(Mailcode_Parser_Statement_Tokenizer_Token_Number::class);
70
    }
71
    
72
    public function limitByClassName(string $className) : Mailcode_Parser_Statement_Info_Pruner
73
    {
74
        if(!in_array($className, $this->classNames))
75
        {
76
            $this->classNames[] = $className;
77
        }
78
        
79
        return $this;
80
    }
81
    
82
   /**
83
    * Retrieves all tokens in the statement matching the
84
    * current criteria.
85
    * 
86
    * @return Mailcode_Parser_Statement_Tokenizer_Token[]
87
    */
88
    public function getTokens() : array
89
    {
90
        $tokens = $this->info->getTokens();
91
        $keep = array(); 
92
        
93
        foreach($tokens as $token)
94
        {
95
            if(in_array($token->getID(), $this->exclude))
96
            {
97
                continue;
98
            }
99
            
100
            $keep[] = $token;
101
        }
102
        
103
        return $keep;
104
    }
105
}
106