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

Commit 0aa4d35

Browse files
committed
Don't ignore files with use function
Previously `ClassFileLocator` would enter a "function context" when it encountered `T_FUNCTION` token, causing it to ignore subsequent classes (thinking they are defined in a function), ultimately leading to the file being ignored because ``ClassFileLocator`` thought there's no (named) classes defined in the file. Now it will additionally check that the `T_FUNCTION` token is not preceded by `T_USE`, fixing the bug.
1 parent da33213 commit 0aa4d35

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#44](https://github.com/zendframework/zend-file/pull/44) fixes an issue where ClassFileLocator would skip the file (otherwise valid class file) containing `use function`
2626

2727
## 2.8.0 - 2018-04-25
2828

src/ClassFileLocator.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ public function accept()
126126
}
127127
break;
128128
case T_FUNCTION:
129-
$inFunctionDeclaration = true;
129+
// `use function` should not enter function context
130+
if ($i < 2 || ! is_array($tokens[$i - 2]) || $tokens[$i - 2][0] !== T_USE) {
131+
$inFunctionDeclaration = true;
132+
}
130133
break;
131134
case T_TRAIT:
132135
case T_CLASS:

test/ClassFileLocatorTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,17 @@ public function testIgnoresMethodsNamedAfterKeywords()
196196

197197
$this->assertEquals($expected, $classNames, '', 0.0, 10, true);
198198
}
199+
200+
public function testIterationFindsClassInAFileWithUseFunction()
201+
{
202+
$locator = new ClassFileLocator(__DIR__);
203+
$found = false;
204+
205+
foreach ($locator as $file) {
206+
if (preg_match('/ContainsUseFunction\.php$/', $file->getFilename())) {
207+
$found = true;
208+
}
209+
}
210+
$this->assertTrue($found, "Failed to find a file that contains `use function`");
211+
}
199212
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace ZendTest\File\TestAsset;
4+
5+
use function strlen;
6+
7+
class ContainsUseFunction
8+
{
9+
10+
}

0 commit comments

Comments
 (0)