Skip to content
This repository was archived by the owner on Jul 8, 2018. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ sudo: false
language: php

php:
- 7.1
- 7.2
- nightly

Expand All @@ -17,7 +16,7 @@ matrix:

env:
- PHP_REF_VERSION=master
- PHP_REF_VERSION=v0.5.0
- PHP_REF_VERSION=v0.6.0

before_install:
- sh install_php_ref_ext.sh ${PHP_REF_VERSION}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ If you like my work and want to show your appreciation, please consider supporti

## Requirements

- PHP >= 7.1
- PHP >= 7.2
- (optional, required only for maps weak behavior) [php-ref][php-ref-ext] extension


Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
}
],
"require": {
"php": "~7.0"
"php": "~7.2"
},
"suggest": {
"ext-ref": "Needed to support weak map variations"
},
"require-dev": {
"phpunit/phpunit": "^6.4",
"pinepain/php-ref-stubs": "~0.4.1"
"pinepain/php-ref-stubs": "~0.6.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 7 additions & 1 deletion src/Bucket.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@
*/
final class Bucket
{
/**
* @var object
*/
public $key;
/**
* @var object
*/
public $value;

/**
* @param object $key
* @param object $value
*/
public function __construct($key, $value)
public function __construct(object $key, object $value)
{
$this->key = $key;
$this->value = $value;
Expand Down
20 changes: 4 additions & 16 deletions src/ObjectBiMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

class ObjectBiMap implements ObjectBiMapInterface
{
use ObjectTypeHintTrait;

protected $behavior = self::DEFAULT;

/**
Expand Down Expand Up @@ -84,16 +82,12 @@ public function values(): ObjectBiMapInterface
/**
* {@inheritdoc}
*/
public function put($key, $value)
public function put(object $key, object $value)
{
$this->assertObject($key, 'Key');

if ($this->keys->has($key)) {
throw new OverflowException('Value with such key already exists');
}

$this->assertObject($value, 'Value');

if ($this->values->has($value)) {
// UNEXPECTED
throw new OverflowException('Key with such value already exists');
Expand All @@ -106,31 +100,25 @@ public function put($key, $value)
/**
* {@inheritdoc}
*/
public function get($key)
public function get(object $key)
{
$this->assertObject($key, 'Key');

return $this->keys->get($key);
}

/**
* {@inheritdoc}
*/

public function has($key): bool
public function has(object $key): bool
{
$this->assertObject($key, 'Key');

return $this->keys->has($key);
}

/**
* {@inheritdoc}
*/
public function remove($key)
public function remove(object $key)
{
$this->assertObject($key, 'Key');

if (!$this->keys->has($key)) {
throw new OutOfBoundsException('Value with such key not found');
}
Expand Down
27 changes: 8 additions & 19 deletions src/ObjectMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

class ObjectMap implements ObjectMapInterface
{
use ObjectTypeHintTrait;

protected $behavior = self::DEFAULT;

/**
Expand All @@ -43,11 +41,8 @@ public function __construct(int $behavior = self::DEFAULT)
/**
* {@inheritdoc}
*/
public function put($key, $value)
public function put(object $key, object $value)
{
$this->assertObject($key, 'Key');
$this->assertObject($value, 'Value'); // while we may associate non-object value, for interface compatibility we don't do that

$hash = $this->getHash($key);

if (isset($this->keys[$hash])) {
Expand All @@ -62,10 +57,8 @@ public function put($key, $value)
/**
* {@inheritdoc}
*/
public function get($key)
public function get(object $key)
{
$this->assertObject($key, 'Key');

$hash = $this->getHash($key);

if (!isset($this->keys[$hash])) {
Expand All @@ -80,10 +73,8 @@ public function get($key)
/**
* {@inheritdoc}
*/
public function has($key): bool
public function has(object $key): bool
{
$this->assertObject($key, 'Key');

$hash = $this->getHash($key);

return isset($this->keys[$hash]);
Expand All @@ -92,10 +83,8 @@ public function has($key): bool
/**
* {@inheritdoc}
*/
public function remove($key)
public function remove(object $key)
{
$this->assertObject($key, 'Key');

$hash = $this->getHash($key);

if (!isset($this->keys[$hash])) {
Expand Down Expand Up @@ -130,7 +119,7 @@ public function clear()
*
* @return string
*/
protected function getHash($value)
protected function getHash(object $value)
{
return spl_object_hash($value);
}
Expand All @@ -152,7 +141,7 @@ protected function doRemove(string $hash)
*
* @return Bucket
*/
protected function createBucket($key, $value, string $hash): Bucket
protected function createBucket(object $key, object $value, string $hash): Bucket
{
if ($this->behavior & self::WEAK_KEY) {
$key = $this->createReference($key, $hash);
Expand Down Expand Up @@ -182,12 +171,12 @@ protected function fetchBucketValue(Bucket $bucket)
}

/**
* @param $obj
* @param object $obj
* @param string $hash
*
* @return WeakReference
*/
protected function createReference($obj, string $hash): WeakReference
protected function createReference(object $obj, string $hash): WeakReference
{
return new WeakReference($obj, function () use ($hash) {
$this->doRemove($hash);
Expand Down
8 changes: 4 additions & 4 deletions src/ObjectMapInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface ObjectMapInterface extends Countable
* @throws OverflowException
* @return void
*/
public function put($key, $value);
public function put(object $key, object $value);

/**
* @param object $key
Expand All @@ -42,14 +42,14 @@ public function put($key, $value);
*
* @return object
*/
public function get($key);
public function get(object $key);

/**
* @param object $key
*
* @return bool
*/
public function has($key): bool;
public function has(object $key): bool;

/**
* @param object $key
Expand All @@ -58,7 +58,7 @@ public function has($key): bool;
*
* @return object
*/
public function remove($key);
public function remove(object $key);

/**
* @return void
Expand Down
29 changes: 0 additions & 29 deletions src/ObjectTypeHintTrait.php

This file was deleted.

55 changes: 0 additions & 55 deletions tests/AbstractObjectMapInterfaceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,28 +122,6 @@ public function testPutAndGet()
$this->assertSame($value, $map->get($key));
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException
* @expectedExceptionMessage Key is not an object
*/
public function testPutKeyNotAnObjectFails()
{
$map = $this->buildMap();

$map->put('foo', 'bar');
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException
* @expectedExceptionMessage Value is not an object
*/
public function testPutValueNotAnObjectFails()
{
$map = $this->buildMap();

$map->put(new stdClass(), 'bar');
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\OverflowException
* @expectedExceptionMessage Value with such key already exists
Expand All @@ -159,17 +137,6 @@ public function testPutExistentKeyFails()
$map->put($key, $value);
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException
* @expectedExceptionMessage Key is not an object
*/
public function testGetByNonObjectFails()
{
$map = $this->buildMap();

$map->get('test');
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\OutOfBoundsException
* @expectedExceptionMessage Value with such key not found
Expand All @@ -181,17 +148,6 @@ public function testGetNonexistentKeyFails()
$map->get(new stdClass());
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException
* @expectedExceptionMessage Key is not an object
*/
public function testHasByNonObjectFails()
{
$map = $this->buildMap();

$map->has('test');
}

public function testHasNonexistentKey()
{
$map = $this->buildMap();
Expand All @@ -209,17 +165,6 @@ public function testHasExistentKey()
$this->assertTrue($map->has($key));
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\UnexpectedValueException
* @expectedExceptionMessage Key is not an object
*/
public function testRemoveByNonObjectFails()
{
$map = $this->buildMap();

$map->remove('test');
}

/**
* @expectedException \Pinepain\ObjectMaps\Exceptions\OutOfBoundsException
* @expectedExceptionMessage Value with such key not found
Expand Down