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

Commit

Permalink
Merge branch 'hotfix/34' into develop
Browse files Browse the repository at this point in the history
Forward port #34

Conflicts:
	CHANGELOG.md
  • Loading branch information
weierophinney committed Jan 11, 2017
2 parents e8e8a0c + 7789b0d commit 0d977c4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ All notable changes to this project will be documented in this file, in reverse

- Nothing.

## 2.7.1 - TBD
## 2.7.1 - 2017-01-11

### Added

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

### Fixed

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

## 2.7.0 - 2016-04-28

Expand Down
11 changes: 11 additions & 0 deletions src/ClassFileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,17 @@ public function accept()
if ($i > 0 && is_array($tokens[$i - 1]) && $tokens[$i - 1][0] === T_DOUBLE_COLON) {
break;
}

// ignore anonymous classes on PHP 7.1 and greater
if ($i >= 2
&& \is_array($tokens[$i - 1])
&& T_WHITESPACE === $tokens[$i - 1][0]
&& \is_array($tokens[$i - 2])
&& T_NEW === $tokens[$i - 2][0]
) {
break;
}

// no break
case T_INTERFACE:
// Abstract class, class, interface or trait found
Expand Down
28 changes: 27 additions & 1 deletion test/ClassFileLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2005-2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\File;

use Zend\File\ClassFileLocator;
use Zend\File\Exception;
use Zend\File\PhpClassFile;

/**
* Test class for Zend\File\ClassFileLocator
Expand Down Expand Up @@ -147,4 +148,29 @@ public function testIterationShouldNotCountFQCNScalarResolutionConstantAsClass()
$this->assertCount(1, $file->getClasses());
}
}

/**
* @requires PHP 7.1
*/
public function testIgnoresAnonymousClasses()
{
$classFileLocator = new ClassFileLocator(__DIR__ . '/TestAsset/Anonymous');

$classFiles = \iterator_to_array($classFileLocator);

$this->assertCount(1, $classFiles);

$classNames = \array_reduce($classFiles, function (array $classNames, PhpClassFile $classFile) {
return \array_merge(
$classNames,
$classFile->getClasses()
);
}, []);

$expected = [
TestAsset\Anonymous\WithAnonymousClass::class,
];

$this->assertEquals($expected, $classNames);
}
}
24 changes: 24 additions & 0 deletions test/TestAsset/Anonymous/WithAnonymousClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
* @link https://github.com/zendframework/zend-file for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-file/blob/master/LICENSE.md New BSD License
*/

namespace ZendTest\File\TestAsset\Anonymous;

final class WithAnonymousClass
{
private $anonymous;

public function __construct()
{
$this->anonymous = new class extends \stdClass {

};
}
}

13 changes: 13 additions & 0 deletions test/TestAsset/Anonymous/with-anonymous-class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/**
* Zend Framework (http://framework.zend.com/)
*
* @link https://github.com/zendframework/zend-file for the canonical source repository
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-file/blob/master/LICENSE.md New BSD License
*/

$anonymous = new class extends \stdClass {

};

0 comments on commit 0d977c4

Please sign in to comment.