Skip to content

Commit 04dd497

Browse files
init: add code/tests
1 parent 56af194 commit 04dd497

File tree

5 files changed

+142
-10
lines changed

5 files changed

+142
-10
lines changed

README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,37 @@
1-
# queue
1+
<p align="center">
2+
<a href="https://github.com/phpsimple"><img src="https://avatars.githubusercontent.com/u/120518417?s=84&v=4"></a><br>
3+
</p>
4+
5+
<h1 align="center">Queue</h1>
6+
7+
<p align="center">Very simple FIFO queue implementation</p>
8+
9+
[![Test Coverage](https://api.codeclimate.com/v1/badges/9487615798456eafda6c/test_coverage)](https://codeclimate.com/github/phpsimple/queue/test_coverage)
10+
11+
---
12+
13+
Installation
14+
---
15+
The recommended way to install is via Composer:
16+
17+
```shell
18+
composer require phpsimple/queue
19+
```
20+
21+
Usage
22+
---
23+
```php
24+
use PhpSimple\Queue;
25+
26+
$queue = new Queue();
27+
28+
for ($i = 0; $i < 10; $i++) {
29+
$queue->push($i);
30+
}
31+
32+
while ($item = $queue->pop()) {
33+
var_dump($item);
34+
}
35+
```
36+
---
37+
[![Stand With Ukraine](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://vshymanskyy.github.io/StandWithUkraine)

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"autoload": {
2626
"psr-4": {
27-
"Phpsimple\\": "src/"
27+
"PhpSimple\\": "src/"
2828
}
2929
},
3030
"autoload-dev": {

phpunit.xml.dist

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
5-
backupGlobals="false"
6-
bootstrap="vendor/autoload.php"
7-
cacheResult="false"
8-
colors="true"
9-
failOnRisky="true"
10-
failOnWarning="true"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
5+
backupGlobals="false"
6+
bootstrap="vendor/autoload.php"
7+
cacheResult="false"
8+
colors="true"
9+
failOnRisky="true"
10+
failOnWarning="true"
1111
>
1212
<testsuites>
1313
<testsuite name="default">

src/Queue.php

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpSimple;
4+
5+
use Countable;
6+
7+
use function array_shift;
8+
use function count;
9+
use function current;
10+
use function in_array;
11+
12+
/**
13+
* @template Q
14+
*/
15+
final class Queue implements Countable
16+
{
17+
public function __construct(/** @var array<Q> */ private array $items = [])
18+
{
19+
}
20+
21+
public function clear(): void
22+
{
23+
$this->items = [];
24+
}
25+
26+
public function contains(mixed $item): bool
27+
{
28+
return in_array(needle: $item, haystack: $this->items, strict: true);
29+
}
30+
31+
public function pop(): mixed
32+
{
33+
if ($this->isEmpty()) {
34+
return false;
35+
}
36+
37+
return array_shift(array: $this->items);
38+
}
39+
40+
public function push(mixed $item): void
41+
{
42+
if (null !== $item) {
43+
$this->items[] = $item;
44+
}
45+
}
46+
47+
public function peek(): mixed
48+
{
49+
return current(array: $this->items);
50+
}
51+
52+
public function isEmpty(): bool
53+
{
54+
return [] === $this->items;
55+
}
56+
57+
public function count(): int
58+
{
59+
return count(value: $this->items);
60+
}
61+
}

tests/src/QueueTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PhpSimple\Tests;
4+
5+
use PhpSimple\Queue;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class QueueTest extends TestCase
9+
{
10+
public function testQueue(): void
11+
{
12+
$queue = new Queue();
13+
14+
$this->assertCount(expectedCount: 0, haystack: $queue);
15+
$this->assertTrue(condition: $queue->isEmpty());
16+
17+
$queue->push(item: 1);
18+
$queue->push(item: 2);
19+
$queue->push(item: 3);
20+
21+
$this->assertCount(expectedCount: 3, haystack: $queue);
22+
$this->assertFalse(condition: $queue->isEmpty());
23+
$this->assertTrue(condition: $queue->contains(item: 1));
24+
25+
$queue->pop();
26+
27+
$this->assertCount(expectedCount: 2, haystack: $queue);
28+
$this->assertEquals(expected: 2, actual: $queue->peek());
29+
30+
$queue->clear();
31+
$this->assertCount(expectedCount: 0, haystack: $queue);
32+
33+
$this->assertFalse(condition: $queue->pop());
34+
}
35+
}

0 commit comments

Comments
 (0)