Passed
Push — master ( 2f714f...1c67eb )
by Sebastian
02:15
created

Mailcode_Parser_Statement::tokenize_escaped_quotes()   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 1
dl 0
loc 3
rs 10
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 ERROR_TOKENIZE_METHOD_MISSING = 48901;
27
    
28
    const VALIDATION_EMPTY = 48801;
29
    
30
    const VALIDATION_UNQUOTED_STRING_LITERALS = 48802;
31
    
32
   /**
33
    * @var string
34
    */
35
    protected $statement;
36
    
37
   /**
38
    * @var OperationResult
39
    */
40
    protected $result;
41
    
42
   /**
43
    * @var Mailcode_Parser_Statement_Tokenizer|NULL
44
    */
45
    protected $tokenizer;
46
    
47
   /**
48
    * @var Mailcode_Parser_Statement_Info|NULL
49
    */
50
    protected $info;
51
    
52
    public function __construct(string $statement)
53
    {
54
        $this->statement = $statement;
55
        $this->result = new OperationResult($this);
56
        $this->tokenizer = new Mailcode_Parser_Statement_Tokenizer($this);
57
        
58
        $this->validate();
59
    }
60
    
61
    public function getStatementString() : string
62
    {
63
        return $this->statement;
64
    }
65
    
66
    public function isValid() : bool
67
    {
68
        return $this->result->isValid();
69
    }
70
    
71
    public function getValidationResult() : OperationResult
72
    {
73
        return $this->result;
74
    }
75
    
76
    public function getInfo() : Mailcode_Parser_Statement_Info
77
    {
78
        if(isset($this->info))
79
        {
80
            return $this->info; 
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->info could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Parser_Statement_Info. Consider adding an additional type-check to rule them out.
Loading history...
81
        }
82
        
83
        $this->info = new Mailcode_Parser_Statement_Info($this->tokenizer);
0 ignored issues
show
Bug introduced by
It seems like $this->tokenizer can also be of type null; however, parameter $tokenizer of Mailcode\Mailcode_Parser...ent_Info::__construct() does only seem to accept Mailcode\Mailcode_Parser_Statement_Tokenizer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        $this->info = new Mailcode_Parser_Statement_Info(/** @scrutinizer ignore-type */ $this->tokenizer);
Loading history...
84
        
85
        return $this->info;
86
    }
87
    
88
    protected function validate() : void
89
    {
90
        if(!$this->tokenizer->hasTokens())
0 ignored issues
show
Bug introduced by
The method hasTokens() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        if(!$this->tokenizer->/** @scrutinizer ignore-call */ hasTokens())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
91
        {
92
            $this->result->makeError(
93
                t('Empty statement'),
94
                self::VALIDATION_EMPTY
95
            );
96
            
97
            return;
98
        }
99
        
100
        $unknown = $this->tokenizer->getFirstUnknown();
101
        
102
        if($unknown)
103
        {
104
            $this->result->makeError(
105
               t('Unquoted string literal found:').' ('.$unknown->getMatchedText().')',
106
                self::VALIDATION_UNQUOTED_STRING_LITERALS
107
            );
108
        }
109
    }
110
    
111
    public function getNormalized() : string
112
    {
113
        return $this->tokenizer->getNormalized();
114
    }
115
}
116