{"id":21466564,"url":"https://github.com/permafrost-dev/code-snippets","last_synced_at":"2025-07-12T08:34:22.201Z","repository":{"id":38848349,"uuid":"389273925","full_name":"permafrost-dev/code-snippets","owner":"permafrost-dev","description":"Easily create and work with code snippets from PHP","archived":false,"fork":false,"pushed_at":"2024-11-18T02:52:34.000Z","size":103,"stargazers_count":14,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-12-06T15:52:41.937Z","etag":null,"topics":["code-snippets","permafrost","snippets"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/permafrost-dev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"permafrost-dev","custom":"https://permafrost.dev/open-source"}},"created_at":"2021-07-25T05:59:53.000Z","updated_at":"2024-09-25T13:12:42.000Z","dependencies_parsed_at":"2024-01-16T14:03:52.594Z","dependency_job_id":"3f688d1b-e3a7-41e5-a4ed-2df4d9a5b744","html_url":"https://github.com/permafrost-dev/code-snippets","commit_stats":{"total_commits":72,"total_committers":3,"mean_commits":24.0,"dds":0.4027777777777778,"last_synced_commit":"38e889c6acb551941753d006e6e53c6452c9464d"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":"spatie/package-skeleton-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Fcode-snippets","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Fcode-snippets/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Fcode-snippets/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/permafrost-dev%2Fcode-snippets/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/permafrost-dev","download_url":"https://codeload.github.com/permafrost-dev/code-snippets/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230532448,"owners_count":18240792,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-snippets","permafrost","snippets"],"created_at":"2024-11-23T08:14:37.294Z","updated_at":"2024-12-20T04:07:37.875Z","avatar_url":"https://github.com/permafrost-dev.png","language":"PHP","funding_links":["https://github.com/sponsors/permafrost-dev","https://permafrost.dev/open-source"],"categories":[],"sub_categories":[],"readme":"# code-snippets\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/v/release/permafrost-dev/code-snippets.svg?sort=semver\u0026logo=github\" alt=\"Package Version\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/license/permafrost-dev/code-snippets.svg?logo=opensourceinitiative\u0026\" alt=\"license\"\u003e\n    \u003cimg src=\"https://github.com/permafrost-dev/code-snippets/actions/workflows/run-tests.yml/badge.svg?branch=main\u0026\" alt=\"Test Run Status\"\u003e\n    \u003cimg src=\"https://codecov.io/gh/permafrost-dev/code-snippets/branch/main/graph/badge.svg\" alt=\"code coverage\"\u003e\n\u003c/p\u003e\n\nEasily create and work with code snippets from source code files of any type in PHP.\n\n_The original code this package is based on was borrowed from the [`spatie/backtrace`](https://github.com/spatie/backtrace) package._\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require permafrost-dev/code-snippets\n```\n\n## Usage\n\n_Note: Although the examples here reference php files, any file type can be used when creating a `CodeSnippet`._\n\n### Creating a snippet\n\nUse the `surroundingLine($num)` method to select the \"target\" line, which will be returned as the middle line of the snippet:\n\n```php\nuse Permafrost\\CodeSnippets\\CodeSnippet;\n\n$snippet = (new CodeSnippet())\n    -\u003esurroundingLine(4)\n    -\u003esnippetLineCount(6)\n    -\u003efromFile('/path/to/a/file.php');\n```\n\nUse the `surroundingLines($first, $last)` method to select a range of \"target\" lines, which will be returned as the middle lines of the snippet:\n\n```php\n$snippet = (new CodeSnippet())\n    -\u003esurroundingLines(4, 7)\n    -\u003esnippetLineCount(6)\n    -\u003efromFile('/path/to/a/file.php');\n```\n\nUse the `linesBefore()` and `linesAfter()` methods to specify the number of context lines to display before and after the \"target\" lines:\n\n```php\n// the \"target\" line isn't displayed in the middle, but as the second line\n$snippet = (new CodeSnippet())\n    -\u003esurroundingLine(4)\n    -\u003elinesBefore(1)\n    -\u003elinesAfter(3)\n    -\u003efromFile('/path/to/a/file.php');\n```\n\n### Getting the snippet contents\n\nThe `getLines()` method returns an array of `SnippetLine` instances.  The keys of the resulting array are the line numbers.\n\nThe `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.\n\nTo get the value of a `SnippetLine`, use the `value()` method or cast the object to a string.\n\n```php\n$snippet = (new CodeSnippet())\n    -\u003esurroundingLine(4)\n    -\u003esnippetLineCount(5)\n    -\u003efromFile('/path/to/a/file.php');\n    \nforeach($snippet-\u003egetLines() as $lineNum =\u003e $line) {\n    $prefix = $line-\u003eisSelected() ? ' * ' : '   ';\n    \n    echo $prefix . $line-\u003elineNumber() . ' - ' . $line-\u003evalue() . PHP_EOL;\n}\n```\n\n### Snippet line count\n\nTo determine the number of lines in the snippet, use the `getSnippetLineCount()` method:\n\n```php\n$snippet = (new CodeSnippet())\n    -\u003esurroundingLines(4, 7)\n    -\u003elinesBefore(3)\n    -\u003elinesAfter(3)\n    -\u003efromFile('/path/to/a/file.php');\n    \necho \"Snippet line count: \" . $snippet-\u003egetSnippetLineCount() . PHP_EOL;\n```\n\nYou can also use `count()` on the result of the `getLines()` method:\n\n```php\n$snippet = (new CodeSnippet())\n    -\u003esurroundingLines(4, 7)\n    -\u003elinesBefore(3)\n    -\u003elinesAfter(3)\n    -\u003efromFile('/path/to/a/file.php');\n    \necho \"Snippet line count: \" . count($snippet-\u003egetLines()) . PHP_EOL;\n```\n\nTo return an array containing the line numbers for the snippet, use `getLineNumbers()`:\n\n```php\nprint_r($snippet-\u003egetLineNumbers());\n```\n\n### Returning the snippet as a string\n\nReturn the contents of the snippet as as string using the `toString()` method or by casting the snippet to a string directly:\n\n```php\n$snippet = (new CodeSnippet())\n    -\u003esurroundingLines(4, 7)\n    -\u003elinesBefore(3)\n    -\u003elinesAfter(3)\n    -\u003efromFile('/path/to/a/file.php');\n    \necho \"Snippet: \\n\" . $snippet-\u003etoString() . PHP_EOL;\n```\n\n## Testing\n\n```bash\n./vendor/bin/phpunit\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Patrick Organ](https://github.com/patinthehat)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpermafrost-dev%2Fcode-snippets","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpermafrost-dev%2Fcode-snippets","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpermafrost-dev%2Fcode-snippets/lists"}