Skip to content

Commit 0ba7aa2

Browse files
authored
Merge pull request #2 from zorji/FixSetForArrayAccess
Fix set()/remove() for ArrayAccess
2 parents b1401c8 + b851eb6 commit 0ba7aa2

File tree

8 files changed

+89
-40
lines changed

8 files changed

+89
-40
lines changed

.php_cs

+17-23
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
2-
use Symfony\CS\Config\Config;
3-
use Symfony\CS\Finder\DefaultFinder;
4-
use Symfony\CS\Fixer\Contrib\HeaderCommentFixer;
5-
use Symfony\CS\FixerInterface;
6-
7-
$finder = DefaultFinder::create()->in(['config', 'src', 'tests']);
2+
$finder = PhpCsFixer\Finder::create()->in(['config', 'src', 'tests']);
83
$header = <<< EOF
94
This file is part of Underscore.php
105
@@ -14,21 +9,20 @@ For the full copyright and license information, please view the LICENSE
149
file that was distributed with this source code.
1510
EOF;
1611

17-
HeaderCommentFixer::setHeader($header);
12+
$fixer = new \PhpCsFixer\Fixer\Comment\HeaderCommentFixer;
1813

19-
return Config::create()
20-
->level(FixerInterface::SYMFONY_LEVEL)
21-
->fixers([
22-
'ereg_to_preg',
23-
'header_comment',
24-
'multiline_spaces_before_semicolon',
25-
'ordered_use',
26-
'php4_constructor',
27-
'phpdoc_order',
28-
'short_array_syntax',
29-
'short_echo_tag',
30-
'strict',
31-
'strict_param',
32-
])
33-
->setUsingCache(true)
34-
->finder($finder);
14+
return PhpCsFixer\Config::create()
15+
->setRules([
16+
'@PSR2' => true,
17+
// 'strict_param' => true,
18+
'array_syntax' => ['syntax' => 'short'],
19+
'header_comment' => [
20+
'header' => $header
21+
],
22+
'ordered_imports' => [],
23+
// 'ereg_to_preg' => true,
24+
'phpdoc_order' => true,
25+
// 'no_php4_constructor' => true,
26+
])
27+
->setUsingCache(true)
28+
->setFinder($finder);

.travis.yml

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ sudo: false
44
# Setup build matrix
55
language: php
66
php:
7-
- 5.4
8-
- 5.5
97
- 5.6
108
- 7.0
119
- hhvm

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
}
1515
],
1616
"require": {
17-
"php": ">=5.4.0",
17+
"php": ">=5.6.0",
1818
"doctrine/inflector": "^1.0",
1919
"patchwork/utf8": "^1.2"
2020
},
2121
"require-dev": {
22-
"friendsofphp/php-cs-fixer": "^1.10",
22+
"friendsofphp/php-cs-fixer": "^2.11",
2323
"phpunit/phpunit": "^4.8"
2424
},
2525
"autoload": {
@@ -35,5 +35,8 @@
3535
"scripts": {
3636
"test": ["php-cs-fixer fix --dry-run", "phpunit --verbose"],
3737
"lint": "php-cs-fixer fix"
38+
},
39+
"config": {
40+
"sort-packages": true
3841
}
3942
}

src/Methods/CollectionMethods.php

+19-9
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public static function setAndGet(&$collection, $key, $default = null)
114114
public static function remove($collection, $key)
115115
{
116116
// Recursive call
117-
if (is_array($key)) {
117+
if (static::isArrayLike($key)) {
118118
foreach ($key as $k) {
119119
static::internalRemove($collection, $k);
120120
}
@@ -137,7 +137,7 @@ public static function pluck($collection, $property)
137137
}, (array) $collection);
138138

139139
// Convert back to object if necessary
140-
if (is_object($collection)) {
140+
if (static::isNonArrayAccessObject($collection)) {
141141
$plucked = (object) $plucked;
142142
}
143143

@@ -199,7 +199,7 @@ public static function filterBy($collection, $property, $value, $comparisonOp =
199199

200200
return $ops[$comparisonOp]($item, $property, $value);
201201
}));
202-
if (is_object($collection)) {
202+
if (static::isNonArrayAccessObject($collection)) {
203203
$result = (object) $result;
204204
}
205205

@@ -300,6 +300,16 @@ public static function group($collection, $grouper, $saveKeys = false)
300300
return $result;
301301
}
302302

303+
protected static function isNonArrayAccessObject($collection)
304+
{
305+
return is_object($collection) && !($collection instanceof \ArrayAccess);
306+
}
307+
308+
protected static function isArrayLike($collection)
309+
{
310+
return is_array($collection) || $collection instanceof \ArrayAccess;
311+
}
312+
303313
////////////////////////////////////////////////////////////////////
304314
////////////////////////////// HELPERS /////////////////////////////
305315
////////////////////////////////////////////////////////////////////
@@ -321,10 +331,10 @@ protected static function internalSet(&$collection, $key, $value)
321331
$key = array_shift($keys);
322332

