Skip to content
This repository was archived by the owner on Jul 8, 2018. It is now read-only.

Commit e8f0f61

Browse files
committed
Merge pull request #2 from pinepain/eliminate_duplication
Add behavior logic to WeakKeyValueMap and use inheritance
2 parents 8f8a1c6 + 5ec0e84 commit e8f0f61

File tree

9 files changed

+217
-263
lines changed

9 files changed

+217
-263
lines changed

.scrutinizer.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
tools:
2+
external_code_coverage: true

.travis.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ before_script:
2727
- composer install
2828

2929
script:
30-
- vendor/bin/phpunit --coverage-text --configuration phpunit.xml
30+
- vendor/bin/phpunit --coverage-text --configuration phpunit.xml --coverage-clover=coverage.clover
31+
32+
after_script:
33+
- wget https://scrutinizer-ci.com/ocular.phar
34+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
# Weak-referenced data structures for PHP [![Build Status](https://travis-ci.org/pinepain/php-weak-lib.svg)](https://travis-ci.org/pinepain/php-weak-lib)
1+
# Weak-referenced data structures for PHP
2+
3+
[![Build Status](https://travis-ci.org/pinepain/php-weak-lib.svg)](https://travis-ci.org/pinepain/php-weak-lib)
4+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/pinepain/php-weak-lib/badges/quality-score.png)](https://scrutinizer-ci.com/g/pinepain/php-weak-lib)
5+
[![Code Coverage](https://scrutinizer-ci.com/g/pinepain/php-weak-lib/badges/coverage.png)](https://scrutinizer-ci.com/g/pinepain/php-weak-lib)
26

37
This library is based on [php-weak][php-weak-ext] PHP extension and provides various weak data structures:
48

src/AbstractWeakMap.php

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the pinepain/php-weak-lib PHP library.
5+
*
6+
* Copyright (c) 2016 Bogdan Padalko <zaq178miami@gmail.com>
7+
*
8+
* Licensed under the MIT license: http://opensource.org/licenses/MIT
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code or visit http://opensource.org/licenses/MIT
12+
*/
13+
14+
namespace Weak;
15+
16+
use RuntimeException;
17+
use SplObjectStorage;
18+
19+
abstract class AbstractWeakMap extends SplObjectStorage
20+
{
21+
const WEAK_KEY = 1;
22+
const WEAK_VAL = 2;
23+
24+
protected $behavior = 0;
25+
26+
/** {@inheritdoc} */
27+
public function attach($object, $data = null)
28+
{
29+
if ($this->behavior & self::WEAK_KEY) {
30+
$object = $this->buildWeakKey($object);
31+
}
32+
33+
if ($this->behavior & self::WEAK_VAL) {
34+
$data = $this->buildWeakValue($object, $data);
35+
}
36+
37+
parent::attach($object, $data);
38+
}
39+
40+
/** {@inheritdoc} */
41+
public function current()
42+
{
43+
$object = parent::current();
44+
45+
if ($this->behavior & self::WEAK_KEY) {
46+
$object = $this->extractWeakObject($object);
47+
}
48+
49+
/* In some rare cases (it should never happen normally) orphaned weak reference may occurs in storage, so
50+
* so current object here MAY be null
51+
*/
52+
53+
return $object;
54+
}
55+
56+
/** {@inheritdoc} */
57+
public function addAll($storage)
58+
{
59+
foreach ($storage as $obj) {
60+
$this->attach($obj, $storage[$obj]);
61+
}
62+
}
63+
64+
/** {@inheritdoc} */
65+
public function removeAllExcept($storage)
66+
{
67+
$pending_removal = new \SplObjectStorage();
68+
69+
foreach ($this as $obj) {
70+
if (!$storage->contains($obj)) {
71+
$pending_removal->attach($obj);
72+
}
73+
}
74+
75+
$this->removeAll($pending_removal);
76+
}
77+
78+
/** {@inheritdoc} */
79+
public function getHash($object)
80+
{
81+
if ($this->behavior & self::WEAK_KEY) {
82+
if ($object instanceof HashedReference) {
83+
return $object->getHash();
84+
}
85+
}
86+
87+
return parent::getHash($object);
88+
}
89+
90+
/** {@inheritdoc} */
91+
public function getInfo()
92+
{
93+
$info = parent::getInfo();
94+
95+
if ($this->behavior & self::WEAK_VAL) {
96+
$info = $this->extractWeakObject($info);
97+
}
98+
99+
return $info;
100+
}
101+
102+
/** {@inheritdoc} */
103+
public function setInfo($data)
104+
{
105+
$object = $this->current();
106+
107+
if (!$object) {
108+
return; // nothing to do
109+
}
110+
111+
if ($this->behavior & self::WEAK_VAL) {
112+
$data = $this->buildWeakValue($object, $data);
113+
}
114+
115+
parent::setInfo($data);
116+
}
117+
118+
/** {@inheritdoc} */
119+
public function offsetSet($object, $data = null)
120+
{
121+
$this->attach($object, $data);
122+
}
123+
124+
/** {@inheritdoc} */
125+
public function offsetGet($object)
126+
{
127+
$info = parent::offsetGet($object);
128+
129+
if ($this->behavior & self::WEAK_VAL) {
130+
$info = $this->extractWeakObject($info);
131+
}
132+
133+
return $info;
134+
}
135+
136+
protected function validateInfo($data)
137+
{
138+
if (!is_object($data)) {
139+
throw new RuntimeException(static::class . ' expects data to be object, ' . gettype($data) . ' given');
140+
}
141+
}
142+
143+
protected function extractWeakObject($object)
144+
{
145+
if ($object instanceof Reference) {
146+
$object = $object->get();
147+
}
148+
149+
return $object;
150+
}
151+
152+
protected function buildWeakKey($object)
153+
{
154+
$object = new HashedReference($object, [$this, 'detach'], 'spl_object_hash');
155+
156+
return $object;
157+
}
158+
159+
protected function buildWeakValue($object, $data)
160+
{
161+
$this->validateInfo($data);
162+
163+
$data = new Reference($data, function () use ($object) {
164+
$this->detach($object);
165+
});
166+
167+
return $data;
168+
}
169+
}

src/WeakKeyMap.php

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,69 +13,7 @@
1313

1414
namespace Weak;
1515

16-
use SplObjectStorage;
17-
18-
class WeakKeyMap extends SplObjectStorage
16+
class WeakKeyMap extends AbstractWeakMap
1917
{
20-
/** {@inheritdoc} */
21-
public function attach($object, $data = null)
22-
{
23-
$object = new HashedReference($object, [$this, 'detach'], 'spl_object_hash');
24-
25-
parent::attach($object, $data);
26-
}
27-
28-
/** {@inheritdoc} */
29-
public function current()
30-
{
31-
$current = parent::current();
32-
33-
if ($current instanceof Reference) {
34-
$current = $current->get();
35-
}
36-
37-
/* In some rare cases (it should never happens normally) orphaned weak reference may occurs in storage, so
38-
* so $current here MAY be null
39-
*/
40-
41-
return $current;
42-
}
43-
44-
/** {@inheritdoc} */
45-
public function offsetSet($object, $data = null)
46-
{
47-
$this->attach($object, $data);
48-
}
49-
50-
/** {@inheritdoc} */
51-
public function addAll($storage)
52-
{
53-
foreach ($storage as $obj) {
54-
$this->attach($obj, $storage[$obj]);
55-
}
56-
}
57-
58-
/** {@inheritdoc} */
59-
public function removeAllExcept($storage)
60-
{
61-
$pending_removal = new \SplObjectStorage();
62-
63-
foreach ($this as $obj) {
64-
if (!$storage->contains($obj)) {
65-
$pending_removal->attach($obj);
66-
}
67-
}
68-
69-
$this->removeAll($pending_removal);
70-
}
71-
72-
/** {@inheritdoc} */
73-
public function getHash($object)
74-
{
75-
if ($object instanceof HashedReference) {
76-
return $object->getHash();
77-
}
78-
79-
return parent::getHash($object);
80-
}
18+
protected $behavior = self::WEAK_KEY;
8119
}

src/WeakKeyValueMap.php

Lines changed: 2 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -13,124 +13,7 @@
1313

1414
namespace Weak;
1515

16-
use RuntimeException;
17-
use SplObjectStorage;
18-
19-
class WeakKeyValueMap extends SplObjectStorage
16+
class WeakKeyValueMap extends AbstractWeakMap
2017
{
21-
/** {@inheritdoc} */
22-
public function attach($object, $data = null)
23-
{
24-
$this->validateInfo($data);
25-
26-
$object = new HashedReference($object, [$this, 'detach'], 'spl_object_hash');
27-
28-
$data = new Reference($data, function () use ($object) {
29-
$this->detach($object);
30-
});
31-
32-
parent::attach($object, $data);
33-
}
34-
35-
/** {@inheritdoc} */
36-
public function current()
37-
{
38-
$current = parent::current();
39-
40-
if ($current instanceof Reference) {
41-
$current = $current->get();
42-
}
43-
44-
/* In some rare cases (it should never happens normally) orphaned weak reference may occurs in storage, so
45-
* so $current here MAY be null
46-
*/
47-
48-
return $current;
49-
}
50-
51-
/** {@inheritdoc} */
52-
public function addAll($storage)
53-
{
54-
foreach ($storage as $obj) {
55-
$this->attach($obj, $storage[$obj]);
56-
}
57-
}
58-
59-
public function removeAllExcept($storage)
60-
{
61-
$pending_removal = new \SplObjectStorage();
62-
63-
foreach ($this as $obj) {
64-
if (!$storage->contains($obj)) {
65-
$pending_removal->attach($obj);
66-
}
67-
}
68-
69-
$this->removeAll($pending_removal);
70-
}
71-
72-
/** {@inheritdoc} */
73-
public function getHash($object)
74-
{
75-
if ($object instanceof HashedReference) {
76-
return $object->getHash();
77-
}
78-
79-
return parent::getHash($object);
80-
}
81-
82-
/** {@inheritdoc} */
83-
public function getInfo()
84-
{
85-
$info = parent::getInfo(); // TODO: Change the autogenerated stub
86-
87-
if ($info instanceof Reference) {
88-
$info = $info->get();
89-
}
90-
91-
return $info;
92-
}
93-
94-
/** {@inheritdoc} */
95-
public function setInfo($data)
96-
{
97-
$this->validateInfo($data);
98-
99-
$object = $this->current();
100-
101-
if (!$object) {
102-
return; // nothing to do
103-
}
104-
105-
$data = new Reference($data, function () use ($object) {
106-
$this->detach($object);
107-
});
108-
109-
parent::setInfo($data);
110-
}
111-
112-
/** {@inheritdoc} */
113-
public function offsetSet($object, $data = null)
114-
{
115-
$this->attach($object, $data);
116-
}
117-
118-
/** {@inheritdoc} */
119-
public function offsetGet($object)
120-
{
121-
$info = parent::offsetGet($object); // TODO: Change the autogenerated stub
122-
123-
if ($info instanceof Reference) {
124-
$info = $info->get();
125-
}
126-
127-
return $info;
128-
}
129-
130-
protected function validateInfo($data)
131-
{
132-
if (!is_object($data)) {
133-
throw new RuntimeException(self::class . ' expects data to be object, ' . gettype($data) . ' given');
134-
}
135-
}
18+
protected $behavior = self::WEAK_KEY | self::WEAK_VAL;
13619
}

0 commit comments

Comments
 (0)