Skip to content

Instantly share code, notes, and snippets.

@mzeis
Last active February 8, 2016 07:48
Show Gist options
  • Save mzeis/464cf110cc407d6c2a28 to your computer and use it in GitHub Desktop.
Save mzeis/464cf110cc407d6c2a28 to your computer and use it in GitHub Desktop.
Magento Coding Standard
<?php
/**
* Full-form, standard PHP tag (don't use a short tag).
* The closing tag must be omitted.
*/
/**
* Opening curly brackets for classes and methods are put on the next line.
* Indentation for the next level is 4 spaces (no tabs).
*
* Class names correspond to the folder structure (exception: controllers) so the Magento auto loader can find the
* files.
*
* Lines should be limited to 120 characters. This is a soft limit but if possible stick to it.
* Remove trailing spaces.
* Both can be handled by IDEs like PhpStorm automatically.
*
* Comments are written in phpdoc format.
*/
class LimeSoda_ModuleName_Model_Example extends Varien_Object
{
/**
* Constants are written all upper case, separated with underscores.
*
* @var string
*/
const EXAMPLE_CONSTANT = 'foobar';
/**
* Private class members are prefixed with an underscore.
*
* @var string
*/
private $_example = '';
/**
* Protected class members are prefixed with an underscore.
*
* @var array
*/
protected $_exampleTwo = array();
/**
* Private methods are prefixed with an underscore.
* Always provide annotations for method parameters and return values.
*
* @return void
*/
private function _example()
{
// ...
}
/**
* Protected methods are prefixed with an underscore.
* Always provide annotations for method parameters and return values.
* Note the camel casing of the method name.
*
* @param string $var1
* @param array $var2
* @return string
*/
protected function _exampleTwo($var1, array $var2 = array())
{
return 'example';
}
/**
* Public methods are not prefixed.
*
* Note that annotations for multiple types for parameters and return values can be specified using "|" although
* this should be avoided when possible.
*
* @param Varien_Object $var1
* @param array $var2
* @param string|int $var3
* @return void|null
*/
public function exampleThree(Varien_Object $var1, array $var2 = array(), $var3 = '')
{
/**
* Spaces before / after comparison operators.
* Opening curly brackets on the same line.
*
* Space between the brackets and the curly bracket.
*/
if ($var3 === 'test') {
$foo = 'bar';
return null;
}
/**
* Same as 'if': spaces before after operators, curly brackets on the same line.
*/
foreach ($var2 as $key => $value) {
$foo = 'bar';
}
/**
* Same as 'if' and 'foreach'.
*/
switch (Mage::helper('limesoda')->getValue()) {
case 'a':
$this->_example();
break;
case 'b':
$this->_exampleTwo('foo');
break;
default:
// do nothing
}
}
/**
* Always use Magento methods to initiate classes, access parameters etc.
*
* @return void
*/
public function magentoMethods()
{
$helper = Mage::helper('limesoda');
$model = Mage::getModel('limesoda/example');
$collection = Mage::getModel('limesoda/example')->getCollection();
/**
* Get a request parameter
*/
$foo = Mage::app()->getRequest()->getParam('foo');
/**
* Get a $_POST value
*/
$bar = Mage::app()->getRequest()->getPost('bar');
/**
* Find out if the request is a GET, POST, ... parameter.
*/
if (Mage::app()->getRequest()->isPost()) {
//
}
/**
* Set session value.
*/
Mage::getSingleton('core/session')->setFoo('bar');
/**
* Set a cookie.
*/
Mage::getSingleton('core/cookie')->set('name', 'value', 86400, '/');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment