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

Conversation

localheinz
Copy link
Member

@localheinz localheinz commented Jan 11, 2017

This PR

  • asserts that anonymous classes are ignored
  • ignores anonymous classes

Follows #29.

@localheinz localheinz force-pushed the fix/anonymous branch 4 times, most recently from eaea370 to 484e5c8 Compare January 11, 2017 14:43
@weierophinney
Copy link
Member

@localheinz Please rebase to resolve conflicts, which will trigger a new build as well. Thanks!

@localheinz
Copy link
Member Author

@weierophinney

Sorted!

@localheinz
Copy link
Member Author

And thanks a lot for reviewing these on such a short notice, I appreciate it!

Since https://github.com/zendframework/modules.zendframework.com (where I am a maintainer) is kind of dead, I wouldn't mind getting involved as a maintainer with a different Zend project, please let me know if there's interest!


// ignore anonymous classes on PHP 7.1 and greater
if (PHP_VERSION_ID >= 70100
&& $i > 2
Copy link
Member Author

Choose a reason for hiding this comment

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

Ha, should this be $i > 1, actually?

Copy link
Member

Choose a reason for hiding this comment

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

You tell me; haven't played with tokenizing anonymous classes yet. :) What happens in the tests?

Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

Noted a few changes. Additionally, in the files changed (but not introduced), update the copyright in the file-level docblock to reference 2017.

Thanks!


// ignore anonymous classes on PHP 7.1 and greater
if (PHP_VERSION_ID >= 70100
&& $i > 2
Copy link
Member

Choose a reason for hiding this comment

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

You tell me; haven't played with tokenizing anonymous classes yet. :) What happens in the tests?

&& \is_array($tokens[$i - 1])
&& $tokens[$i - 1][0] === T_WHITESPACE
&& \is_array($tokens[$i - 2])
&& $tokens[$i - 2][0] === T_NEW
Copy link
Member

Choose a reason for hiding this comment

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

Use yoda conditions for the constant comparisons here.

*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
Copy link
Member

Choose a reason for hiding this comment

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

Three changes:

*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
Copy link
Member

Choose a reason for hiding this comment

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

Same notes on this docblock as for the previous.

@localheinz localheinz force-pushed the fix/anonymous branch 2 times, most recently from ee58139 to b91ff16 Compare January 11, 2017 15:12

// ignore anonymous classes on PHP 7.1 and greater
if (PHP_VERSION_ID >= 70100
&& $i >= 2
Copy link
Member Author

Choose a reason for hiding this comment

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

Modified this condition

-&& $i > 2
+&& $i >+ 2

as the index doesn't need to be greater than 2, but at least 2 (since we try to access the element at $i - 2).

@weierophinney
Copy link
Member

Will merge once I verify the build with Travis. Currently their running on a bit of a delay, due to the queue.

@localheinz
Copy link
Member Author

Thanks a ton, @weierophinney!

@weierophinney
Copy link
Member

@localheinz Already seeing errors on all jobs run so far: https://travis-ci.org/zendframework/zend-file/builds/190986651


namespace ZendTest\File\TestAsset\Anonymous;

$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!

@localheinz
Copy link
Member Author

@weierophinney

Amended the previous commit:

diff --git a/src/ClassFileLocator.php b/src/ClassFileLocator.php
index 82619a2..767fee4 100644
--- a/src/ClassFileLocator.php
+++ b/src/ClassFileLocator.php
@@ -124,8 +124,7 @@ class ClassFileLocator extends FilterIterator
                     }

                     // ignore anonymous classes on PHP 7.1 and greater
-                    if (PHP_VERSION_ID >= 70100
-                        && $i >= 2
+                    if ($i >= 2
                         && \is_array($tokens[$i - 1])
                         && T_WHITESPACE === $tokens[$i - 1][0]
                         && \is_array($tokens[$i - 2])

👍

@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

Um... && to start the conditional...? Also, will $i >= 2 only ever happen on PHP 7.1, or will this work on any version?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ha, hehe!

Well, I guess trying to run

$tokens   = token_get_all($contents);

when $contents contains usage of anonymous classes will already fail on a PHP version less than PHP 7.1, what do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, no, works fine on all PHP versions, see https://3v4l.org/0enag!

@weierophinney weierophinney merged commit 8651341 into zendframework:master Jan 11, 2017
weierophinney added a commit that referenced this pull request Jan 11, 2017
weierophinney added a commit that referenced this pull request Jan 11, 2017
weierophinney added a commit that referenced this pull request Jan 11, 2017
weierophinney added a commit that referenced this pull request Jan 11, 2017
Forward port #34

Conflicts:
	CHANGELOG.md
@localheinz
Copy link
Member Author

Thanks a ton, @weierophinney!

@localheinz localheinz deleted the fix/anonymous branch January 11, 2017 17:21
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants