Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 328490

Summary: [PHP 5.3] Adding use statement on wrong place
Product: z_Archived Reporter: Sylvia Tancheva <silviya>
Component: PDTAssignee: PHP Core <php.core-inbox>
Status: CLOSED FIXED QA Contact: Ilina Stefanova <ilina.s>
Severity: normal    
Priority: P1 CC: ganoro
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
patch
none
patch none

Description Sylvia Tancheva CLA 2010-10-22 11:50:02 EDT
Use code:
<?php
/**
 *
 */
namespace NS\Registry;

class MyException extends \Exception {
}
;

class Registry {
	
	protected static $_registry = array ();
	
	static public function register($key, $value, $isPersistent = false) {
		if (isset ( self::$_registry ['$key'] ))
			throw new My| <-- Code Assist here, choose MyException
	}
}

As a result:
- use directive is inserted right after the php opening tag (and the comments if any), but before the namespace declaration, which results in compilation error.
- btw, the use directive isn’t needed in this case at all

<?php
/**
 *
 */
use NS\Registry\MyException;
namespace NS\Registry;
Comment 1 Zhongwei Zhao CLA 2010-10-23 01:26:33 EDT
first I remove the ; after
class MyException extends \Exception {
}

it works as expected

so I think the issue is about parser,so I checked file php_ast_parser.cup,I found(from line 371) 

			if (!(statement instanceof EmptyStatement) && lastStatement instanceof NamespaceDeclaration && !((NamespaceDeclaration)lastStatement).isBracketed()) {
				((NamespaceDeclaration)lastStatement).addStatement(statement);
			} else {
				sList.add(statement);
			}
			
if I change the above code to :

			if (lastStatement instanceof NamespaceDeclaration && !((NamespaceDeclaration)lastStatement).isBracketed()) {
				((NamespaceDeclaration)lastStatement).addStatement(statement);
			} else {
				sList.add(statement);
			}
			
the issue has gone!

so probably it is intentional.
Comment 2 xu jiaxi CLA 2010-10-24 22:26:29 EDT
Created attachment 181607 [details]
patch

Hi zhao,
You can look at this patch.
Comment 3 Zhongwei Zhao CLA 2010-11-05 02:29:52 EDT
Created attachment 182452 [details]
patch

fix the bug in php_ast_parser.cup
Comment 4 Zhongwei Zhao CLA 2010-11-05 02:34:20 EDT
Hi Xu,

I have reviewed your patch,and it is really very nice.sometimes the parser could not parse the php file correctly,and if use your patch we do not need to worry about this when we call method UseStatementInjector.getCurrentNamespace.
Comment 5 xu jiaxi CLA 2010-11-05 02:41:17 EDT
Yes, I think this patch can help #267813 too.
Comment 6 Zhongwei Zhao CLA 2011-04-18 11:41:24 EDT
change status
Comment 7 Ilina Stefanova CLA 2011-07-22 04:36:10 EDT
Verified on Windows XP, 32 bit with Zend Studio 9.0.0, build 179.