Skip to content

Commit 1cdf76f

Browse files
committed
Added support for heredoc.
1 parent 954f8dc commit 1cdf76f

3 files changed

Lines changed: 107 additions & 1 deletion

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "MIT",
1111
"require": {
1212
"php": ">=7.1.0",
13-
"setbased/helper-code-store": "^2.1.0"
13+
"setbased/helper-code-store": "^2.2.0"
1414
},
1515
"require-dev": {
1616
"phpunit/phpunit": "^7.0.0 || ^8.0.0",

src/PhpCodeStore.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ class PhpCodeStore extends CodeStore
1616
*/
1717
private $defaultLevel = [];
1818

19+
/**
20+
* The heredoc identifier.
21+
*
22+
* @var string|null
23+
*/
24+
private $heredocIdentifier;
25+
1926
//--------------------------------------------------------------------------------------------------------------------
2027
/**
2128
* Object constructor.
@@ -42,6 +49,8 @@ protected function indentationMode(string $line): int
4249
$line = trim($line);
4350

4451
$mode = 0;
52+
53+
$mode |= $this->indentationModeHeredoc($line);
4554
$mode |= $this->indentationModeSwitch($line);
4655
$mode |= $this->indentationModeBLock($line);
4756

@@ -93,6 +102,8 @@ private function indentationModeBlock(string $line): int
93102
{
94103
$mode = 0;
95104

105+
if ($this->heredocIdentifier!==null) return $mode;
106+
96107
if (substr($line, -1, 1)=='{')
97108
{
98109
$mode |= self::C_INDENT_INCREMENT_AFTER;
@@ -119,6 +130,38 @@ private function indentationModeBlock(string $line): int
119130
return $mode;
120131
}
121132

133+
//--------------------------------------------------------------------------------------------------------------------
134+
/**
135+
*
136+
* @param string $line The line of code.
137+
*
138+
* @return int
139+
*/
140+
private function indentationModeHeredoc(string $line): int
141+
{
142+
$mode = 0;
143+
144+
if ($this->heredocIdentifier!==null)
145+
{
146+
$mode |= self::C_INDENT_HEREDOC;
147+
148+
if ($line==$this->heredocIdentifier.';')
149+
{
150+
$this->heredocIdentifier = null;
151+
}
152+
}
153+
else
154+
{
155+
$n = preg_match('/=\s*<<<\s*([A-Z]+)$/', $line, $parts);
156+
if ($n==1)
157+
{
158+
$this->heredocIdentifier = $parts[1];
159+
}
160+
}
161+
162+
return $mode;
163+
}
164+
122165
//--------------------------------------------------------------------------------------------------------------------
123166
/**
124167
* Returns the indentation mode based on a line of code for switch statements.
@@ -131,6 +174,8 @@ private function indentationModeSwitch(string $line): int
131174
{
132175
$mode = 0;
133176

177+
if ($this->heredocIdentifier!==null) return $mode;
178+
134179
if (substr($line, 0, 5)=='case ')
135180
{
136181
$mode |= self::C_INDENT_INCREMENT_AFTER;

test/CodeStoreTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,67 @@ public function testSwitch2(\$i)
170170
}
171171

172172
//--------------------------------------------------------------------------------------------------------------------
173+
/**
174+
* Test indentation levels for heredoc.
175+
*/
176+
public function testHeredoc(): void
177+
{
178+
$store = new PhpCodeStore(2, 80);
179+
180+
$store->append('<?php');
181+
$store->appendSeparator();
182+
$store->append('namespace SetBased\Helper\CodeStore\Test;');
183+
$store->append('');
184+
$store->append('use SetBased\Helper\CodeStore\PhpCodeStore;');
185+
$store->append('');
186+
$store->appendSeparator();
187+
$store->append('class PhpCodeStoreTest extends \PHPUnit_Framework_TestCase');
188+
$store->append('{');
189+
$store->appendSeparator();
190+
$store->append('public function testHeredoc()');
191+
$store->append('{');
192+
$store->append('$text = <<< EOT');
193+
$store->append('Hello, World!');
194+
$store->append('Hello, World!');
195+
$store->append('EOT;');
196+
$store->append('}');
197+
$store->append('');
198+
$store->appendSeparator();
199+
$store->append('}');
200+
$store->append('');
201+
$store->appendSeparator();
202+
203+
$expected = <<< EOL
204+
<?php
205+
//------------------------------------------------------------------------------
206+
namespace SetBased\Helper\CodeStore\Test;
207+
208+
use SetBased\Helper\CodeStore\PhpCodeStore;
209+
210+
//------------------------------------------------------------------------------
211+
class PhpCodeStoreTest extends \PHPUnit_Framework_TestCase
212+
{
213+
//----------------------------------------------------------------------------
214+
public function testHeredoc()
215+
{
216+
\$text = <<< EOT
217+
Hello, World!
218+
Hello, World!
219+
EOT;
220+
}
221+
222+
//----------------------------------------------------------------------------
223+
}
224+
225+
//------------------------------------------------------------------------------
226+
227+
EOL;
228+
229+
$code = $store->getCode();
230+
231+
self::assertEquals($expected, $code);
232+
}
233+
//--------------------------------------------------------------------------------------------------------------------
173234
}
174235

175236
//----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)