PHPackages                             permafrost-dev/code-snippets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. permafrost-dev/code-snippets

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

permafrost-dev/code-snippets
============================

Easily work with code snippets in PHP

1.2.0(4y ago)14403.5k↓37.5%[1 PRs](https://github.com/permafrost-dev/code-snippets/pulls)2MITPHPPHP ^7.3|^8.0CI passing

Since Jul 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/permafrost-dev/code-snippets)[ Packagist](https://packagist.org/packages/permafrost-dev/code-snippets)[ Docs](https://github.com/permafrost-dev/code-snippets)[ Fund](https://permafrost.dev/open-source)[ GitHub Sponsors](https://github.com/permafrost-dev)[ RSS](/packages/permafrost-dev-code-snippets/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (7)Used By (2)

code-snippets
=============

[](#code-snippets)

 [![Package Version](https://camo.githubusercontent.com/b3268555db3fd8af16108b7aa062683b91395a42810cf82571e34bc25b6f9eca/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7065726d6166726f73742d6465762f636f64652d736e6970706574732e7376673f736f72743d73656d766572266c6f676f3d676974687562)](https://camo.githubusercontent.com/b3268555db3fd8af16108b7aa062683b91395a42810cf82571e34bc25b6f9eca/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7065726d6166726f73742d6465762f636f64652d736e6970706574732e7376673f736f72743d73656d766572266c6f676f3d676974687562) [![license](https://camo.githubusercontent.com/deca50eccb8bb783caed280cf372b4553d5384a5d627bef16a3288caaf929019/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7065726d6166726f73742d6465762f636f64652d736e6970706574732e7376673f6c6f676f3d6f70656e736f75726365696e697469617469766526)](https://camo.githubusercontent.com/deca50eccb8bb783caed280cf372b4553d5384a5d627bef16a3288caaf929019/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7065726d6166726f73742d6465762f636f64652d736e6970706574732e7376673f6c6f676f3d6f70656e736f75726365696e697469617469766526) [![Test Run Status](https://github.com/permafrost-dev/code-snippets/actions/workflows/run-tests.yml/badge.svg?branch=main&)](https://github.com/permafrost-dev/code-snippets/actions/workflows/run-tests.yml/badge.svg?branch=main&) [![code coverage](https://camo.githubusercontent.com/53c99915eb0d1614c2cc583683ccb143b624907743668e48706af66baa9e395c/68747470733a2f2f636f6465636f762e696f2f67682f7065726d6166726f73742d6465762f636f64652d736e6970706574732f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://camo.githubusercontent.com/53c99915eb0d1614c2cc583683ccb143b624907743668e48706af66baa9e395c/68747470733a2f2f636f6465636f762e696f2f67682f7065726d6166726f73742d6465762f636f64652d736e6970706574732f6272616e63682f6d61696e2f67726170682f62616467652e737667)

Easily create and work with code snippets from source code files of any type in PHP.

*The original code this package is based on was borrowed from the [`spatie/backtrace`](https://github.com/spatie/backtrace) package.*

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require permafrost-dev/code-snippets
```

Usage
-----

[](#usage)

*Note: Although the examples here reference php files, any file type can be used when creating a `CodeSnippet`.*

### Creating a snippet

[](#creating-a-snippet)

Use the `surroundingLine($num)` method to select the "target" line, which will be returned as the middle line of the snippet:

```
use Permafrost\CodeSnippets\CodeSnippet;

$snippet = (new CodeSnippet())
    ->surroundingLine(4)
    ->snippetLineCount(6)
    ->fromFile('/path/to/a/file.php');
```

Use the `surroundingLines($first, $last)` method to select a range of "target" lines, which will be returned as the middle lines of the snippet:

```
$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->snippetLineCount(6)
    ->fromFile('/path/to/a/file.php');
```

Use the `linesBefore()` and `linesAfter()` methods to specify the number of context lines to display before and after the "target" lines:

```
// the "target" line isn't displayed in the middle, but as the second line
$snippet = (new CodeSnippet())
    ->surroundingLine(4)
    ->linesBefore(1)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');
```

### Getting the snippet contents

[](#getting-the-snippet-contents)

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

The `SnippetLine` instances may be cast to strings to display the value. When working with `SnippetLine` instances, use `isSelected()` to determine if the line was selected using either the `surroundingLine()` or `surroundingLines()` method on the `CodeSnippet` instance.

To get the value of a `SnippetLine`, use the `value()` method or cast the object to a string.

```
$snippet = (new CodeSnippet())
    ->surroundingLine(4)
    ->snippetLineCount(5)
    ->fromFile('/path/to/a/file.php');

foreach($snippet->getLines() as $lineNum => $line) {
    $prefix = $line->isSelected() ? ' * ' : '   ';

    echo $prefix . $line->lineNumber() . ' - ' . $line->value() . PHP_EOL;
}
```

### Snippet line count

[](#snippet-line-count)

To determine the number of lines in the snippet, use the `getSnippetLineCount()` method:

```
$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->linesBefore(3)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');

echo "Snippet line count: " . $snippet->getSnippetLineCount() . PHP_EOL;
```

You can also use `count()` on the result of the `getLines()` method:

```
$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->linesBefore(3)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');

echo "Snippet line count: " . count($snippet->getLines()) . PHP_EOL;
```

To return an array containing the line numbers for the snippet, use `getLineNumbers()`:

```
print_r($snippet->getLineNumbers());
```

### Returning the snippet as a string

[](#returning-the-snippet-as-a-string)

Return the contents of the snippet as as string using the `toString()` method or by casting the snippet to a string directly:

```
$snippet = (new CodeSnippet())
    ->surroundingLines(4, 7)
    ->linesBefore(3)
    ->linesAfter(3)
    ->fromFile('/path/to/a/file.php');

echo "Snippet: \n" . $snippet->toString() . PHP_EOL;
```

Testing
-------

[](#testing)

```
./vendor/bin/phpunit
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Patrick Organ](https://github.com/patinthehat)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance30

Infrequent updates — may be unmaintained

Popularity42

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 95.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

5

Last Release

1757d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/de8dcb8e8a47ddc1076a26721d1ce7b17e3066b53c4a5c5e9aa31367c1955a20?d=identicon)[permafrost-dev](/maintainers/permafrost-dev)

---

Top Contributors

[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (80 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (4 commits)")

---

Tags

code-snippetspermafrostsnippetscodesnippetspermafrostcode-snippets

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/permafrost-dev-code-snippets/health.svg)

```
[![Health](https://phpackages.com/badges/permafrost-dev-code-snippets/health.svg)](https://phpackages.com/packages/permafrost-dev-code-snippets)
```

###  Alternatives

[endroid/qr-code

Endroid QR Code

4.8k67.6M348](/packages/endroid-qr-code)[laminas/laminas-code

Extensions to the PHP Reflection API, static code scanning, and code generation

1.9k185.4M172](/packages/laminas-laminas-code)[nette/php-generator

🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.5 features.

2.3k64.2M576](/packages/nette-php-generator)[jetbrains/phpstorm-stubs

PHP runtime &amp; extensions header files for PhpStorm

1.4k27.7M68](/packages/jetbrains-phpstorm-stubs)[scrivo/highlight.php

Server side syntax highlighter that supports 185 languages. It's a PHP port of highlight.js

71140.3M82](/packages/scrivo-highlightphp)[riimu/kit-phpencoder

Highly customizable alternative to var\_export for PHP code generation

717.8M32](/packages/riimu-kit-phpencoder)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
