Skip to content

Commit

Permalink
Merge pull request #3 from permafrost-dev/add-get-line-numbers-method
Browse files Browse the repository at this point in the history
Add helper methods
  • Loading branch information
patinthehat authored Jul 27, 2021
2 parents 52ce7bc + 9cb3605 commit 639827b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

All notable changes to `code-snippets` will be documented in this file.

## 1.1.0 - 2021-07-25
## 1.2.0 - 2021-07-27

- add `getLineNumbers()` method
- add `getSelectedBounds()` method
- add `Bounds::size()` method

## 1.1.0 - 2021-07-27

- add `toString()` and `__toString()` methods

Expand Down
5 changes: 5 additions & 0 deletions src/Bounds.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public function copy(self $bounds): self

return $this;
}

public function size(): int
{
return count(range($this->start, $this->end));
}
}
10 changes: 10 additions & 0 deletions src/CodeSnippet.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,16 @@ public function getLineNumberEnd(): int
return $this->surroundingLines[count($this->surroundingLines) - 1] ?? 0;
}

public function getLineNumbers(): array
{
return array_keys($this->getLines());
}

public function getSelectedBounds(): Bounds
{
return Bounds::create($this->getLineNumberStart(), $this->getLineNumberEnd());
}

public function toString()
{
$result = '';
Expand Down
23 changes: 23 additions & 0 deletions tests/CodeSnippetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,27 @@ public function it_returns_a_string_when_the_snippet_is_cast_to_a_string()

$this->assertMatchesSnapshot((string)$snippet);
}

/** @test */
public function it_returns_the_line_numbers()
{
$snippet = (new CodeSnippet())
->surroundingLines(10, 12)
->snippetLineCount(8)
->fromFile($this->testsPath('data/file3.php'));

$this->assertCount(8, $snippet->getLineNumbers());
$this->assertMatchesSnapshot($snippet->getLineNumbers());
}

/** @test */
public function it_returns_the_selected_bounds()
{
$snippet = (new CodeSnippet())
->surroundingLines(10, 12)
->snippetLineCount(8)
->fromFile($this->testsPath('data/file3.php'));

$this->assertEquals([10, 11, 12], range($snippet->getSelectedBounds()->start, $snippet->getSelectedBounds()->end));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14

0 comments on commit 639827b

Please sign in to comment.