• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

phpStorm class name generation

Jake B.

Well-known member
#1
Does anyone who uses phpStorm know of some way I can set it up so when I create something along the lines of library/X/Y/Z.php it will create it with a template along the lines of:
Code:
class X_Y_Z
{

}
No big deal if there is no way to do this since it only takes a matter of seconds to manually do it, just more curious what phpStorm is capable of :)
 

Chris D

XenForo developer
Staff member
#2
I don't think so.

It's easier with namespaces.

PhpStorm can infer the correct namespace automatically when they are used so when you go to New > PHP Class, all you usually have to do is enter the file name itself (e.g. ClassTest) and it will populate the namespace automatically (e.g. Test)

PHP:
<?php

namespace Test;

class ClassTest
{

}
 

Jake B.

Well-known member
#3
Does XenForo support namespaces in its autoloader? All of our Add-ons require PHP 5.3 anyways, so I have no problem starting to use namespaces if they're supported.
 

Liam W

Well-known member
#4
Does XenForo support namespaces in its autoloader? All of our Add-ons require PHP 5.3 anyways, so I have no problem starting to use namespaces if they're supported.
Yes, it was added a few versions ago. Can't remember which one, might have been before or after 1.2.

Some of my add-ons use namespaces.

For the class proxy, you have to extend XFCP_Class, like so:

PHP:
namespace LiamW\Test\Extend\Model;

class User extends XFCP_User
{
}
Oh, another thing, when adding the class in the listener, use the full class name with backslashes.

So, for above:

PHP:
namespace LiamW\Test;

class Listener
{
    public static function extendUserModel($class, array &$extend)
    {
        $extend[] = 'LiamW\Test\Extend\Model\User';
    }
}
Liam
 
Last edited:

lol768

Active member
#6
I normally use a helper file in conjunction with namespaces to improve auto-complete with PhpStorm. Maybe this will also be useful to others. Something like:

PHP:
<?php
if (false) {
    namespace Addon\ControllerPublic;
    class XFCP_Account extends \XenForo_ControllerPublic_Account {}
}
 

Liam W

Well-known member
#7
I normally use a helper file in conjunction with namespaces to improve auto-complete with PhpStorm. Maybe this will also be useful to others. Something like:

PHP:
<?php
if (false) {
    namespace Addon\ControllerPublic;
    class XFCP_Account extends \XenForo_ControllerPublic_Account {}
}
I do the same, without the additional namespace... no need for it. I just have the if(false) branch in the same file as the main extending class.
 

Yoskaldyr

Well-known member
#8
PhpStorm file templates can be used for auto generating headers of new class files.
I've modified a "\.WebIde90\config\fileTemplates\internal\PHP Class.php" file:
PHP:
<?php
#parse("PHP File Header.php")
#set ( $EXTENDS = "")
#if (${NAMESPACE})
 #set ( $NAMESPACE = $NAMESPACE.replace("\", "_") )
 #set ( $NAME = "${NAMESPACE}_${NAME}")
 #if ( $NAMESPACE.endsWith("_Extend"))
  #set ( $EXTENDS = " extends XFCP_${NAME}")
 #elseif ( $NAMESPACE.endsWith("_Model"))
  #set ( $EXTENDS = " extends XenForo_Model")
 #elseif ( $NAMESPACE.endsWith("_DataWriter"))
  #set ( $EXTENDS = " extends XenForo_DataWriter")
 #elseif ( $NAMESPACE.endsWith("_ControllerPublic"))
  #set ( $EXTENDS = " extends XenForo_ControllerPublic_Abstract")
 #elseif ( $NAMESPACE.endsWith("_ControllerAdmin"))
  #set ( $EXTENDS = " extends XenForo_ControllerAdmin_Abstract")
 #elseif ( $NAMESPACE.endsWith("_ControllerHelper"))
  #set ( $EXTENDS = " extends XenForo_ControllerHelper_Abstract")
 #elseif ( $NAMESPACE.endsWith("_ViewPublic"))
  #set ( $EXTENDS = " extends XenForo_ViewPublic_Base")
 #elseif ( $NAMESPACE.endsWith("_AdminAdmin"))
  #set ( $EXTENDS = " extends XenForo_ViewAdmin_Base")
 #end
#end

class ${NAME}${EXTENDS}
{
}
And after that in the "New PHP Class" form a namespace field auto renamed to a correct xenforo class.
How it works in the attached screenshots.
 

Attachments