Skip to content

Commit 35c5f2a

Browse files
committed
Code coverage, docs, test updates
1 parent c5d87a5 commit 35c5f2a

File tree

12 files changed

+85
-41
lines changed

12 files changed

+85
-41
lines changed

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/.travis.yml export-ignore
4+
/.scrutinizer.yml export-ignore
5+
/phpunit.xml export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
build
12
vendor
23
composer.lock

.scrutinizer.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
build:
2+
tests:
3+
override:
4+
-
5+
command: 'phpunit'
6+
coverage:
7+
file: 'build/logs/coverage.xml'
8+
format: 'php-clover'
9+
10+
checks:
11+
php:
12+
line_length:
13+
max_length: '80'
14+
15+
tools:
16+
php_code_sniffer:
17+
config:
18+
standard: "PSR2"

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ php:
66

77
before_install:
88
- phpenv config-rm xdebug.ini
9-
- composer self-update
109

1110
install:
12-
- composer update --no-interaction
11+
- composer install --no-interaction
1312

1413
script:
1514
- composer test
File renamed without changes.

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "php-ds/php-ds",
33
"license": "MIT",
4-
"keywords": ["php", "data structures", "polyfill"],
4+
"keywords": ["php", "ds", "data structures", "polyfill"],
55
"authors": [
66
{
77
"name": "Rudi Theunissen",
@@ -15,10 +15,10 @@
1515
"php-ds/tests": "^1.1"
1616
},
1717
"suggest": {
18-
"ext-ds": "to improve performance and memory usage"
18+
"ext-ds": "to improve performance and reduce memory usage"
1919
},
2020
"scripts": {
21-
"test": "phpunit -c ./vendor/php-ds/tests/phpunit.xml"
21+
"test": "phpunit"
2222
},
2323
"autoload": {
2424
"psr-4" : {

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<phpunit bootstrap="vendor/autoload.php" colors="true">
2+
<testsuites>
3+
<testsuite>
4+
<directory>tests</directory>
5+
</testsuite>
6+
</testsuites>
7+
<filter>
8+
<whitelist>
9+
<directory>src</directory>
10+
</whitelist>
11+
</filter>
12+
<logging>
13+
<log type="coverage-html" target="build/logs/coverage"/>
14+
<log type="coverage-text" target="build/logs/coverage.txt"/>
15+
<log type="coverage-clover" target="build/logs/coverage.xml"/>
16+
</logging>
17+
</phpunit>

src/Traits/Capacity.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
namespace Ds\Traits;
33

44
/**
5-
* Capacity
6-
*
7-
* @package Ds\Traits
5+
* Common to structures that deal with an internal capacity. While none of the
6+
* PHP implementations actually make use of a capacity, it's important to keep
7+
* consistent with the extension.
88
*/
99
trait Capacity
1010
{
1111
/**
12-
* @var int
12+
* @var int internal capacity
1313
*/
1414
private $capacity = self::MIN_CAPACITY;
1515

@@ -37,23 +37,26 @@ public function allocate(int $capacity)
3737
}
3838

3939
/**
40-
* Increase Capacity
40+
* Called when capacity should be increased to accommodate new values.
4141
*/
42-
abstract protected function increaseCapacity();
42+
abstract protected function increaseCapacityWhenFull();
4343

4444
/**
45-
* Adjust Capacity
45+
* Adjusts the structure's capacity according to its current size.
4646
*/
4747
private function adjustCapacity()
4848
{
4949
$size = count($this);
5050

51-
if ($size >= $this->capacity) {
52-
$this->increaseCapacity();
53-
51+
// Automatically truncate the allocated buffer when the size of the
52+
// structure drops low enough.
53+
if ($size < $this->capacity / 4) {
54+
$this->capacity = max(self::MIN_CAPACITY, $this->capacity / 2);
5455
} else {
55-
if ($size < $this->capacity / 4) {
56-
$this->capacity = max(self::MIN_CAPACITY, $this->capacity / 2);
56+
57+
// Also check if we should increase capacity when the size changes.
58+
while ($size >= $this->capacity) {
59+
$this->increaseCapacityWhenFull();
5760
}
5861
}
5962
}

src/Traits/Collection.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
namespace Ds\Traits;
33

44
/**
5-
* Collection
6-
*
7-
* @package Ds\Traits
5+
* Common to structures that implement the base collection interface.
86
*/
97
trait Collection
108
{
@@ -14,27 +12,30 @@ trait Collection
1412
* This should be equivalent to a count of zero, but is not required.
1513
* Implementations should define what empty means in their own context.
1614
*
17-
* @return bool
15+
* @return bool whether the collection is empty.
1816
*/
1917
public function isEmpty(): bool
2018
{
2119
return count($this) === 0;
2220
}
2321

2422
/**
25-
* Json Serialize
23+
* Returns a representation that can be natively converted to JSON, which is
24+
* called when invoking json_encode.
2625
*
27-
* @return string
26+
* @return mixed
27+
*
28+
* @see JsonSerializable
2829
*/
2930
public function jsonSerialize()
3031
{
3132
return $this->toArray();
3233
}
3334

3435
/**
35-
* Creates a copy of the collection.
36+
* Creates a shallow copy of the collection.
3637
*
37-
* @return self
38+
* @return \Ds\Collection a shallow copy of the collection.
3839
*/
3940
public function copy(): \Ds\Collection
4041
{
@@ -44,24 +45,27 @@ public function copy(): \Ds\Collection
4445
/**
4546
* Returns an array representation of the collection.
4647
*
47-
* The format of the returned array is implementation-dependent.
48-
* Some implementations may throw an exception if an array representation
49-
* could not be created.
48+
* The format of the returned array is implementation-dependent. Some
49+
* implementations may throw an exception if an array representation
50+
* could not be created (for example when object are used as keys).
5051
*
5152
* @return array
5253
*/
5354
abstract public function toArray(): array;
5455

5556
/**
56-
* Debug Info
57+
* Invoked when calling var_dump.
58+
*
59+
* @return array
5760
*/
5861
public function __debugInfo()
5962
{
6063
return $this->toArray();
6164
}
6265

6366
/**
64-
* To String
67+
* Returns a string representation of the collection, which is invoked when
68+
* the collection is converted to a string.
6569
*/
6670
public function __toString()
6771
{

src/Traits/Sequence.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
<?php
22
namespace Ds\Traits;
33

4-
use Error;
54
use OutOfRangeException;
65
use Traversable;
76
use UnderflowException;
87

98
/**
10-
* Sequence
11-
*
12-
* @package Ds\Traits
9+
* Common functionality of all structures that implement 'Sequence'. Because the
10+
* polyfill's only goal is to achieve consistent behaviour, all sequences will
11+
* share the same implementation using an internal array.
1312
*/
1413
trait Sequence
1514
{

0 commit comments

Comments
 (0)