# Clone the repository
git clone https://github.com/guccilive/sorted-linked-list.git
cd sorted-linked-list
# Install dependencies
composer install
# or
make install# Run tests
composer test
# or
make test
# Run tests with coverage (requires Xdebug or PCOV)
composer test:coverage
# or
make coverage# Run all checks (tests + phpstan + cs)
composer check
# or
make check
# Run PHPStan
composer phpstan
# or
make phpstan
# Check coding standards
composer cs
# or
make cs
# Fix coding standards
composer cs:fix
# or
make cs-fixThe repository includes an optional pre-commit hook that automatically runs quality checks before each commit:
# The hook is already set up and will:
# 1. Auto-fix coding standards (composer cs:fix)
# 2. Run all checks (composer check)
# 3. Block commit if checks fail
# To bypass the hook (emergency only):
git commit --no-verify -m "your message"
# To run checks manually:
./pre-commit.shThe pre-commit hook ensures code quality by catching issues before they reach the repository.
To generate code coverage reports, you need to install a coverage driver.
pecl install xdebugpecl install pcovThen add to your php.ini:
extension=pcov.so
pcov.enabled=1Once installed, run:
composer test:coverage # HTML report
composer test:coverage-text # Terminal output- β Type Safety: Enforces consistent types (int or string) throughout the list
- β‘ Auto-Sorting: Elements maintain sorted order automatically on insertion
- π― Optimized Operations: Early-exit algorithms for efficient searching
- πΎ Memory Efficient: Single-linked structure with minimal overhead
- π Standard Interfaces: Implements
CountableandIteratorAggregate - π O(1) Count: Cached counter for instant size retrieval
- π‘οΈ Final Classes: Prevents inheritance issues and enables JIT optimization
composer require guccilive/sorted-linked-list- PHP >= 8.3.0
use Guccilive\SortedLinkedList\SortedLinkedList;
// Create a new sorted list
$list = new SortedLinkedList();
// Insert values (automatically sorted)
$list->insert(30);
$list->insert(10);
$list->insert(20);
// Get as array
print_r($list->toArray()); // [10, 20, 30]
// Check if value exists
$list->contains(20); // true
$list->contains(40); // false
// Get by index
$list->get(0); // 10
$list->get(1); // 20
// Count elements
count($list); // 3
$list->isEmpty(); // false
// Remove value
$list->remove(20);
print_r($list->toArray()); // [10, 30]
// Clear all elements
$list->clear();The list enforces type consistency - once the first element is inserted, all subsequent elements must be of the same type:
$list = new SortedLinkedList();
$list->insert(10);
$list->insert(20);
// This will throw TypeMismatchException
$list->insert('string'); // β Error: List contains int values, but received stringWorks perfectly with strings too:
$list = new SortedLinkedList();
$list->insert('delta');
$list->insert('alpha');
$list->insert('charlie');
print_r($list->toArray()); // ['alpha', 'charlie', 'delta']The list implements IteratorAggregate, so you can use it in foreach loops:
$list = new SortedLinkedList();
$list->insert(30);
$list->insert(10);
$list->insert(20);
foreach ($list as $value) {
echo $value . "\n";
}
// Output:
// 10
// 20
// 30Duplicate values are allowed:
$list = new SortedLinkedList();
$list->insert(20);
$list->insert(10);
$list->insert(20);
print_r($list->toArray()); // [10, 20, 20]
// Remove only removes first occurrence
$list->remove(20);
print_r($list->toArray()); // [10, 20]insert(int|string $value): void- Inserts a value in sorted positioncontains(int|string $value): bool- Checks if value existsremove(int|string $value): bool- Removes first occurrence of valueget(int $index): int|string- Gets value at indextoArray(): array- Returns all values as arrayclear(): void- Removes all elementsisEmpty(): bool- Checks if list is emptycount(): int- Returns number of elementsgetIterator(): Traversable- Returns iterator for foreach
TypeMismatchException- Thrown when inserting incompatible typeOutOfBoundsException- Thrown when accessing invalid index
- Final Classes: Prevents inheritance complexity and enables PHP's JIT optimization
- Readonly Properties: Node values are immutable after creation (PHP 8.1+)
- Strict Types:
declare(strict_types=1)in every file - Generics: PHPStan templates for enhanced type safety
- Early Exits: Search algorithms terminate early when target cannot exist
- Cached Count: O(1) count operations via internal counter
- Insert: O(n) - Linear scan to find insertion point, optimized with early exits
- Search: O(n) - Linear search with early termination for sorted data
- Remove: O(n) - Linear scan with early exit optimization
- Get by Index: O(n) - Linear traversal to index
- Count: O(1) - Cached counter
- toArray: O(n) - Single traversal
- Follow PSR-12
- Use strict types
- Add PHPDoc blocks
- Write tests for new features
- Keep complexity under 10
This project is licensed under the MIT License - see the LICENSE file for details.
Heritier Mashini
- GitHub: @guccilive
- Email: 37352340+guccilive@users.noreply.github.com
Built with modern PHP practices and attention to performance and type safety.