docs/internals-fa/core-code-style.md
البته که نیاز نیست حتما این موارد رو در برنامههای خودتون رعایت کنید و می تونید در این مورد راحت باشید...
</p> <p dir='rtl'> میتونید برای دریافت پیکربندی CodeSniffer اینجا رو مطالعه کنید: https://github.com/yiisoft/yii2-coding-standards </p>https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md</p>
<p dir='rtl'> در فایلها باید از برچسبهای php?> و =?> استفاده شود.</p> <p dir='rtl'> در پایان هر فایل باید یک خط جدید (newline) داشته باشید.</p> <p dir='rtl'> encoding فایل برای کدهای php باید UTF-8 without BOM باشد.</p> <p dir='rtl'> به جای tab از 4 فضای خالی (space) استفاده کنید.</p> <p dir='rtl'> نام کلاسها باید به صورت StudlyCaps تعریف شوند.</p> <p dir='rtl'> ثابتهای داخل کلاس تماما باید با حروف بزرگ و گاهی با جداکننده "_" تعریف شوند.</p> <p dir='rtl'> نام متدها و پراپرتیها باید به صورت camelCase تعریف شوند.</p> <p dir='rtl'> پراپرتیهای خصوصی (private) باید با "_" شروع شوند.</p> <p dir='rtl'> همیشه از elseif جای else if استفاده کنید.</p>/**
* Documentation
*/
class MyClass extends \yii\base\BaseObject implements MyInterface
{
// code
}
<?php
class Foo
{
const VERSION = '1.0';
const DATE_APPROVED = '2012-06-01';
}
<?php
class Foo
{
public $publicProp1;
public $publicProp2;
protected $protectedProp;
private $_privateProp;
public function someMethod()
{
// ...
}
}
/**
* Documentation
*/
class Foo
{
/**
* Documentation
*/
public function bar()
{
// code
return $value;
}
}
/**
* Checks whether the IP is in subnet range
*
* @param string $ip an IPv4 or IPv6 address
* @param int $cidr the CIDR lendth
* @param string $range subnet in CIDR format e.g. `10.0.0.0/8` or `2001:af::/64`
* @return bool whether the IP is in subnet range
*/
private function inRange($ip, $cidr, $range)
{
// ...
}
public function save(Transaction $transaction, $argument2 = 100)
{
$transaction = new Connection; // bad
$argument2 = 200; // good
}
$str = 'Like this.';
$str1 = "Hello $username!";
$str2 = "Hello {$username}!";
$str3 = "Hello ${username}!";
$name = 'Yii' . ' Framework';
$sql = "SELECT *"
. "FROM `post` "
. "WHERE `id` = 121 ";
$arr = [3, 14, 15, 'Yii', 'Framework'];
$arr = [
3, 14, 15,
92, 6, $test,
'Yii', 'Framework',
];
$config = [
'name' => 'Yii',
'options' => ['usePHP' => true],
];
if ($event === null) {
return new Event();
}
if ($event instanceof CoolEvent) {
return $event->instance();
}
return null;
// the following is NOT allowed:
if (!$model && null === $event)
throw new Exception('test');
$result = $this->getResult();
if (empty($result)) {
return true;
} else {
// process result
}
$result = $this->getResult();
if (empty($result)) {
return true;
}
// process result
switch ($this->phpType) {
case 'string':
$a = (string) $value;
break;
case 'integer':
case 'int':
$a = (int) $value;
break;
case 'boolean':
$a = (bool) $value;
break;
default:
$a = null;
}
doIt(2, 3);
doIt(['a' => 'b']);
doIt('a', [
'a' => 'b',
'c' => 'd',
]);
// good
$n = 100;
$sum = array_reduce($numbers, function ($r, $x) use ($n) {
$this->doMagic();
$r += $x * $n;
return $r;
});
// bad
$n = 100;
$mul = array_reduce($numbers, function($r, $x) use($n) {
$this->doMagic();
$r *= $x * $n;
return $r;
});
<?php
/**
* Returns the errors for all attribute or a single attribute.
* @param string $attribute attribute name. Use null to retrieve errors for all attributes.
* @property array An array of errors for all attributes. Empty array is returned if no error.
* The result is a two-dimensional array. See [[getErrors()]] for detailed description.
* @return array errors for all attributes or the specified attribute. Empty array is returned if no error.
* Note that when returning errors for all attributes, the result is a two-dimensional array, like the following:
* ...
*/
public function getErrors($attribute = null)
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
/**
* Component is the base class that provides the *property*, *event* and *behavior* features.
*
* @include @yii/docs/base-Component.md
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Component extends \yii\base\BaseObject
/**
* Returns the list of attached event handlers for an event.
* You may manipulate the returned [[Vector]] object by adding or removing handlers.
* For example,
*
* ```
* $component->getEventHandlers($eventName)->insertAt(0, $eventHandler);
* ```
*
* @param string $name the event name
* @return Vector list of attached event handlers for the event
* @throws Exception if the event is not defined
*/
public function getEventHandlers($name)
{
if (!isset($this->_e[$name])) {
$this->_e[$name] = new Vector;
}
$this->ensureBehaviors();
return $this->_e[$name];
}