Skip to content

Commit b63d5e8

Browse files
authored
Update README.md
1 parent 4c89a26 commit b63d5e8

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ composer require permafrost-dev/code-snippets
2121

2222
## Usage
2323

24+
### Creating a snippet
25+
2426
Use the `surroundingLine($num)` method to select the "target" line, which will be returned as the middle line of the snippet:
2527

2628
```php
@@ -55,6 +57,7 @@ $snippet = (new CodeSnippet())
5557
->linesAfter(3)
5658
->fromFile('/path/to/a/file.php');
5759
```
60+
### Getting the snippet contents
5861

5962
The `getLines()` method returns an array of `SnippetLine` instances. The keys of the resulting array are the line numbers.
6063

@@ -80,6 +83,33 @@ foreach($snippet->getLines() as $lineNum => $line) {
8083
}
8184
```
8285

86+
### Snippet line count
87+
88+
To determine the number of lines in the snippet, use the `getSnippetLineCount()` method:
89+
90+
```php
91+
$snippet = (new CodeSnippet())
92+
->surroundingLines(4, 7)
93+
->linesBefore(3)
94+
->linesAfter(3)
95+
->fromFile('/path/to/a/file.php');
96+
97+
echo "Snippet line count: " . $snippet->getSnippetLineCount() . PHP_EOL;
98+
```
99+
100+
You can also use `count()` on the result of the `getLines()` method:
101+
102+
```php
103+
$snippet = (new CodeSnippet())
104+
->surroundingLines(4, 7)
105+
->linesBefore(3)
106+
->linesAfter(3)
107+
->fromFile('/path/to/a/file.php');
108+
109+
echo "Snippet line count: " . count($snippet->getLines()) . PHP_EOL;
110+
```
111+
112+
83113
## Testing
84114

85115
```bash

0 commit comments

Comments
 (0)