Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 20 | trait ApplicationTrait |
||
| 21 | { |
||
| 22 | private $_webroot; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string Title for the application used in different sections like Login screen |
||
| 26 | */ |
||
| 27 | public $siteTitle = 'LUYA Application'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string|boolean Set a token, which will be used to collect data from a central host, if you want to enable this feature. |
||
| 31 | * Use http://passwordsgenerator.net/ to create complex strings. When you have enabled this feature you can collect information's from |
||
| 32 | * all your hosts with `example.com/admin/remote?token=Sha1EncodedRemoteToken`. |
||
| 33 | */ |
||
| 34 | public $remoteToken = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string The directory where your webroot represents, this is basically used to find the webroot directory |
||
| 38 | * in the console mode, cause some importer classes need those variables. |
||
| 39 | */ |
||
| 40 | public $webrootDirectory = 'public_html'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string This value will be used as hostInfo when running console applications in urlManager. An example for using the hostInfo |
||
| 44 | * |
||
| 45 | * ```php |
||
| 46 | * 'consoleHostInfo' => 'https://luya.io' |
||
| 47 | * ``` |
||
| 48 | */ |
||
| 49 | public $consoleHostInfo; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string This value is used when declared for console request as urlManger baseUrl in order to enable urlHandling. If {{luya\web\traits\ApplicationTrait::$consoleHostInfo}} |
||
| 53 | * is defined, consoleBaseUrl will use `/` as default value. The base url is the path where the application is running after hostInfo like |
||
| 54 | * |
||
| 55 | * ```php |
||
| 56 | * 'consoleBaseUrl' => '/luya-kickstarter' |
||
| 57 | * ``` |
||
| 58 | * |
||
| 59 | * But in the most cases when the website is online the baseUrl is `/` which is enabled by default when {{luya\web\traits\ApplicationTrait::$consoleHostInfo}} is defined. |
||
| 60 | */ |
||
| 61 | public $consoleBaseUrl; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array Add tags to the TagParser class. Example |
||
| 65 | * |
||
| 66 | * ```php |
||
| 67 | * 'tags' => [ |
||
| 68 | * 'foobar' => ['class' => '\app\tags\FoobarTag'], |
||
| 69 | * ], |
||
| 70 | * ``` |
||
| 71 | */ |
||
| 72 | public $tags = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var array Can override the localisation value used for php internal `setlocale()` method for specific language. For example |
||
| 76 | * the language is de but the it should use the locale charset `de_CH.utf` (locale -a will return all locales installed on the server) |
||
| 77 | * you can define them inside an array where key is the language and value the locale value to be used. |
||
| 78 | * |
||
| 79 | * ```php |
||
| 80 | * public $locales = [ |
||
| 81 | * 'de' => 'de_CH', |
||
| 82 | * 'en' => 'en_GB', |
||
| 83 | * ]; |
||
| 84 | * ``` |
||
| 85 | */ |
||
| 86 | public $locales = []; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Add trace info to luya application trait |
||
| 90 | */ |
||
| 91 | public function init() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Transform the $language into a locale sign to set php env settings. |
||
| 103 | * |
||
| 104 | * Example transform input `de` to `de_CH` when available $locales property. |
||
| 105 | * |
||
| 106 | * @param string $lang Find the locale POSIX for the provided $lang short code. |
||
| 107 | * @return string The localisation code for the provided lang short code. |
||
| 108 | */ |
||
| 109 | public function ensureLocale($lang) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Setter method ensures the locilations POSIX from {{ensureLocale}} for the provided lang |
||
| 139 | * and changes the Yii::$app->langauge and sets the `setlocale()` code from ensureLocale(). |
||
| 140 | * |
||
| 141 | * @param string $lang The language short code to set the locale for. |
||
| 142 | */ |
||
| 143 | public function setLocale($lang) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Get the package Installer |
||
| 152 | * @return \luya\base\PackageInstaller |
||
| 153 | */ |
||
| 154 | public function getPackageInstaller() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @inheritdoc |
||
| 165 | */ |
||
| 166 | protected function bootstrap() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Read only property which is used in cli bootstrap process to set the @webroot alias |
||
| 177 | * |
||
| 178 | * ```php |
||
| 179 | * Yii::setAlias('@webroot', $app->webroot); |
||
| 180 | * ``` |
||
| 181 | */ |
||
| 182 | public function getWebroot() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Add additional core components to the yii2 base core components. |
||
| 193 | */ |
||
| 194 | public function luyaCoreComponents() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get an array with all modules which are an instance of the `luya\base\Module`. |
||
| 204 | * |
||
| 205 | * @return \luya\base\Module |
||
| 206 | */ |
||
| 207 | View Code Duplication | public function getApplicationModules() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Return a list with all registered frontend modules except 'luya' and 'cms'. This is needed in the module block. |
||
| 222 | * |
||
| 223 | * @return \luya\base\Module |
||
| 224 | */ |
||
| 225 | View Code Duplication | public function getFrontendModules() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Return all Admin Module Interface implementing modules. |
||
| 240 | * |
||
| 241 | * @return \luya\base\AdminModuleInterface |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function getAdminModules() |
|
| 255 | } |
||
| 256 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: