From c3c3db36173eb6d6f4b793a426a8472c8be5c234 Mon Sep 17 00:00:00 2001 From: George Zhao Date: Thu, 29 Mar 2018 16:15:18 +1100 Subject: [PATCH 1/3] Make Arrays::set Array::remove work for ArrayAccess --- src/Methods/CollectionMethods.php | 24 +++++++++++++++++------- src/Types/Strings.php | 2 +- tests/Types/ArraysTest.php | 25 ++++++++++++++++++++++++- tests/UnderscoreTestCase.php | 19 +++++++++++++++++++ 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/Methods/CollectionMethods.php b/src/Methods/CollectionMethods.php index b5d7396..9a01ae8 100644 --- a/src/Methods/CollectionMethods.php +++ b/src/Methods/CollectionMethods.php @@ -114,7 +114,7 @@ public static function setAndGet(&$collection, $key, $default = null) public static function remove($collection, $key) { // Recursive call - if (is_array($key)) { + if (static::isArrayLike($key)) { foreach ($key as $k) { static::internalRemove($collection, $k); } @@ -137,7 +137,7 @@ public static function pluck($collection, $property) }, (array) $collection); // Convert back to object if necessary - if (is_object($collection)) { + if (static::isNonArrayAccessObject($collection)) { $plucked = (object) $plucked; } @@ -199,7 +199,7 @@ public static function filterBy($collection, $property, $value, $comparisonOp = return $ops[$comparisonOp]($item, $property, $value); })); - if (is_object($collection)) { + if (static::isNonArrayAccessObject($collection)) { $result = (object) $result; } @@ -300,6 +300,16 @@ public static function group($collection, $grouper, $saveKeys = false) return $result; } + protected static function isNonArrayAccessObject($collection) + { + return is_object($collection) && !($collection instanceof \ArrayAccess); + } + + protected static function isArrayLike($collection) + { + return is_array($collection) || $collection instanceof \ArrayAccess; + } + //////////////////////////////////////////////////////////////////// ////////////////////////////// HELPERS ///////////////////////////// //////////////////////////////////////////////////////////////////// @@ -321,7 +331,7 @@ protected static function internalSet(&$collection, $key, $value) $key = array_shift($keys); // If we're dealing with an object - if (is_object($collection)) { + if (static::isNonArrayAccessObject($collection)) { $collection->$key = static::get($collection, $key, []); $collection = &$collection->$key; // If we're dealing with an array @@ -333,7 +343,7 @@ protected static function internalSet(&$collection, $key, $value) // Bind final tree on the collection $key = array_shift($keys); - if (is_array($collection)) { + if (static::isArrayLike($collection)) { $collection[$key] = $value; } else { $collection->$key = $value; @@ -357,7 +367,7 @@ protected static function internalRemove(&$collection, $key) } // If we're dealing with an object - if (is_object($collection)) { + if (static::isNonArrayAccessObject($collection)) { $collection = &$collection->$key; // If we're dealing with an array } else { @@ -366,7 +376,7 @@ protected static function internalRemove(&$collection, $key) } $key = array_shift($keys); - if (is_object($collection)) { + if (static::isNonArrayAccessObject($collection)) { unset($collection->$key); } else { unset($collection[$key]); diff --git a/src/Types/Strings.php b/src/Types/Strings.php index 0376a0e..45dd549 100644 --- a/src/Types/Strings.php +++ b/src/Types/Strings.php @@ -16,7 +16,7 @@ /** * Strings repository. - + * *@mixin StringsMethods */ diff --git a/tests/Types/ArraysTest.php b/tests/Types/ArraysTest.php index 0a69cbf..39b3abe 100644 --- a/tests/Types/ArraysTest.php +++ b/tests/Types/ArraysTest.php @@ -14,6 +14,10 @@ use Underscore\Underscore; use Underscore\UnderscoreTestCase; +class MyArrayAccess extends \ArrayObject +{ +} + class ArraysTest extends UnderscoreTestCase { // Tests --------------------------------------------------------- / @@ -61,6 +65,14 @@ public function testCanSetValues() $this->assertEquals('ter', $array['foo']['bar']['bis']); $this->assertArrayHasKey('bar', $array); + + $array = Arrays::set($this->arrayObjectMulti, 'a.d', 3); + $this->assertEquals(2, Arrays::get($array, 'a.c')); + $this->assertEquals(3, Arrays::get($array, 'a.d')); + + $array = Arrays::set($this->arrayObjectMulti2, 'a.d', 3); + $this->assertEquals(2, Arrays::get($array, 'a.c')); + $this->assertEquals(3, Arrays::get($array, 'a.d')); } public function testCanRemoveValues() @@ -70,6 +82,14 @@ public function testCanRemoveValues() unset($matcher[0]['foo']); $this->assertEquals($matcher, $array); + + $array = Arrays::remove($this->arrayObjectMulti, 'a.b'); + $this->assertEquals(2, Arrays::get($array, 'a.c')); + $this->assertEquals(null, Arrays::get($array, 'a.b')); + + $array = Arrays::remove($this->arrayObjectMulti2, 'a.b'); + $this->assertEquals(2, Arrays::get($array, 'a.c')); + $this->assertEquals(null, Arrays::get($array, 'a.b')); } public function testCanRemoveMultipleValues() @@ -103,7 +123,7 @@ public function testCanGetSumOfArray() $this->assertEquals(6, $array); } - public function testCanGetcountArray() + public function testCanGetCountArray() { $array = Arrays::size([1, 2, 3]); @@ -637,6 +657,9 @@ public function testFilterBy() $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'); $this->assertContains('qux', Arrays::pluck($f, 'name')); $this->assertContains('flux', Arrays::pluck($f, 'name')); + + $under = Arrays::filterBy($this->arrayObjectNumbers, 'value', 1, 'gt'); + $this->assertEquals([(object) ['value' => 2], (object) ['value' => 3]], $under); } public function testFindBy() diff --git a/tests/UnderscoreTestCase.php b/tests/UnderscoreTestCase.php index b8ec3fd..92c51cc 100644 --- a/tests/UnderscoreTestCase.php +++ b/tests/UnderscoreTestCase.php @@ -23,6 +23,9 @@ abstract class UnderscoreTestCase extends PHPUnit_Framework_TestCase ['bar' => 'foo', 'bis' => 'ter'], ]; public $object; + public $arrayObjectNumbers; + public $arrayObjectMulti; + public $arrayObjectMulti2; /** * Restore data just in case. @@ -35,5 +38,21 @@ public function setUp() (object) $this->arrayMulti[1], (object) $this->arrayMulti[2], ]; + + $this->arrayObjectNumbers = new \ArrayObject(); + $this->arrayObjectNumbers[] = (object) ['value' => 1]; + $this->arrayObjectNumbers[] = (object) ['value' => 2]; + $this->arrayObjectNumbers[] = (object) ['value' => 3]; + + $this->arrayObjectMulti = new \ArrayObject(); + $this->arrayObjectMulti['a'] = new \ArrayObject(); + $this->arrayObjectMulti['a']['b'] = 1; + $this->arrayObjectMulti['a']['c'] = 2; + + $this->arrayObjectMulti2 = new \ArrayObject(); + $this->arrayObjectMulti2['a'] = [ + 'b' => 1, + 'c' => 2, + ]; } } From efa7ff99757dc6d560000c7e7d6ac8d5cc73ed1b Mon Sep 17 00:00:00 2001 From: George Zhao Date: Thu, 29 Mar 2018 16:43:14 +1100 Subject: [PATCH 2/3] Update PHP-CS-Fixer --- .php_cs | 40 +++++++++++++------------------ composer.json | 5 +++- src/Methods/CollectionMethods.php | 4 ++-- tests/MethodTest.php | 6 +++-- 4 files changed, 27 insertions(+), 28 deletions(-) diff --git a/.php_cs b/.php_cs index 8076e18..508413e 100644 --- a/.php_cs +++ b/.php_cs @@ -1,10 +1,5 @@ in(['config', 'src', 'tests']); +$finder = PhpCsFixer\Finder::create()->in(['config', 'src', 'tests']); $header = <<< EOF This file is part of Underscore.php @@ -14,21 +9,20 @@ For the full copyright and license information, please view the LICENSE file that was distributed with this source code. EOF; -HeaderCommentFixer::setHeader($header); +$fixer = new \PhpCsFixer\Fixer\Comment\HeaderCommentFixer; -return Config::create() - ->level(FixerInterface::SYMFONY_LEVEL) - ->fixers([ - 'ereg_to_preg', - 'header_comment', - 'multiline_spaces_before_semicolon', - 'ordered_use', - 'php4_constructor', - 'phpdoc_order', - 'short_array_syntax', - 'short_echo_tag', - 'strict', - 'strict_param', - ]) - ->setUsingCache(true) - ->finder($finder); +return PhpCsFixer\Config::create() + ->setRules([ + '@PSR2' => true, + // 'strict_param' => true, + 'array_syntax' => ['syntax' => 'short'], + 'header_comment' => [ + 'header' => $header + ], + 'ordered_imports' => [], + // 'ereg_to_preg' => true, + 'phpdoc_order' => true, + // 'no_php4_constructor' => true, + ]) + ->setUsingCache(true) + ->setFinder($finder); diff --git a/composer.json b/composer.json index 8c1a7ff..332528a 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "patchwork/utf8": "^1.2" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^1.10", + "friendsofphp/php-cs-fixer": "^2.11", "phpunit/phpunit": "^4.8" }, "autoload": { @@ -35,5 +35,8 @@ "scripts": { "test": ["php-cs-fixer fix --dry-run", "phpunit --verbose"], "lint": "php-cs-fixer fix" + }, + "config": { + "sort-packages": true } } diff --git a/src/Methods/CollectionMethods.php b/src/Methods/CollectionMethods.php index 9a01ae8..f267538 100644 --- a/src/Methods/CollectionMethods.php +++ b/src/Methods/CollectionMethods.php @@ -334,7 +334,7 @@ protected static function internalSet(&$collection, $key, $value) if (static::isNonArrayAccessObject($collection)) { $collection->$key = static::get($collection, $key, []); $collection = &$collection->$key; - // If we're dealing with an array + // If we're dealing with an array } else { $collection[$key] = static::get($collection, $key, []); $collection = &$collection[$key]; @@ -369,7 +369,7 @@ protected static function internalRemove(&$collection, $key) // If we're dealing with an object if (static::isNonArrayAccessObject($collection)) { $collection = &$collection->$key; - // If we're dealing with an array + // If we're dealing with an array } else { $collection = &$collection[$key]; } diff --git a/tests/MethodTest.php b/tests/MethodTest.php index 4bb35dd..46b8c34 100644 --- a/tests/MethodTest.php +++ b/tests/MethodTest.php @@ -67,8 +67,10 @@ public function testCanFindMethodsInClasses() public function testCanThrowExceptionAtUnknownMethods() { - $this->setExpectedException('BadMethodCallException', - 'The method Underscore\Types\Arrays::fuck does not exist'); + $this->setExpectedException( + 'BadMethodCallException', + 'The method Underscore\Types\Arrays::fuck does not exist' + ); $test = Arrays::fuck($this); } From b851eb6143d3bd6c6182e743d9529c79c1fc2e80 Mon Sep 17 00:00:00 2001 From: George Zhao Date: Thu, 29 Mar 2018 16:47:29 +1100 Subject: [PATCH 3/3] No longer support PHP 5.5 or below --- .travis.yml | 2 -- composer.json | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 405ed57..3ce2d36 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,6 @@ sudo: false # Setup build matrix language: php php: - - 5.4 - - 5.5 - 5.6 - 7.0 - hhvm diff --git a/composer.json b/composer.json index 332528a..910a31f 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": ">=5.4.0", + "php": ">=5.6.0", "doctrine/inflector": "^1.0", "patchwork/utf8": "^1.2" },