Skip to content

Commit 7ba2a44

Browse files
committed
Added heredoc support.
1 parent 42d248b commit 7ba2a44

3 files changed

Lines changed: 50 additions & 7 deletions

File tree

src/CodeStore.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ abstract class CodeStore
4949
*/
5050
const C_INDENT_DECREMENT_AFTER = 8;
5151

52+
/**
53+
* No indentation, heredoc.
54+
*
55+
* @since 1.0.0
56+
* @api
57+
*/
58+
const C_INDENT_HEREDOC = 32;
59+
5260
/**
5361
* String for separating parts of the generated code. In most cases a comment with one character repeated many times.
5462
*
@@ -206,8 +214,15 @@ public function getCode(): string
206214
$line = $this->shortenSeparator($this->width - $this->indentation * $indentLevel);
207215
}
208216

209-
// Append the line with indentation.
210-
$lines[] = $this->addIndentation($line, $indentLevel);
217+
if ($mode & self::C_INDENT_HEREDOC)
218+
{
219+
$lines[] = $line;
220+
}
221+
else
222+
{
223+
// Append the line with indentation.
224+
$lines[] = $this->addIndentation($line, $indentLevel);
225+
}
211226

212227
// Increment or decrement indentation level.
213228
if ($mode & self::C_INDENT_INCREMENT_AFTER)

test/CodeStoreTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ public function testIndentationLevels1(): void
136136
'statement',
137137
'middle',
138138
'statement',
139+
'heredoc-start',
140+
'line1',
141+
'line2',
142+
'line3',
143+
'heredoc-end',
139144
'statement',
140145
'end',
141146
'begin',
@@ -162,6 +167,11 @@ public function testIndentationLevels1(): void
162167
statement
163168
middle
164169
statement
170+
heredoc-start
171+
line1
172+
line2
173+
line3
174+
heredoc-end
165175
statement
166176
end
167177
begin

test/ConcreteCodeStore.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,25 @@
1010
*/
1111
class ConcreteCodeStore extends CodeStore
1212
{
13+
//--------------------------------------------------------------------------------------------------------------------
14+
/**
15+
* If true the current lines are part of a heredoc.
16+
*
17+
* @var bool
18+
*/
19+
private $isHeredoc = false;
20+
1321
//--------------------------------------------------------------------------------------------------------------------
1422
/**
1523
* {@inheritdoc}
1624
*/
17-
public function __construct(int $indentation=2, int $width=15)
25+
public function __construct(int $indentation = 2, int $width = 15)
1826
{
1927
parent::__construct($indentation, $width);
20-
28+
2129
$this->separator = '#'.str_repeat('-', $width - 1);
2230
}
23-
31+
2432
//--------------------------------------------------------------------------------------------------------------------
2533
/**
2634
* {@inheritdoc}
@@ -50,11 +58,21 @@ protected function indentationMode(string $line): int
5058
case 'end-end':
5159
return self::C_INDENT_DECREMENT_BEFORE_DOUBLE;
5260

53-
default:
61+
case 'heredoc-start':
62+
$this->isHeredoc = true;
63+
5464
return 0;
65+
66+
case 'heredoc-end':
67+
$this->isHeredoc = false;
68+
69+
return self::C_INDENT_HEREDOC;
70+
71+
default:
72+
return ($this->isHeredoc===false) ? 0 : self::C_INDENT_HEREDOC;
5573
}
5674
}
57-
75+
5876
//--------------------------------------------------------------------------------------------------------------------
5977
}
6078

0 commit comments

Comments
 (0)