cancel
Showing results for 
Search instead for 
Did you mean: 

Importing and aliases vs. fully qualified namespaces

0 Kudos

Importing and aliases vs. fully qualified namespaces

Feature request from ldusan84, posted on GitHub Dec 25, 2014

I think that Magento2 should import namespaces and use aliases instead of using fully qualified namespaces all over the place.

Here is an example.

What I suggest is that instead of:

throw new \Magento\Framework\Model\Exception(
    __('Invalid login or password.'),
    self::EXCEPTION_INVALID_EMAIL_OR_PASSWORD
);

We could have:

use \Magento\Framework\Model\Exception as MagentoException
/* ... */
throw new MagentoException(
    __('Invalid login or password.'),
    self::EXCEPTION_INVALID_EMAIL_OR_PASSWORD
);

Here are the benefits from this:

  1. With importing namespaces keyword at the first few lines of a class you actually get a nice documentation of your class dependencies. Everything that class uses is in those first few lines. It makes dependencies explicit.

  2. If something changes in the future, you would only need to change it in one place, in import declaration and the alias remains. With fully qualified namespaces you would need to find them everywhere in class and change.

  3. Less confusion if you have similar namespaces, you can give them different aliases.

  4. All major frameworks use importing/aliases over fully qualified namespaces.

Let me know what you think.

4 Comments
apiuser
New Member
Status changed to: Investigating
 
apiuser
New Member

Comment from orlangur, posted on GitHub Dec 25, 2014

I totally second the importing approach :+1:

use \Magento\Framework\Model\Exception as MagentoException Less confusion if you have similar namespaces, you can give them different aliases.

I would rather forbid aliases usage or allow them only for exceptional cases. Proper class name must contain all data necessary to understand the purpose of class. There are a lot of names with directory-per-word approach and would be better to rename classes than introduce aliases each time you want to use them, for instance: Magento\Framework\Model\Exception -> Magento\Framework\Model\ModelException Magento\Catalog\Model\Resource\Product\Collection -> Magento\Catalog\Model\Resource\ProductCollection So that classes could be imported without aliasing.

You may notice that there are a lot of code in Magento2 using short names already. But in code which was not changed for a long time there are still a lot of fully-qualified class names. Would be nice to replace fully-qualified class names with imports all over the code and enforce short name usage with static test (for example, check that T_NS_SEPARATOR token does not appear outside the namespace and use statements; or some more sophisticated check if we want to allow relative class names).

What tool would you suggest for such transformation? The closest ready-to-use utility I found so far is PHP Refactoring Browser, but "optimize use statements" feature here does not work as expected: type-hinted class names in constructor/other methods, PHPDoc blocks are not updated.

So, except the IDE abilities which I didn't analyze, I would build such script using one of:

apiuser
New Member

Comment from ldusan84, posted on GitHub Dec 25, 2014

Thanks @orlangur for the comment.

I just picked that Exception class for no reason, and yes I should have made a better alias, but for me even importing without alias is still better than fully qualified name. Maybe aliases could be used only if we have two classes with the same name.

To tell you the truth, I am not sure which tool would be the best to do this automatically. I know some IDEs can do that, but I am not sure how reliable are they. I guess it would be hard to find a reliable tool.

apiuser
New Member

Comment from verklov, posted on GitHub Jan 05, 2015

Internal ticket: MAGETWO-32347