Skip to content

Commit

Permalink
[5.6] class_uses_recursive() return parent class's trait first (#22537)
Browse files Browse the repository at this point in the history
* class_uses_recursive() return parent class's trait first

* fix cs
  • Loading branch information
zhwei authored and taylorotwell committed Dec 26, 2017
1 parent 5e2240d commit 1b4e49b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,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);
}

Expand Down
27 changes: 23 additions & 4 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,22 +699,32 @@ 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'));
Expand Down Expand Up @@ -845,6 +855,15 @@ class SupportTestClassTwo extends SupportTestClassOne
{
}

trait SupportTestTraitThree
{
}

class SupportTestClassThree extends SupportTestClassTwo
{
use SupportTestTraitThree;
}

class SupportTestArrayAccess implements ArrayAccess
{
protected $attributes = [];
Expand Down

0 comments on commit 1b4e49b

Please sign in to comment.