zawiszaty / sorted-linked-list
Sorted linked list library with strict type safety for int or string values.
1.0.0
2026-02-19 13:51 UTC
Requires
- php: ^8.2
- ext-iconv: *
Requires (Dev)
- ext-xdebug: *
- friendsofphp/php-cs-fixer: ^3.76
- infection/infection: ^0.31
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- vimeo/psalm: ^6.11
README
Strictly typed, always-sorted singly linked list for PHP.
Overview
This library provides a SortedLinkedList data structure that:
- keeps values in ascending order after every write operation,
- supports one type per instance:
intorstring, - allows duplicates with stable order for equal values,
- exposes a small and predictable API.
Project specification:
requirements.mdHLD.md
Requirements
- PHP
^8.2 - Composer
Installation
composer install
Quick Start
<?php declare(strict_types=1); use Zawiszaty\SortedLinkedList\SortedLinkedList; $list = SortedLinkedList::forInt(); $list->addAll([3, 1, 2, 2]); echo $list . PHP_EOL; // [1, 2, 2, 3] echo $list->first() . PHP_EOL; // 1 echo $list->last() . PHP_EOL; // 3
String mode:
$list = SortedLinkedList::forString(); $list->addAll(['pear', 'apple', 'banana']); // ['apple', 'banana', 'pear']
Custom comparator:
$list = SortedLinkedList::forInt( static fn (int $a, int $b): int => $b <=> $a ); $list->addAll([1, 3, 2]); // [3, 2, 1]
Public API
SortedLinkedList::forInt(?callable $comparator = null): selfSortedLinkedList::forString(?callable $comparator = null): selfadd(int|string $value): voidaddAll(iterable<int|string> $values): voidremove(int|string $value): boolremoveAll(int|string $value): intremoveAt(int $index): voidcontains(int|string $value): boolget(int $index): int|stringfirst(): int|stringlast(): int|stringclear(): voidtoArray(): arraycount(): intisEmpty(): boolgetIterator(): Traversable__toString(): string
Exceptions
TypeMismatchExceptionIndexOutOfBoundsExceptionEmptyListException
Example Script
Run the example:
php examples/demo.php
Performance Benchmarking
Run a quick terminal benchmark:
php benchmarks/performance.php
Generate machine-readable results:
php benchmarks/performance.php --json > benchmarks/current.json
Render a human-readable report from JSON:
php benchmarks/render-report.php benchmarks/current.json
Check performance regression against the baseline (30% threshold by default):
php benchmarks/check-regression.php benchmarks/current.json benchmarks/baseline.json
Use a custom regression threshold (example 20%):
php benchmarks/check-regression.php benchmarks/current.json benchmarks/baseline.json 0.20
Quality Gates
This repository is configured with:
- PHPUnit
- PHPStan (max level)
- Psalm
- PHP CS Fixer
- Infection (mutation testing)
Current mutation gate is configured in infection.json5:
minMsi: 80.0minCoveredMsi: 80.0
Development Commands
composer test
composer stan
composer psalm
composer cs:check
XDEBUG_MODE=coverage composer infection
CI Pipeline
GitHub Actions workflow (.github/workflows/ci.yml) runs in this order:
build- static analysis in parallel:
phpstan,cs-check,psalm - in parallel after static analysis:
tests(PHP matrix) andmutation-testing
Project Layout
src/- library source codetests/- PHPUnit testsexamples/- runnable usage demorequirements.md- requirements specificationHLD.md- high-level design