Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I eventually figured out that I could use a code sniffer in Sublime Text, load up a CodeIgniter rule set (not a Craft project, obviously), and see in real time where my formatting, spacing and naming conventions could be improved for consistency.

Is there an equivalent standard for Craft/Yii? I'd like to start forming good habits, and the Sublime Text part is a bonus but not required—I'm most interested in whatever standards there are to reference.

share|improve this question

4 Answers 4

up vote 4 down vote accepted

I would strongly suggest that you follow PSR-2 since that is what most of the craft source code adheres to and it is a pretty good standard for general PHP development. Just be aware that craft itself won't pass PSR-2 linting because there are places where it deviates from the standard, two of which are namespacing (craft has a global namespace) and indentation (tabs over spaces) but that is totally fine in my opinion.

Writing clean, readable, intuitive, and testable code should be the end goal of any developer and if that means you have to violate a standards rule here or there, go for it: )

share|improve this answer
    
Thank you Selvin! –  Matt Stein Jun 21 at 14:31

We plan on adding official "Craft Plugin Code Guidelines" recommendations once the plugin store is out of the gate.

In the meantime, Selvin is correct. We aren't 100% PSR-2 compliant, but it's easy enough to look in the craft/app folder and see what our coding style is.

The Yii 1.0 coding style is, IMO, quite awful, so I wouldn't recommend following that. Yii 2.0, on the other hand is PSR-2 compliant and we'll re-address Craft's guidelines when we're ready to integration Yii 2.0 into Craft.

share|improve this answer
    
Cool, thanks Brad. I've been generally trying to mimic what I see in the land of /app, it's just nice to have the extra reinforcement from an automated tool like phpcs. –  Matt Stein Jun 20 at 22:05

I don't know about any Craft specific-guide, but there are is a guide and code-sniffer for Yii 1:

share|improve this answer
3  
Thanks @VictorIn. You just reminded me that Ben Parizek's Craft-Twig Sublime bundle exists, too. –  Matt Stein Jun 20 at 17:41
2  
For what it's worth, Yii2 is adopting PSR-2 coding standards (for the most part). Yii2 is a ways down the road as far as Craft implementation is concerned, but it's worth keeping in the back of your mind. github.com/yiisoft/yii2-coding-standards –  Wes Rice Jun 20 at 18:20

Another option: Use http://editorconfig.org/

If you're using Sublime Text, you can use https://github.com/sindresorhus/editorconfig-sublime to enforce those rules.

share|improve this answer
    
Cool, I had no idea about editorconfig. Thanks @Tim! –  Matt Stein Jun 20 at 18:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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