| Conditions | 14 |
| Paths | 42 |
| Total Lines | 57 |
| Code Lines | 33 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 24 | public function actionLoad() |
||
| 25 | { |
||
| 26 | echo "\033[36m Are you sure you want to load fixtures? Your database will be purged! [Y/N] \033[0m"; |
||
| 27 | |||
| 28 | $phpStdin = fopen("php://stdin", "r"); |
||
| 29 | $inputValue = fgets($phpStdin); |
||
| 30 | $purifiedLine = preg_replace('/[^A-Za-z0-9\-]/', '', $inputValue); |
||
| 31 | |||
| 32 | if (strtolower($purifiedLine) == 'n') { |
||
| 33 | echo "\033[34m Stopping the executing... Done. \033[0m \n"; |
||
| 34 | die; |
||
| 35 | } |
||
| 36 | |||
| 37 | if (empty($this->pathToFixtures) || !file_exists($this->pathToFixtures)) { |
||
| 38 | echo "\033[33m There is no file with fixtures to load! Make sure that you create file with fixtures, |
||
| 39 | or pass correct file name \033[0m \n"; |
||
| 40 | die; |
||
| 41 | } |
||
| 42 | |||
| 43 | // import models classes to make available create new instances |
||
| 44 | if (is_array($this->modelsFolder)) { |
||
| 45 | foreach ($this->modelsFolder as $folder) { |
||
| 46 | Yii::import($folder); //import each folder form array |
||
| 47 | } |
||
| 48 | } else { |
||
| 49 | Yii::import(empty($this->modelsFolder) ? 'application.models.*' : $this->modelsFolder); |
||
| 50 | } |
||
| 51 | |||
| 52 | $fixtures = require_once $this->pathToFixtures; // require that file with fixtures, will be array |
||
| 53 | $errorList = array(); |
||
| 54 | |||
| 55 | foreach ($fixtures as $modelClass => $instances) { // run through the array with fixtures |
||
| 56 | $modelClass::model()->deleteAll(); // removing old rows from database if database is not truncated |
||
| 57 | foreach ($instances as $key => $instance) { // go through all instances for certain model, and save it into db |
||
| 58 | $model = new $modelClass(); |
||
| 59 | $model->attributes = $instances[$key]; |
||
| 60 | if (!$model->save()) { // if model can't be saved append errors into error list array |
||
| 61 | $errorList[] = $model->getErrors(); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | if (!empty($errorList)) { // if error list not empty |
||
| 67 | echo "\033[31m Validation errors occurs during loading the fixtures, some fixtures wasn't loaded to database \033[0m \n |
||
| 68 | \033[33m The next errors occur \033[0m \n"; |
||
| 69 | foreach ($errorList as $errors) { // run over all errors and display error what occur during saving into db |
||
| 70 | foreach ($errors as $error) { |
||
| 71 | foreach ($error as $value) { |
||
| 72 | echo "\033[37;41m" . $value . "\033[0m \n"; //display error |
||
| 73 | } |
||
| 74 | } |
||
| 75 | } |
||
| 76 | die; |
||
| 77 | } |
||
| 78 | |||
| 79 | echo "\033[37;42m All fixtures loaded properly \033[0m \n"; |
||
| 80 | } |
||
| 81 | |||
| 95 |