davidvarney / sortedlinkedlist
A PHP library providing a sorted linked list for int or string values.
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/davidvarney/sortedlinkedlist
Requires
- php: >=8.0
Requires (Dev)
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-10-01 14:45:17 UTC
README
A tiny PHP library that provides a sorted singly-linked list which accepts either ints or strings (but not both). The list keeps values in sorted order as values are added.
Installation
Install with Composer:
composer require davidvarney/sortedlinkedlist
For development (to run tests):
composer install
Basic usage
use DavidVarney\SortedLinkedList\SortedLinkedList; $list = new SortedLinkedList(); $list->add(5); $list->add(1); $list->add(3); // returns [1, 3, 5] print_r($list->toArray()); // string join with default glue ", " echo $list->toString(); // "1, 3, 5" // type information echo $list->getType(); // "int" $list->clear(); $list->add('banana'); $list->add('apple'); echo $list->toString('|'); // "apple|banana"
Notes:
- The list enforces type consistency: once you add an int the list only accepts ints; same for strings.
- Attempting to add the other type throws an InvalidArgumentException.
API
add(int|string $value): void
— Insert a value in sorted order.remove(int|string $value): bool
— Remove a value; returns true if removed.toArray(): array
— Return the list as an array of values.toString(string $glue = ', '): string
— Return a joined string of values.contains(int|string $value): bool
— Check presence of a value.isEmpty(): bool
— Check if the list is empty.clear(): void
— Clear the list and reset the stored type.getType(): ?string
— Returns'int'
,'string'
, ornull
for empty list.
Running tests
This project uses PHPUnit (configured in phpunit.xml
). Run the tests with the vendored PHPUnit binary:
./vendor/bin/phpunit
For a more readable output use testdox:
./vendor/bin/phpunit --testdox
Test coverage
This repository doesn't include a coverage tool by default, but you can generate coverage with Xdebug or PCOV installed and PHPUnit's --coverage-html
option. Example (requires Xdebug enabled):
./vendor/bin/phpunit --coverage-html coverage
# open coverage/index.html in your browser
CI / PHPStan
This repository runs PHPStan via GitHub Actions on push
and pull_request
to main
.
To run PHPStan locally after installing dev dependencies:
./vendor/bin/phpstan analyse -c phpstan.neon
License
MIT