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

Commit 888fd48

Browse files
committed
Merge branch 'hotfix/34'
Close #34
2 parents e925fb2 + 7789b0d commit 888fd48

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.7.1 - TBD
5+
## 2.7.1 - 2017-01-11
66

77
### Added
88

@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#34](https://github.com/zendframework/zend-file/pull/34) ensures that
22+
anonymous classes are ignored by the `ClassFileLocator` when identifying files
23+
with class declarations.
2224

2325
## 2.7.0 - 2016-04-28
2426

src/ClassFileLocator.php

Lines changed: 11 additions & 0 deletions
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

Lines changed: 27 additions & 1 deletion
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
}
Lines changed: 24 additions & 0 deletions
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+
Lines changed: 13 additions & 0 deletions
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)