phpStorm class name generation

Jake B.

Well-known member
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 :)
 
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
{

}
 
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.
 
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:
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 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.
 
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

  • 2015-05-27_00-55-37.webp
    2015-05-27_00-55-37.webp
    80.6 KB · Views: 35
  • 2015-05-27_00-57-00.webp
    2015-05-27_00-57-00.webp
    15.9 KB · Views: 34
  • 2015-05-27_00-58-24.webp
    2015-05-27_00-58-24.webp
    16.1 KB · Views: 36
  • 2015-05-27_00-58-46.webp
    2015-05-27_00-58-46.webp
    45.5 KB · Views: 34
  • 2015-05-27_00-59-20.webp
    2015-05-27_00-59-20.webp
    13.7 KB · Views: 33
  • 2015-05-27_00-59-35.webp
    2015-05-27_00-59-35.webp
    27.2 KB · Views: 32
Top Bottom