323333
// If we're dealing with an object
324-
if (is_object($collection)) {
334+
if (static::isNonArrayAccessObject($collection)) {
325335
$collection->$key = static::get($collection, $key, []);
326336
$collection = &$collection->$key;
327-
// If we're dealing with an array
337+
// If we're dealing with an array
328338
} else {
329339
$collection[$key] = static::get($collection, $key, []);
330340
$collection = &$collection[$key];
@@ -333,7 +343,7 @@ protected static function internalSet(&$collection, $key, $value)
333343

334344
// Bind final tree on the collection
335345
$key = array_shift($keys);
336-
if (is_array($collection)) {
346+
if (static::isArrayLike($collection)) {
337347
$collection[$key] = $value;
338348
} else {
339349
$collection->$key = $value;
@@ -357,16 +367,16 @@ protected static function internalRemove(&$collection, $key)
357367
}
358368

359369
// If we're dealing with an object
360-
if (is_object($collection)) {
370+
if (static::isNonArrayAccessObject($collection)) {
361371
$collection = &$collection->$key;
362-
// If we're dealing with an array
372+
// If we're dealing with an array
363373
} else {
364374
$collection = &$collection[$key];
365375
}
366376
}
367377

368378
$key = array_shift($keys);
369-
if (is_object($collection)) {
379+
if (static::isNonArrayAccessObject($collection)) {
370380
unset($collection->$key);
371381
} else {
372382
unset($collection[$key]);

src/Types/Strings.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
/**
1818
* Strings repository.
19-
19+
2020
*
2121
*@mixin StringsMethods
2222
*/

tests/MethodTest.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ public function testCanFindMethodsInClasses()
6767

6868
public function testCanThrowExceptionAtUnknownMethods()
6969
{
70-
$this->setExpectedException('BadMethodCallException',
71-
'The method Underscore\Types\Arrays::fuck does not exist');
70+
$this->setExpectedException(
71+
'BadMethodCallException',
72+
'The method Underscore\Types\Arrays::fuck does not exist'
73+
);
7274

7375
$test = Arrays::fuck($this);
7476
}

tests/Types/ArraysTest.php

+24-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
use Underscore\Underscore;
1515
use Underscore\UnderscoreTestCase;
1616

17+
class MyArrayAccess extends \ArrayObject
18+
{
19+
}
20+
1721
class ArraysTest extends UnderscoreTestCase
1822
{
1923
// Tests --------------------------------------------------------- /
@@ -61,6 +65,14 @@ public function testCanSetValues()
6165

6266
$this->assertEquals('ter', $array['foo']['bar']['bis']);
6367
$this->assertArrayHasKey('bar', $array);
68+
69+
$array = Arrays::set($this->arrayObjectMulti, 'a.d', 3);
70+
$this->assertEquals(2, Arrays::get($array, 'a.c'));
71+
$this->assertEquals(3, Arrays::get($array, 'a.d'));
72+
73+
$array = Arrays::set($this->arrayObjectMulti2, 'a.d', 3);
74+
$this->assertEquals(2, Arrays::get($array, 'a.c'));
75+
$this->assertEquals(3, Arrays::get($array, 'a.d'));
6476
}
6577

6678
public function testCanRemoveValues()
@@ -70,6 +82,14 @@ public function testCanRemoveValues()
7082
unset($matcher[0]['foo']);
7183

7284
$this->assertEquals($matcher, $array);
85+
86+
$array = Arrays::remove($this->arrayObjectMulti, 'a.b');
87+
$this->assertEquals(2, Arrays::get($array, 'a.c'));
88+
$this->assertEquals(null, Arrays::get($array, 'a.b'));
89+
90+
$array = Arrays::remove($this->arrayObjectMulti2, 'a.b');
91+
$this->assertEquals(2, Arrays::get($array, 'a.c'));
92+
$this->assertEquals(null, Arrays::get($array, 'a.b'));
7393
}
7494

7595
public function testCanRemoveMultipleValues()
@@ -103,7 +123,7 @@ public function testCanGetSumOfArray()
103123
$this->assertEquals(6, $array);
104124
}
105125

106-
public function testCanGetcountArray()
126+
public function testCanGetCountArray()
107127
{
108128
$array = Arrays::size([1, 2, 3]);
109129

@@ -637,6 +657,9 @@ public function testFilterBy()
637657
$this->assertCount(3, $f, 'Count should pick up groups which are explicitly set as null AND those which don\'t have the property at all');
638658
$this->assertContains('qux', Arrays::pluck($f, 'name'));
639659
$this->assertContains('flux', Arrays::pluck($f, 'name'));
660+
661+
$under = Arrays::filterBy($this->arrayObjectNumbers, 'value', 1, 'gt');
662+
$this->assertEquals([(object) ['value' => 2], (object) ['value' => 3]], $under);
640663
}
641664

642665
public function testFindBy()

tests/UnderscoreTestCase.php

+19
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ abstract class UnderscoreTestCase extends PHPUnit_Framework_TestCase
2323
['bar' => 'foo', 'bis' => 'ter'],
2424
];
2525
public $object;
26+
public $arrayObjectNumbers;
27+
public $arrayObjectMulti;
28+
public $arrayObjectMulti2;
2629

2730
/**
2831
* Restore data just in case.
@@ -35,5 +38,21 @@ public function setUp()
3538
(object) $this->arrayMulti[1],
3639
(object) $this->arrayMulti[2],
3740
];
41+
42+
$this->arrayObjectNumbers = new \ArrayObject();
43+
$this->arrayObjectNumbers[] = (object) ['value' => 1];
44+
$this->arrayObjectNumbers[] = (object) ['value' => 2];
45+
$this->arrayObjectNumbers[] = (object) ['value' => 3];
46+
47+
$this->arrayObjectMulti = new \ArrayObject();
48+
$this->arrayObjectMulti['a'] = new \ArrayObject();
49+
$this->arrayObjectMulti['a']['b'] = 1;
50+
$this->arrayObjectMulti['a']['c'] = 2;
51+
52+
$this->arrayObjectMulti2 = new \ArrayObject();
53+
$this->arrayObjectMulti2['a'] = [
54+
'b' => 1,
55+
'c' => 2,
56+
];
3857
}
3958
}

0 commit comments

Comments
 (0)