Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit a306dc0

Browse files
committed
Resolve issues with traits in ClassScanner
- Only the first use statement would be accounted for when getting lists of traits, due to unnecessary break statements at the end of the loop. Fixes #58 - If one of the methods touched by this commit, such as getTraitNames(), or getTraits(), which calls getTraitNames(), was called as the first action on this class, no traits would be returned, due to the fact that scan() was not called at the start of the method. Fixes #57
1 parent cf5eb51 commit a306dc0

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/Scanner/ClassScanner.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,8 @@ public function getTraits()
547547
*/
548548
public function getTraitNames()
549549
{
550+
$this->scan();
551+
550552
$return = [];
551553
foreach ($this->infos as $info) {
552554
if ($info['type'] !== 'use') {
@@ -562,7 +564,6 @@ public function getTraitNames()
562564
$return[] = $traitName;
563565
}
564566
}
565-
break;
566567
}
567568

568569
return $return;
@@ -575,6 +576,8 @@ public function getTraitNames()
575576
*/
576577
public function getTraitAliases()
577578
{
579+
$this->scan();
580+
578581
$return = [];
579582
foreach ($this->infos as $info) {
580583
if ($info['type'] !== 'use') {
@@ -598,7 +601,6 @@ public function getTraitAliases()
598601
$return[$alias['alias']] = $trait . '::' . $method;
599602
}
600603
}
601-
break;
602604
}
603605

604606
return $return;
@@ -612,6 +614,8 @@ public function getTraitAliases()
612614
*/
613615
protected function getVisibilityForAlias($aliasName)
614616
{
617+
$this->scan();
618+
615619
$return = null;
616620
foreach ($this->infos as $info) {
617621
if ($info['type'] !== 'use') {
@@ -632,7 +636,6 @@ protected function getVisibilityForAlias($aliasName)
632636
}
633637
}
634638
}
635-
break;
636639
}
637640

638641
return $return;
@@ -645,6 +648,8 @@ protected function getVisibilityForAlias($aliasName)
645648
*/
646649
protected function getBlockedTraitMethods()
647650
{
651+
$this->scan();
652+
648653
$return = [];
649654
foreach ($this->infos as $info) {
650655
if ($info['type'] !== 'use') {
@@ -668,7 +673,6 @@ protected function getBlockedTraitMethods()
668673
$return[] = $trait . '::' . $method;
669674
}
670675
}
671-
break;
672676
}
673677

674678
return $return;

test/Scanner/ClassScannerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public function testClassScannerCanScanClassThatUsesTraits()
202202
$class->getTraitAliases();
203203
$this->assertContains('ZendTest\Code\TestAsset\BarTrait', $traitNames);
204204
$this->assertContains('ZendTest\Code\TestAsset\FooTrait', $traitNames);
205+
$this->assertContains('ZendTest\Code\TestAsset\BazTrait', $traitNames);
205206
}
206207

207208
/**

test/TestAsset/BazTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @link http://github.com/zendframework/zf2 for the canonical source repository
6+
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license http://framework.zend.com/license/new-bsd New BSD License
8+
*/
9+
10+
namespace ZendTest\Code\TestAsset;
11+
12+
trait BazTrait
13+
{
14+
public function baz()
15+
{
16+
}
17+
}

test/TestAsset/TestClassUsesTraitSimple.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414
class TestClassUsesTraitSimple
1515
{
1616
use \ZendTest\Code\TestAsset\BarTrait, FooTrait;
17+
use BazTrait;
1718
}

0 commit comments

Comments
 (0)