Easily create and work with code snippets from PHP

Overview

code-snippets

Package Version license Test Run Status code coverage

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 package.

Installation

You can install the package via composer:

composer require permafrost-dev/code-snippets

Usage

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

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

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

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

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

./vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Easily create WooCommerce replacement orders for your customers.
Easily create WooCommerce replacement orders for your customers.

Support Orders for WooCommerce Requires PHP: 7.0 WP requires at least: 5.7 WP tested up to: 5.7 WC requires at least: 5.6.0 WC tested up to: 5.8.0 Sta

Greyhole uses Samba to create a storage pool of all your available hard drives, and allows you to create redundant copies of the files you store.

Greyhole Greyhole is an application that uses Samba to create a storage pool of all your available hard drives (whatever their size, however they're c

WPForms coding standards are based on the WordPress Coding Standards and the PHPCompatibility Coding Standards and help create strict and high-quality code.

WPForms coding standards are based on the WordPress Coding Standards and the PHPCompatibility Coding Standards and help create strict and high-quality code.

Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Super Simple DTO Creating data transfer objects with the power of php objects. No php attributes, no reflection api, and no other under the hook work.

Testing your OpenApi documentation and your code easily.

Raven - How to test your API documentation and behavior. This library was written to allow testing OpenAPI documentation easily. It also allows verify

PHP shells that work on Linux OS, macOS, and Windows OS.
PHP shells that work on Linux OS, macOS, and Windows OS.

PHP Reverse Shell Just a little refresh on the popular PHP reverse shell script pentestmonkey/php-reverse-shell. Credits to the original author! Works

Fly50W is a new language which helps you build simple apps using more than 500k lines of code easily.

Fly50W is a new language which helps you build simple apps using more than 500k lines of code easily. Installation

Easily add logs anywhere in your Magento 2 code

Magento 2 Simple Log Easily add logs anywhere in the code (like Magento 1). Requirements Requires Magento 2 in any version. Installation Add the Log.p

Easily run code asynchronously
Easily run code asynchronously

Asynchronous and parallel PHP This library provides a small and easy wrapper around PHP's PCNTL extension. It allows running of different processes in

Comments
  • Bump codecov/codecov-action from 1 to 2.0.2

    Bump codecov/codecov-action from 1 to 2.0.2

    Bumps codecov/codecov-action from 1 to 2.0.2.

    Release notes

    Sourced from codecov/codecov-action's releases.

    v2.0.2

    Fixes

    • Underlying uploader fixes issues with tokens not being sent properly for users seeing Error!: Error: Error uploading to https://codecov.io: Error: Error uploading to Codecov: Error: Not Found
    • #432 fix: use import to destructure package.json
    • #434 fix: openpgp and asn1.js
    • #440 2.0.2 token fixes

    Dependencies

    • #420 Bump eslint from 7.30.0 to 7.31.0
    • #433 build(deps-dev): bump @​types/node from 16.3.3 to 16.4.0
    • #425 build(deps-dev): bump @​typescript-eslint/eslint-plugin from 4.28.3 to 4.28.4
    • #426 build(deps-dev): bump @​typescript-eslint/parser from 4.28.3 to 4.28.4
    • #438 Set up Dependabot for github-actions dependencies

    v2.0.1

    Fixes

    • #424 fix: Issue in building all deep dependencies

    v2.0.0

    On February 1, 2022, the v1 uploader will be full sunset and no longer function. This is due to the deprecation of the underlying bash uploader. This version uses the new uploader.

    The v2 Action downloads, verifies, and runs the Codecov binary.

    Breaking Changes

    • Multiple fields have not been transferred from the bash uploader or have been deprecated. Notably many of the functionalities and gcov_ arguments have been removed. Please check the documentation for the full list.

    Features

    • dry-run argument allows Codecov flow without uploading reports to Codecov
    • (Enterprise only) slug allows specifying the repository slug manually
    • (Enterprise only) url allows changing the upload host

    v1.5.2

    1.5.2

    Fixes

    • fix: Import version properly as string not object

    v1.5.1

    1.5.1

    Fixes

    • #320 doc: add github actions badge
    • #336 Update bash uploader to 1.0.3
    • #339 fix: Add action version

    Dependencies

    ... (truncated)

    Changelog

    Sourced from codecov/codecov-action's changelog.

    2.0.2

    Fixes

    • Underlying uploader fixes issues with tokens not being sent properly for users seeing Error!: Error: Error uploading to https://codecov.io: Error: Error uploading to Codecov: Error: Not Found
    • #440 fix: Validation ordering

    2.0.1

    Fixes

    • #424 fix: Issue in building all deep dependencies

    2.0.0

    On February 1, 2022, the v1 uploader will be full sunset and no longer function. This is due to the deprecation of the underlying bash uploader. This version uses the new uploader.

    The v2 Action downloads, verifies, and runs the Codecov binary.

    Breaking Changes

    • Multiple fields have not been transferred from the bash uploader or have been deprecated. Notably many of the functionalities and gcov_ arguments have been removed. Please check the documentation for the full list.

    Features

    • dry-run argument allows Codecov flow without uploading reports to Codecov
    • (Enterprise only) slug allows specifying the repository slug manually
    • (Enterprise only) url allows changing the upload host

    1.5.2

    Fixes

    • fix: Import version properly as string not object

    1.5.1

    Fixes

    • #320 doc: add github actions badge
    • #336 Update bash uploader to 1.0.3
    • #339 fix: Add action version

    Dependencies

    • #302 Bump @​typescript-eslint/eslint-plugin from 4.22.0 to 4.22.1
    • #303 Bump @​typescript-eslint/parser from 4.22.0 to 4.22.1
    • #304 Bump ts-jest from 26.5.5 to 26.5.6
    • #309 Bump lodash from 4.17.19 to 4.17.21
    • #310 Bump hosted-git-info from 2.8.8 to 2.8.9
    • #311 Bump @​actions/github from 4.0.0 to 5.0.0
    • #314 Bump eslint from 7.25.0 to 7.27.0
    • #315 Bump @​actions/core from 1.2.7 to 1.3.0
    • #316 Bump @​typescript-eslint/parser from 4.22.1 to 4.25.0
    • #317 Bump @​typescript-eslint/eslint-plugin from 4.22.1 to 4.25.0
    • #319 Bump jest-junit from 12.0.0 to 12.1.0
    • #321 Bump typescript from 4.2.4 to 4.3.2
    • #323 Bump ws from 7.3.1 to 7.4.6

    ... (truncated)

    Commits
    • 51d8108 Merge pull request #440 from codecov/2.0.2-token-fixes
    • 88c796d Merge pull request #438 from mmorel-35/chore/dependabot
    • 0bbb082 fix: Update validation
    • 53f686a fix: Update validation
    • 6ab08a7 Bump to 2.0.2
    • f2242e1 Merge pull request #425 from codecov/dependabot/npm_and_yarn/typescript-eslin...
    • fc2878a build(deps-dev): bump @​typescript-eslint/eslint-plugin
    • e00e953 Merge pull request #426 from codecov/dependabot/npm_and_yarn/typescript-eslin...
    • 8dcb1d2 Merge pull request #433 from codecov/dependabot/npm_and_yarn/types/node-16.4.0
    • c5857ba Merge pull request #434 from RA80533/fix/openpgp
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies github_actions 
    opened by dependabot[bot] 2
  • Bump codecov/codecov-action from 2 to 3

    Bump codecov/codecov-action from 2 to 3

    Bumps codecov/codecov-action from 2 to 3.

    Release notes

    Sourced from codecov/codecov-action's releases.

    v3.0.0

    Breaking Changes

    • #689 Bump to node16 and small fixes

    Features

    • #688 Incorporate gcov arguments for the Codecov uploader

    Dependencies

    • #548 build(deps-dev): bump jest-junit from 12.2.0 to 13.0.0
    • #603 [Snyk] Upgrade @​actions/core from 1.5.0 to 1.6.0
    • #628 build(deps): bump node-fetch from 2.6.1 to 3.1.1
    • #634 build(deps): bump node-fetch from 3.1.1 to 3.2.0
    • #636 build(deps): bump openpgp from 5.0.1 to 5.1.0
    • #652 build(deps-dev): bump @​vercel/ncc from 0.30.0 to 0.33.3
    • #653 build(deps-dev): bump @​types/node from 16.11.21 to 17.0.18
    • #659 build(deps-dev): bump @​types/jest from 27.4.0 to 27.4.1
    • #667 build(deps): bump actions/checkout from 2 to 3
    • #673 build(deps): bump node-fetch from 3.2.0 to 3.2.3
    • #683 build(deps): bump minimist from 1.2.5 to 1.2.6
    • #685 build(deps): bump @​actions/github from 5.0.0 to 5.0.1
    • #681 build(deps-dev): bump @​types/node from 17.0.18 to 17.0.23
    • #682 build(deps-dev): bump typescript from 4.5.5 to 4.6.3
    • #676 build(deps): bump @​actions/exec from 1.1.0 to 1.1.1
    • #675 build(deps): bump openpgp from 5.1.0 to 5.2.1

    v2.1.0

    2.1.0

    Features

    • #515 Allow specifying version of Codecov uploader

    Dependencies

    • #499 build(deps-dev): bump @​vercel/ncc from 0.29.0 to 0.30.0
    • #508 build(deps): bump openpgp from 5.0.0-5 to 5.0.0
    • #514 build(deps-dev): bump @​types/node from 16.6.0 to 16.9.0

    v2.0.3

    2.0.3

    Fixes

    • #464 Fix wrong link in the readme
    • #485 fix: Add override OS and linux default to platform

    Dependencies

    • #447 build(deps): bump openpgp from 5.0.0-4 to 5.0.0-5
    • #458 build(deps-dev): bump eslint from 7.31.0 to 7.32.0
    • #465 build(deps-dev): bump @​typescript-eslint/eslint-plugin from 4.28.4 to 4.29.1
    • #466 build(deps-dev): bump @​typescript-eslint/parser from 4.28.4 to 4.29.1
    • #468 build(deps-dev): bump @​types/jest from 26.0.24 to 27.0.0
    • #470 build(deps-dev): bump @​types/node from 16.4.0 to 16.6.0
    • #472 build(deps): bump path-parse from 1.0.6 to 1.0.7
    • #473 build(deps-dev): bump @​types/jest from 27.0.0 to 27.0.1

    ... (truncated)

    Changelog

    Sourced from codecov/codecov-action's changelog.

    3.0.0

    Breaking Changes

    • #689 Bump to node16 and small fixes

    Features

    • #688 Incorporate gcov arguments for the Codecov uploader

    Dependencies

    • #548 build(deps-dev): bump jest-junit from 12.2.0 to 13.0.0
    • #603 [Snyk] Upgrade @​actions/core from 1.5.0 to 1.6.0
    • #628 build(deps): bump node-fetch from 2.6.1 to 3.1.1
    • #634 build(deps): bump node-fetch from 3.1.1 to 3.2.0
    • #636 build(deps): bump openpgp from 5.0.1 to 5.1.0
    • #652 build(deps-dev): bump @​vercel/ncc from 0.30.0 to 0.33.3
    • #653 build(deps-dev): bump @​types/node from 16.11.21 to 17.0.18
    • #659 build(deps-dev): bump @​types/jest from 27.4.0 to 27.4.1
    • #667 build(deps): bump actions/checkout from 2 to 3
    • #673 build(deps): bump node-fetch from 3.2.0 to 3.2.3
    • #683 build(deps): bump minimist from 1.2.5 to 1.2.6
    • #685 build(deps): bump @​actions/github from 5.0.0 to 5.0.1
    • #681 build(deps-dev): bump @​types/node from 17.0.18 to 17.0.23
    • #682 build(deps-dev): bump typescript from 4.5.5 to 4.6.3
    • #676 build(deps): bump @​actions/exec from 1.1.0 to 1.1.1
    • #675 build(deps): bump openpgp from 5.1.0 to 5.2.1

    2.1.0

    Features

    • #515 Allow specifying version of Codecov uploader

    Dependencies

    • #499 build(deps-dev): bump @​vercel/ncc from 0.29.0 to 0.30.0
    • #508 build(deps): bump openpgp from 5.0.0-5 to 5.0.0
    • #514 build(deps-dev): bump @​types/node from 16.6.0 to 16.9.0

    2.0.3

    Fixes

    • #464 Fix wrong link in the readme
    • #485 fix: Add override OS and linux default to platform

    Dependencies

    • #447 build(deps): bump openpgp from 5.0.0-4 to 5.0.0-5
    • #458 build(deps-dev): bump eslint from 7.31.0 to 7.32.0
    • #465 build(deps-dev): bump @​typescript-eslint/eslint-plugin from 4.28.4 to 4.29.1
    • #466 build(deps-dev): bump @​typescript-eslint/parser from 4.28.4 to 4.29.1
    • #468 build(deps-dev): bump @​types/jest from 26.0.24 to 27.0.0
    • #470 build(deps-dev): bump @​types/node from 16.4.0 to 16.6.0
    • #472 build(deps): bump path-parse from 1.0.6 to 1.0.7
    • #473 build(deps-dev): bump @​types/jest from 27.0.0 to 27.0.1
    • #478 build(deps-dev): bump @​typescript-eslint/parser from 4.29.1 to 4.29.2
    • #479 build(deps-dev): bump @​typescript-eslint/eslint-plugin from 4.29.1 to 4.29.2

    ... (truncated)

    Commits
    • e3c5604 Merge pull request #689 from codecov/feat/gcov
    • 174efc5 Update package-lock.json
    • 6243a75 bump to 3.0.0
    • 0d6466f Bump to node16
    • d4729ee fetch.default
    • 351baf6 fix: bash
    • d8cf680 Merge pull request #675 from codecov/dependabot/npm_and_yarn/openpgp-5.2.1
    • b775e90 Merge pull request #676 from codecov/dependabot/npm_and_yarn/actions/exec-1.1.1
    • 2ebc2f0 Merge pull request #682 from codecov/dependabot/npm_and_yarn/typescript-4.6.3
    • 8e2ef2b Merge pull request #681 from codecov/dependabot/npm_and_yarn/types/node-17.0.23
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies github_actions 
    opened by dependabot[bot] 1
  • Bump actions/checkout from 2 to 3

    Bump actions/checkout from 2 to 3

    Bumps actions/checkout from 2 to 3.

    Release notes

    Sourced from actions/checkout's releases.

    v3.0.0

    • Update default runtime to node16

    v2.4.0

    • Convert SSH URLs like org-<ORG_ID>@github.com: to https://github.com/ - pr

    v2.3.5

    Update dependencies

    v2.3.4

    v2.3.3

    v2.3.2

    Add Third Party License Information to Dist Files

    v2.3.1

    Fix default branch resolution for .wiki and when using SSH

    v2.3.0

    Fallback to the default branch

    v2.2.0

    Fetch all history for all tags and branches when fetch-depth=0

    v2.1.1

    Changes to support GHES (here and here)

    v2.1.0

    Changelog

    Sourced from actions/checkout's changelog.

    Changelog

    v2.3.1

    v2.3.0

    v2.2.0

    v2.1.1

    • Changes to support GHES (here and here)

    v2.1.0

    v2.0.0

    Commits

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    dependencies github_actions 
    opened by dependabot[bot] 1
Releases(1.2.0)
Owner
Permafrost Software
Permafrost Software
Demo serverless applications, examples code snippets and resources for PHP

The Serverless LAMP stack Examples Code example Description AWS blog link 0.1-SimplePhpFunction A very simple implementation of a PHP Lambda function.

AWS Samples 303 Dec 20, 2022
A library of powerful code snippets to help you get the job done with Gravity Forms and Gravity Perks.

Gravity Wiz Snippet Library Gravity Wiz is creating the most comprehensive library of snippets for Gravity Forms ever. We'll be consistently moving ou

Gravity Wiz 151 Dec 27, 2022
Custom code snippets and examples for SkyVerge-built WooCommerce extensions

SkyVerge WooCommerce Plugins Welcome to the wc-plugins-snippets repository! This repository stores code snippets related to SkyVerge WooCommerce plugi

SkyVerge 255 Nov 16, 2022
A PHP Library To Make Your Work Work Easier/Faster

This Is A Php Library To Make Your Work Easier/Faster,

functionality 2 Dec 30, 2022
A self-hosted alternative to putting your code snippets on Gist.

WP-Gistpen Contributors: JamesDiGioia Donate link: http://jamesdigioia.com/ Tags: gist, code snippets, codepen Requires at least: 5.2 Requires PHP: 5.

Intraxia 71 Nov 15, 2022
Collection of useful PHP functions, mini-classes, and snippets for every day.

JBZoo / Utils Collection of PHP functions, mini classes and snippets for everyday developer's routine life. Install composer require jbzoo/utils Usage

JBZoo Toolbox 786 Dec 30, 2022
Collection of PHP functions, mini classes and snippets for everyday developer's routine life

JBZoo / Utils Collection of PHP functions, mini classes and snippets for everyday developer's routine life. Install composer require jbzoo/utils Usage

JBZoo Toolbox 776 Jun 2, 2022
⚡ Php snippets, random stuff, demos, functions, fast message system, agnostic and framework free - 100% compactible ;) ⚡

⚡ Php8 FPM Nginx Fast, Scripts, Pearls & Treasures ?? Want to run and test asap ? docker-compose up -d phpgit_php8;ip=$(docker-machine ip default);ech

Benjamin FONTAINE 0 Mar 20, 2022
MagentoSnippets - Magento Front End Snippets, plugin for Sublime Text

MagentoSnippets Magento Front End Snippets, plugin for Sublime Text. This tool serves to aid the productivity during the Magento's theme development t

MageFront 27 Nov 1, 2022
Easily create and read Corona Warn App Check-In URLs

bauer-kirch/cwa-event-registration-url This PHP library allows generating Check-In URLs for the Corona Warn App which can then be used to generate a Q

Bauer + Kirch GmbH 9 Dec 30, 2021