Skip to content

Commit 1918caa

Browse files
committed
Added method for getting the last line in de code store.
1 parent 269e14b commit 1918caa

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/CodeStore.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ public function getCode()
214214
return implode(PHP_EOL, $lines).PHP_EOL;
215215
}
216216

217+
//--------------------------------------------------------------------------------------------------------------------
218+
/**
219+
* Returns the last line of code (without indentation applied).
220+
*
221+
* @return string
222+
*
223+
* @since 1.1.0
224+
* @api
225+
*/
226+
public function getLastLine()
227+
{
228+
if (empty($this->lines))
229+
{
230+
throw new \LogicException('No code in code store');
231+
}
232+
233+
return $this->lines[count($this->lines) - 1];
234+
}
235+
217236
//--------------------------------------------------------------------------------------------------------------------
218237
/**
219238
* Returns the raw code without indentation as an array of strings.

test/CodeStoreTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,32 @@ public function testTrim()
286286
$this->assertEquals($expected, $code);
287287
}
288288

289+
//--------------------------------------------------------------------------------------------------------------------
290+
/**
291+
* Test getLastLine.
292+
*/
293+
public function testGetLastLine1()
294+
{
295+
$store = new ConcreteCodeStore();
296+
$store->append('begin');
297+
$store->append('statement1');
298+
$store->append('statement2');
299+
300+
$this->assertEquals('statement2', $store->getLastLine());
301+
}
302+
303+
//--------------------------------------------------------------------------------------------------------------------
304+
/**
305+
* Test getLastLine with empty code store.
306+
*
307+
* @expectedException \LogicException
308+
*/
309+
public function testGetLastLine2()
310+
{
311+
$store = new ConcreteCodeStore();
312+
$store->getLastLine();
313+
}
314+
289315
//--------------------------------------------------------------------------------------------------------------------
290316
}
291317

0 commit comments

Comments
 (0)