From 93f137400c634f96aa639b96c0e893422d6782ad Mon Sep 17 00:00:00 2001 From: zhwei Date: Tue, 26 Dec 2017 16:07:23 +0800 Subject: [PATCH 1/2] class_uses_recursive() return parent class's trait first --- src/Illuminate/Support/helpers.php | 2 +- tests/Support/SupportHelpersTest.php | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Support/helpers.php b/src/Illuminate/Support/helpers.php index 63154d58aa99..d72dd1681924 100755 --- a/src/Illuminate/Support/helpers.php +++ b/src/Illuminate/Support/helpers.php @@ -373,7 +373,7 @@ function class_uses_recursive($class) $results = []; - foreach (array_merge([$class => $class], class_parents($class)) as $class) { + foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) { $results += trait_uses_recursive($class); } diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index a883c8c17210..84267413401c 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -691,22 +691,31 @@ public function testLast() public function testClassUsesRecursiveShouldReturnTraitsOnParentClasses() { - $this->assertEquals([ - 'Illuminate\Tests\Support\SupportTestTraitOne' => 'Illuminate\Tests\Support\SupportTestTraitOne', + $this->assertSame([ 'Illuminate\Tests\Support\SupportTestTraitTwo' => 'Illuminate\Tests\Support\SupportTestTraitTwo', + 'Illuminate\Tests\Support\SupportTestTraitOne' => 'Illuminate\Tests\Support\SupportTestTraitOne', ], class_uses_recursive('Illuminate\Tests\Support\SupportTestClassTwo')); } - public function testClassUsesRecursiveAcceptsObject() { - $this->assertEquals([ - 'Illuminate\Tests\Support\SupportTestTraitOne' => 'Illuminate\Tests\Support\SupportTestTraitOne', + $this->assertSame([ 'Illuminate\Tests\Support\SupportTestTraitTwo' => 'Illuminate\Tests\Support\SupportTestTraitTwo', + 'Illuminate\Tests\Support\SupportTestTraitOne' => 'Illuminate\Tests\Support\SupportTestTraitOne', ], class_uses_recursive(new SupportTestClassTwo)); } + public function testClassUsesRecursiveReturnParentTraitsFirst() + { + $this->assertSame([ + 'Illuminate\Tests\Support\SupportTestTraitTwo' => 'Illuminate\Tests\Support\SupportTestTraitTwo', + 'Illuminate\Tests\Support\SupportTestTraitOne' => 'Illuminate\Tests\Support\SupportTestTraitOne', + 'Illuminate\Tests\Support\SupportTestTraitThree' => 'Illuminate\Tests\Support\SupportTestTraitThree', + ], + class_uses_recursive(SupportTestClassThree::class)); + } + public function testArrayAdd() { $this->assertEquals(['surname' => 'Mövsümov'], Arr::add([], 'surname', 'Mövsümov')); @@ -768,6 +777,15 @@ class SupportTestClassTwo extends SupportTestClassOne { } +trait SupportTestTraitThree +{ +} + +class SupportTestClassThree extends SupportTestClassTwo +{ + use SupportTestTraitThree; +} + class SupportTestArrayAccess implements ArrayAccess { protected $attributes = []; From 8f28fc4ea7c3709c21959de9f8f9b60bc0fcc134 Mon Sep 17 00:00:00 2001 From: zhwei Date: Tue, 26 Dec 2017 16:10:52 +0800 Subject: [PATCH 2/2] fix cs --- tests/Support/SupportHelpersTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index 84267413401c..aab12fcd3a93 100755 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -697,6 +697,7 @@ public function testClassUsesRecursiveShouldReturnTraitsOnParentClasses() ], class_uses_recursive('Illuminate\Tests\Support\SupportTestClassTwo')); } + public function testClassUsesRecursiveAcceptsObject() { $this->assertSame([