Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 328490 - [PHP 5.3] Adding use statement on wrong place
Summary: [PHP 5.3] Adding use statement on wrong place
Status: CLOSED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: PDT (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows XP
: P1 normal (vote)
Target Milestone: ---   Edit
Assignee: PHP Core CLA
QA Contact: Ilina Stefanova CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-10-22 11:50 EDT by Sylvia Tancheva CLA
Modified: 2020-05-14 10:17 EDT (History)
1 user (show)

See Also:


Attachments
patch (2.03 KB, patch)
2010-10-24 22:26 EDT, xu jiaxi CLA
no flags Details | Diff
patch (1.26 KB, patch)
2010-11-05 02:29 EDT, Zhongwei Zhao CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.