Passed
Push — master ( 707ba4...19aa6f )
by Sebastian
02:43
created

Mailcode_Parser_Statement::getSourceCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Statement} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Statement
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\OperationResult;
15
16
/**
17
 * Mailcode statement parser: parses arbitrary statements
18
 * to check for validation issues.
19
 *
20
 * @package Mailcode
21
 * @subpackage Parser
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Mailcode_Parser_Statement
25
{
26
    const VALIDATION_EMPTY = 48801;
27
    const VALIDATION_UNQUOTED_STRING_LITERALS = 48802;
28
    
29
   /**
30
    * @var string
31
    */
32
    protected $statement;
33
    
34
   /**
35
    * @var OperationResult
36
    */
37
    protected $result;
38
    
39
   /**
40
    * @var Mailcode_Parser_Statement_Tokenizer
41
    */
42
    protected $tokenizer;
43
    
44
   /**
45
    * @var Mailcode_Parser_Statement_Info|NULL
46
    */
47
    protected $info;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $freeform = false;
53
54
    /**
55
     * @var Mailcode_Commands_Command|null
56
     */
57
    private $sourceCommand;
58
59
    public function __construct(string $statement, bool $freeform=false, ?Mailcode_Commands_Command $sourceCommand=null)
60
    {
61
        $this->sourceCommand = $sourceCommand;
62
        $this->statement = $statement;
63
        $this->result = new OperationResult($this);
64
        $this->tokenizer = new Mailcode_Parser_Statement_Tokenizer($this);
65
        $this->freeform = $freeform;
66
67
        $this->validate();
68
    }
69
70
    public function getSourceCommand() : ?Mailcode_Commands_Command
71
    {
72
        return $this->sourceCommand;
73
    }
74
75
    public function getStatementString() : string
76
    {
77
        return $this->statement;
78
    }
79
    
80
    public function isValid() : bool
81
    {
82
        $this->validate();
83
84
        return $this->result->isValid();
85
    }
86
    
87
    public function getValidationResult() : OperationResult
88
    {
89
        return $this->result;
90
    }
91
    
92
    public function getInfo() : Mailcode_Parser_Statement_Info
93
    {
94
        if($this->info instanceof Mailcode_Parser_Statement_Info)
95
        {
96
            return $this->info; 
97
        }
98
        
99
        $this->info = new Mailcode_Parser_Statement_Info($this->tokenizer);
100
        
101
        return $this->info;
102
    }
103
    
104
    protected function validate() : void
105
    {
106
        if($this->freeform)
107
        {
108
            return;
109
        }
110
111
        if(!$this->tokenizer->hasTokens())
112
        {
113
            $this->result->makeError(
114
                t('Empty statement'),
115
                self::VALIDATION_EMPTY
116
            );
117
            
118
            return;
119
        }
120
        
121
        $unknown = $this->tokenizer->getFirstUnknown();
122
        
123
        if($unknown)
124
        {
125
            $this->result->makeError(
126
               t('Unquoted string literal found:').' ('.htmlspecialchars($unknown->getMatchedText()).')',
127
                self::VALIDATION_UNQUOTED_STRING_LITERALS
128
            );
129
        }
130
    }
131
    
132
    public function getNormalized() : string
133
    {
134
        return $this->tokenizer->getNormalized();
135
    }
136
}
137