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

Commit f999eb5

Browse files
committed
Merge pull request #34 from localheinz/fix/anonymous
Fix: Ignore anonymous classes
2 parents e925fb2 + 8651341 commit f999eb5

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

src/ClassFileLocator.php

+11
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,17 @@ public function accept()
122122
if ($i > 0 && is_array($tokens[$i - 1]) && $tokens[$i - 1][0] === T_DOUBLE_COLON) {
123123
break;
124124
}
125+
126+
// ignore anonymous classes on PHP 7.1 and greater
127+
if ($i >= 2
128+
&& \is_array($tokens[$i - 1])
129+
&& T_WHITESPACE === $tokens[$i - 1][0]
130+
&& \is_array($tokens[$i - 2])
131+
&& T_NEW === $tokens[$i - 2][0]
132+
) {
133+
break;
134+
}
135+
125136
// no break
126137
case T_INTERFACE:
127138
// Abstract class, class, interface or trait found

test/ClassFileLocatorTest.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
* Zend Framework (http://framework.zend.com/)
44
*
55
* @link http://github.com/zendframework/zf2 for the canonical source repository
6-
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
6+
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
77
* @license http://framework.zend.com/license/new-bsd New BSD License
88
*/
99

1010
namespace ZendTest\File;
1111

1212
use Zend\File\ClassFileLocator;
1313
use Zend\File\Exception;
14+
use Zend\File\PhpClassFile;
1415

1516
/**
1617
* Test class for Zend\File\ClassFileLocator
@@ -147,4 +148,29 @@ public function testIterationShouldNotCountFQCNScalarResolutionConstantAsClass()
147148
$this->assertCount(1, $file->getClasses());
148149
}
149150
}
151+
152+
/**
153+
* @requires PHP 7.1
154+
*/
155+
public function testIgnoresAnonymousClasses()
156+
{
157+
$classFileLocator = new ClassFileLocator(__DIR__ . '/TestAsset/Anonymous');
158+
159+
$classFiles = \iterator_to_array($classFileLocator);
160+
161+
$this->assertCount(1, $classFiles);
162+
163+
$classNames = \array_reduce($classFiles, function (array $classNames, PhpClassFile $classFile) {
164+
return \array_merge(
165+
$classNames,
166+
$classFile->getClasses()
167+
);
168+
}, []);
169+
170+
$expected = [
171+
TestAsset\Anonymous\WithAnonymousClass::class,
172+
];
173+
174+
$this->assertEquals($expected, $classNames);
175+
}
150176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* Zend Framework (http://framework.zend.com/)
5+
*
6+
* @link https://github.com/zendframework/zend-file for the canonical source repository
7+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
8+
* @license https://github.com/zendframework/zend-file/blob/master/LICENSE.md New BSD License
9+
*/
10+
11+
namespace ZendTest\File\TestAsset\Anonymous;
12+
13+
final class WithAnonymousClass
14+
{
15+
private $anonymous;
16+
17+
public function __construct()
18+
{
19+
$this->anonymous = new class extends \stdClass {
20+
21+
};
22+
}
23+
}
24+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/**
4+
* Zend Framework (http://framework.zend.com/)
5+
*
6+
* @link https://github.com/zendframework/zend-file for the canonical source repository
7+
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
8+
* @license https://github.com/zendframework/zend-file/blob/master/LICENSE.md New BSD License
9+
*/
10+
11+
$anonymous = new class extends \stdClass {
12+
13+
};

0 commit comments

Comments
 (0)