40

I recently started working extensively with Magento and Code sniffer, and I would like to know which standards I should follow.

I tried Zend coding standards, but I don't want to use the 80 line limit, and it's not used in the core either.

After that, I tried CS2 standards, but that doesn't work either because of the _ in various functions, and class names, which have an _ in them, and don't have namespaces.

So, is there another standard that the core code complies 100% with ? If not, shouldn't magento have its own coding standards? It has its own stackexchange site, some well defined coding standards would be welcome to be used by extension developers.

21

You can also try ECG Magento Code Sniffer Coding Standard

18

Magento best practices are quite good described by Josh Pratt here. He suggests to adopt and follow the Zend Code Style Standards and I can only join him.

The maximal line length is not a strict requirement. However from readability point of view too long lines are not encouraged.

  • 2
    Thanks, I switched the ruleset.xml to 120 chars allowed per line so I don't get the pesky errors in my IDE :) – Vlad Preda Feb 11 '13 at 13:24
  • 1
    Here are the rules from Magento 2: raw.github.com/magento/magento2/master/dev/tests/static/…. Word is that it's very likely they use them on Magento 1.x. – Bob Brodie Aug 26 '13 at 16:40
  • 1
    Tim, do you think we should use the Magento code sniff? magento.stackexchange.com/a/8743/41 – kalenjordan Oct 1 '13 at 15:24
  • 1
    @BobBrodie - I'm managing Magento 1.x and I'm not aware of such changes coming to Magento 1.x. This would be a large and unnecessary change, leading to huge diffs for no reason. Magento 1 coding standard is ZF+, Magento 2 is PSR-1/2. Please see Zyava's answer for "official" sniffer. – Piotr Kaminski Dec 3 '13 at 21:34
12

Here are latest Magento marketplace coding standard for Magento1 and Magento2

MEQP2 For Magento2

MEQP1 For Magento1

https://github.com/magento/marketplace-eqp

  • 1
    Made this as the accepted answer since these are the official coding standards. Thanks @QaisarSatti – Vlad Preda Dec 7 '16 at 9:38
11

I'd like to submit that we should be using the Magento 2 code sniff as a standard for both Magento 1.X and 2.X: https://github.com/cobhimself/phpcs-magento-rules/blob/master/ruleset.xml

And here's essentially a mirror of that ruleset as a standalone repository: https://github.com/cobhimself/phpcs-magento-rules

  • 1
    I personally use: github.com/magento-ecg/coding-standard – B00MER Nov 13 '13 at 18:51
  • 2
    Ah good call. I think the link I posted was referenced in a Magento 2 github discussion...any idea if/how the one you posted differs? It's certainly a more authoritative github account. – kalenjordan Nov 14 '13 at 3:34
2

This post is a bit old (2008) but I guess that it's still valid:

Is Magento following the ZF coding standards? Yes

With few exceptions, such as:

  • short open tags are allowed in templates.
  • concatenation dots are not enclosed in spaces.
  • indentation for for long lines hard wraps should be always one soft tab forward.

Edit As Tim worte in comment there are no short open tags in Magento core templatesa and it's best not to use them anyway as they are not recommanded (see here).

But my advice to you... Since you are writing your own module use your own coding style. From my experience you will make less mistakes when writing code in your own style, that you are familiar with, than if you follow Magento coding style one day and e.g. Wordpress coding style the next just because you are writing a module for a different website project.

  • 2
    I strictly don't agree. Community extension have to be readable for others. – Foo Bar Feb 11 '13 at 12:40
  • 2
    And I never saw short open tags in Magento. – Foo Bar Feb 11 '13 at 12:43
  • 1
    Point of oppinion. Since I've had quite a few years of experience with programming on different projects in different programming languages at the same time I've never had problems reading well structured code no matter the coding style but when it comes to rappidly switching between the projects and writting code it is far more benifitial to have the same style across your own code than it is to keep coding style compatibility with others outside your team. – Domen Vrankar Feb 11 '13 at 12:49
  • @DomenVrankar isn't assuming your code style is well structured and readable also an opinion? Whereas the teams behind the likes of Zend have many people making the decision as to what the style is and is known worldwide. Just food for thought... – Tom Burman Sep 28 '15 at 13:44
  • I disagree too. – sv3n Feb 3 '18 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.