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

<p>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.</p>

<p>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.</p>

<hr>

<p><strong>Update:</strong> We now have official <a href="https://github.com/pixelandtonic/CodingStandards" rel="nofollow">"P&amp;T Coding Standards"</a> which we recommend you to use, when writing Craft plugins.</p>

CC BY-SA 3.0
2014-06-20T22:05:01.773Z
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
CC BY-SA 3.0
2014-06-20T22:01:32.583Z
2014-09-21T17:12:17.490Z
2014-09-21T17:12:17.490Z
Brad Bell
<p>Another option: Use <a href="http://editorconfig.org/" rel="nofollow">http://editorconfig.org/</a></p>

<p>If you're using Sublime Text, you can use <a href="https://github.com/sindresorhus/editorconfig-sublime" rel="nofollow">https://github.com/sindresorhus/editorconfig-sublime</a> to enforce those rules.</p>

CC BY-SA 3.0
2014-06-20T18:45:54.890Z
Cool, I had no idea about editorconfig. Thanks @Tim!
Matt Stein
CC BY-SA 3.0
2014-06-20T18:36:57.987Z
2014-06-20T18:36:57.987Z
Tim Kelty
<p>I don't know about any Craft specific-guide, but there are is a guide and code-sniffer for Yii 1:</p>

<ul>
<li><a href="https://github.com/yiisoft/yii/wiki/Core-framework-code-style" rel="nofollow">Core framework code style</a></li>
<li><a href="https://github.com/Ardem/yii-coding-standard" rel="nofollow">Yii Framework PHP CodeSniffer Coding Standard</a></li>
</ul>

CC BY-SA 3.0
2014-06-20T17:41:39.623Z
Thanks @VictorIn. You just reminded me that Ben Parizek's [Craft-Twig](https://github.com/BarrelStrength/Craft-Twig.tmbundle) Sublime bundle exists, too.
Matt Stein
CC BY-SA 3.0
2014-06-20T18:20:38.773Z
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. https://github.com/yiisoft/yii2-coding-standards
Wes Rice
CC BY-SA 3.0
2014-06-20T17:39:38.083Z
2014-06-20T17:39:38.083Z
Victor
<p>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. </p>

<p>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: )</p>

CC BY-SA 3.0
2014-06-20T20:25:48.643Z
2014-06-20T20:25:48.643Z
Selvin Ortiz
<p>I eventually figured out that I could use a code sniffer in Sublime Text, load up a <a href="https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer" rel="nofollow">CodeIgniter rule set</a> (not a Craft project, obviously), and see in real time where my formatting, spacing and naming conventions could be improved for consistency.</p>

<p>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.</p>

<plugin-development>
Are there coding standards I can follow when writing my Craft plugin?
# Are there coding standards I can follow when writing my Craft plugin?

(Asked by: username_0 on 2014-06-20)

I eventually figured out that I could use a code sniffer in Sublime Text, load up a [CodeIgniter rule set](https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer) (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.

**Tags:** plugin-development

---

## 4 answers

### Answer by username_5 on 2014-06-20. Score: 6 | Accepted answer

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: )


---

### Answer by username_1 on 2014-06-20. Score: 6

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.

---

**Update:** We now have official ["P&T Coding Standards"](https://github.com/pixelandtonic/CodingStandards) which we recommend you to use, when writing Craft plugins.

> **Comments:**
> **username_0**: 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.  

---

### Answer by username_2 on 2014-06-20. Score: 1

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.

> **Comments:**
> **username_0**: Cool, I had no idea about editorconfig. Thanks @Tim!  

---

### Answer by username_3 on 2014-06-20. Score: 1

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

* [Core framework code style](https://github.com/yiisoft/yii/wiki/Core-framework-code-style)
* [Yii Framework PHP CodeSniffer Coding Standard](https://github.com/Ardem/yii-coding-standard)

> **Comments:**
> **username_0**: Thanks @VictorIn. You just reminded me that Ben Parizek's [Craft-Twig](https://github.com/BarrelStrength/Craft-Twig.tmbundle) Sublime bundle exists, too.  
> **username_4**: 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. https://github.com/yiisoft/yii2-coding-standards  

---

