PHP::inlineComment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of PHP-Yacc package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace PhpYacc\CodeGen\Language;
11
12
use PhpYacc\CodeGen\Language;
13
14
/**
15
 * Class PHP.
16
 */
17
class PHP implements Language
18
{
19
    /**
20
     * @var resource
21
     */
22
    protected $fp;
23
24
    /**
25
     * @var resource
26
     */
27
    protected $hp;
28
29
    /**
30
     * @var string
31
     */
32
    protected $fileBuffer = '';
33
34
    /**
35
     * @var string
36
     */
37
    protected $headerBuffer = '';
38
39
    /**
40
     * @param $file
41
     * @param $headerFile
42
     */
43
    public function begin($file, $headerFile)
44
    {
45
        $this->fp = $file;
46
        $this->hp = $headerFile;
47
        $this->fileBuffer = '';
48
        $this->headerBuffer = '';
49
    }
50
51
    /**
52
     * @return void
53
     */
54
    public function commit()
55
    {
56
        \fwrite($this->fp, $this->fileBuffer);
57
        \fwrite($this->hp, $this->headerBuffer);
58
        $this->fp = $this->hp = null;
59
        $this->fileBuffer = '';
60
        $this->headerBuffer = '';
61
    }
62
63
    /**
64
     * @param string $text
65
     */
66
    public function inlineComment(string $text)
67
    {
68
        $this->fileBuffer .= sprintf('/* %s */', $text);
69
    }
70
71
    /**
72
     * @param string $text
73
     */
74
    public function comment(string $text)
75
    {
76
        $this->fileBuffer .= sprintf("// %s\n", $text);
77
    }
78
79
    /**
80
     * @param string $indent
81
     * @param int    $num
82
     * @param string $value
83
     */
84
    public function caseBlock(string $indent, int $num, string $value)
85
    {
86
        $this->fileBuffer .= \sprintf(
87
            "%scase %d: return %s;\n", $indent, $num, \var_export($value, true)
88
        );
89
    }
90
91
    /**
92
     * @param string $text
93
     * @param bool   $includeHeader
94
     */
95
    public function write(string $text, bool $includeHeader = false)
96
    {
97
        $this->fileBuffer .= $text;
98
        if ($includeHeader) {
99
            $this->headerBuffer .= $text;
100
        }
101
    }
102
103
    /**
104
     * @param string $text
105
     */
106
    public function writeQuoted(string $text)
107
    {
108
        $buf = [];
109
        for ($i = 0; $i < \mb_strlen($text); $i++) {
110
            $char = $text[$i];
111
112
            if ($char == '\\' || $char == '"' || $char == '$') {
113
                $buf[] = '\\';
114
            }
115
116
            $buf[] = $char;
117
        }
118
119
        $this->fileBuffer .= implode('', $buf);
120
    }
121
}
122