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

Fix: Ignore anonymous classes #34

Merged
merged 2 commits into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is, on the other hand, should the ClassFileLocator actually accept a PHP file that doesn't contain any classes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that it would ignore it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify: the intent of the class is to find files that declare named classes. It was originally developed as a utility for creating class maps for autoloading. Anonymous classes, as they are not named, would fall outside that criteria.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will update later, @weierophinney!


};