moodev/php-weasel — pull request #42 by JonathanO (2013-05-27 14:30:07+00:00)
Title: Fix for issue #41 and namespace parsing

Issue #41 is more exciting than anticipated. It seems that the fix for PHP bug 55156 merely masked the real problem under many circumstances.

The real problem is that a call to token_get_all(), which we need to do to parse the header of files in order to extract use statements, updates several internal compiler globals. The next time the Zend Compiler comes long to compile a file it starts off with the compiler globals still in the state our token_get_all() left them.

If we did a token_get_all() on something containing a docblock comment this is left in the doc_comment compiler global. The next time the compiler is invoked this comment will find itself associated with whatever the first thing the compiler saw that it thinks needs a docblock (providing it hasn't already seen another docblock.) If the first thing it runs into is a class declaration (with no doc comment of it's own), that class gains our spare comment, and future calls to getDocComment() for that class return said comment.

There are a few tokens which would cause the compiler to clear the doc_comment global. Since the fix for 55156 the namespace declaration was one of them. As, in modern PHP, namespace is quite likely to feature at the top of any file, the problem is hidden. Unfortunately if you have some old non-namespaced code around, this can lead to annotations being attached to classes which do not really have them.

*sigh*