This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 209402 - Automatically add namespace to a new PHP class
Summary: Automatically add namespace to a new PHP class
Status: VERIFIED FIXED
Alias: None
Product: php
Classification: Unclassified
Component: Project (show other bugs)
Version: 7.2
Hardware: All All
: P2 normal with 5 votes (vote)
Assignee: Tomas Mysik
URL:
Keywords:
: 152507 (view as bug list)
Depends on: 226157
Blocks:
  Show dependency tree
 
Reported: 2012-03-10 04:08 UTC by Monomachus
Modified: 2013-03-12 08:14 UTC (History)
5 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Screenshot of the "new file wizard" and a newly created file. (157.47 KB, image/png)
2013-02-26 07:23 UTC, naitsirch
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Monomachus 2012-03-10 04:08:10 UTC
Please add the default namespace to the PHP templates, so a user would have the namespace already in the file. I've chosen Symfony2 framework here because it seems more... guessable what to namespace to have in a new framework, but in general this option would very nice play with PHP.
Comment 1 Tomas Mysik 2012-03-12 09:19:53 UTC
Well, the problem here is - where to get the namespace? From the wizard (for the new file)?

Thanks for reporting.
Comment 2 naitsirch 2012-03-22 13:06:23 UTC
That would be a great feature!

Maybe Netbeans could look in the folder where the file should be created or in it's parent folders and guess the namespace from the classes there, when the wizard (for the new file) is opened. In a text field the guessed namespace could be outputed.

A further idea would be to set a "default" namespace in the folders (maybe in the wizard, too). In this case the same guessing could be used as mentioned above.

Thanks for your great work! (to all NB developers)
Comment 3 naitsirch 2012-03-22 13:12:36 UTC
> A further idea would be to set a "default" namespace in the folders (maybe
> in the wizard, too). In this case the same guessing could be used as
> mentioned above.

A little bit clearer: The default namespace could be a property of the folder guessed and editable in the 'New Folder' wizard.
Comment 4 Monomachus 2012-04-09 12:47:28 UTC
Yeah like naitsirch said we need to have the guessed namespace in the File Wizard, so if user don't like it he could change it.
That would be the first step to do.
Than we could have an autocompletion (or something like 10 recently used namespaces) from all the namespaces already used in the projects files, so the user could choose from there. But again the first step would need to be done first, and than we could do something further.
Comment 5 vmattila 2013-01-15 20:56:15 UTC
Namespace could be guessed from the folder structure, as defined in PHP's PSR-0 standard (see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md ).

At least if the folder, where a new class/file is created, already contains other PHP files with namespace, Netbeans could guess that the newly created class will be in the same namespace.
Comment 6 magnetik 2013-01-16 09:52:53 UTC
+1 for PSR0 support for namespaces
Comment 7 Tomas Mysik 2013-01-16 10:22:00 UTC
I will try to look at it for the next release.

Thanks.
Comment 8 Tomas Mysik 2013-02-15 08:13:30 UTC
*** Bug 152507 has been marked as a duplicate of this bug. ***
Comment 9 Tomas Mysik 2013-02-15 09:20:32 UTC
Fixed.

http://hg.netbeans.org/web-main/rev/13c832aff786
Comment 10 Tomas Mysik 2013-02-15 12:38:34 UTC
Minor improvement.

http://hg.netbeans.org/web-main/rev/0fa171a946e5
Comment 11 Quality Engineering 2013-02-16 01:53:16 UTC
Integrated into 'main-golden', will be available in build *201302152300* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/13c832aff786
User: Tomas Mysik <tmysik@netbeans.org>
Log: #209402 - Automatically add namespace to a new PHP class
Comment 12 naitsirch 2013-02-18 07:14:37 UTC
How is it resolved, now?
Is the namespace added automatically to the new PHP file or do one have to include some special code to the file templates?
Comment 13 Tomas Mysik 2013-02-25 07:13:00 UTC
(In reply to comment #12)
> How is it resolved, now?
> Is the namespace added automatically to the new PHP file or do one have to
> include some special code to the file templates?

Try it in the dev build; the new file wizard has been improved.

Thanks.
Comment 14 naitsirch 2013-02-26 07:23:17 UTC
Created attachment 131882 [details]
Screenshot of the "new file wizard" and a newly created file.

Hi. In the wizard the namespace has been detected correctly, but the created file does not contain the namespace declaration. How can I achieve that? Do I have to add something to the PHP Code Templates?

Great work so far ;)
Comment 15 Tomas Mysik 2013-02-26 07:25:47 UTC
@naitsirch: You have your own template; recreate it from the bundled one (it contains code for namespace).

Thanks.
Comment 16 naitsirch 2013-02-26 12:36:02 UTC
> recreate it from the bundled one (it contains code for namespace).

I see. Now it works :)
Maybe this could be documented anywhere. Otherwise people won't see this great feature, when they install the new version and Netbeans copies the templates from an earlier NetBeans installation.

This is the directive to insert the guessed namespace in the PHP file template:

<#if namespace?? && namespace?length &gt; 0>
namespace ${namespace};
</#if>
Comment 17 Tomas Mysik 2013-02-26 12:43:01 UTC
(In reply to comment #16)
> Maybe this could be documented anywhere.

Frankly, nearly nobody reads documentation... Moreover, it is perfectly OK that NetBeans will _not_ change any custom (user defined) template.

Thanks.
Comment 18 Ondrej Brejla 2013-02-26 12:46:25 UTC
The most correct way how to inform users is to blog about it ;)
Comment 19 Tomas Mysik 2013-02-26 13:39:22 UTC
(In reply to comment #18)
> The most correct way how to inform users is to blog about it ;)

Agreed, and this is going to happen soon ;)
Comment 20 Vladimir Riha 2013-03-12 08:14:13 UTC
Verified, if target folder contains classes with some namespaces, these are offered in New File wizard

Product Version: NetBeans IDE Dev (Build 201303112300)
Java: 1.7.0_15; Java HotSpot(TM) Client VM 23.7-b01
Runtime: Java(TM) SE Runtime Environment 1.7.0_15-b03
System: Linux version 3.2.0-38-generic-pae running on i386; UTF-8; en_US (nb)