AccountController::actionUpdateName()   B
last analyzed

Complexity

Conditions 7
Paths 19

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 19
nop 1
dl 0
loc 20
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 13.01.2018
6
 */
7
8
namespace app\commands;
9
10
11
use app\models\Account;
12
use yii\console\Controller;
13
use yii\console\ExitCode;
14
use yii\helpers\Console;
15
use yii\helpers\StringHelper;
16
17
class AccountController extends Controller
18
{
19
    /**
20
     * Update account usernames.
21
     * format: username1_from,username1_to1,name1;username2_from,username2_to,name2;username3_from,username3_to,name3
22
     *
23
     * @param $names
24
     * @return int
25
     * @throws \Throwable
26
     * @throws \yii\db\StaleObjectException
27
     */
28
    public function actionUpdateName($names)
29
    {
30
        $rows = StringHelper::explode($names, ';', true, true);
31
        foreach ($rows as $row) {
32
            $username = StringHelper::explode($row);
33
            if (count($username) != 3) {
34
                $this->stdout("Wrong format!\n", Console::BG_RED);
35
            }
36
            $account = Account::findOne(['username' => $username['0']]);
37
            if ($account) {
38
                if ($username['1']) {
39
                    $account->username = $username['1'];
40
                }
41
                if ($username['2']) {
42
                    $account->name = $username['2'];
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
                }
44
                if (!$account->update()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $account->update() of type false|integer is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
45
                    echo Console::errorSummary($account);
46
47
                    return ExitCode::DATAERR;
48
                }
49
50
            }
51
        }
52
    }
53
}