BantenprovUserSeeder::orangeText()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Database\Seeder;
4
use \Bantenprov\Sekolah\Models\Bantenprov\Sekolah\AdminSekolah;
5
6
/**
7
 * Usage : 
8
 * [1] $ composer dump-autoload -o
9
 * [2] $ php artisan db:seed --class=UserSeeder
10
 */
11
12
class BantenprovUserSeeder extends Seeder
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    /* text color */
15
    protected $RED     ="\033[0;31m";
16
    protected $CYAN    ="\033[0;36m";
17
    protected $YELLOW  ="\033[1;33m";
18
    protected $ORANGE  ="\033[0;33m"; 
19
    protected $PUR     ="\033[0;35m";
20
    protected $GRN     ="\e[32m";
21
    protected $WHI     ="\e[37m";
22
    protected $NC      ="\033[0m";
23
24
    /* File name */
25
    /* location : /databse/seeds/file_name.csv */
26
    protected $fileName = "BantenprovUserSeeder.csv";
27
28
    /* text info : default (true) */
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
    protected $textInfo = true;
30
31
    /* model class */
32
    protected $model;
33
34
    /* __construct */
35
    public function __construct(){
36
37
        $this->model = new App\User;
38
39
    }
40
41
    /**
42
     * Run the database seeds.
43
     *
44
     * @return void
45
     */
46
    public function run()
47
    {        
48
        $this->insertData();
49
    }
50
51
    /* function insert data */
52
    protected function insertData()
53
    {
54
        /* silahkan di rubah sesuai kebutuhan */
55
        foreach($this->readCSV() as $data){ 
56
            $password['p1'] = rand(1376123,999234999);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$password was never initialized. Although not strictly required by PHP, it is generally a good practice to add $password = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
57
            $password['p2'] = rand(785482,9785482);            
0 ignored issues
show
Bug introduced by
The variable $password does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
58
59
            $admin_sekolah = $this->model->create([
60
                //'id'    => $data['id'],
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
                'name'  => strtolower($data['name']),
62
                'email' => strtolower($data['email']),
63
                'password' => bcrypt($password['p1'].$password['p2'])
64
            ]);
65
66
            if($data['sekolah_id'] != 0){
67
                // attach role to user
68
                $admin_sekolah->attachRole(5);
69
70
                //  create admin sekolah
71
                
72
                AdminSekolah::create([
73
                    'sekolah_id' => $data['sekolah_id'],
74
                    'admin_sekolah_id' => $admin_sekolah->id,
75
                    'user_id' => '1',
76
                ]);
77
            }elseif($data['sekolah_id'] == 0){
78
                $admin_sekolah->attachRole(1);
79
            }
80
81
            if($this->textInfo){                
82
                echo "============[Account]============\n";
83
                
84
                $this->orangeText('name : ').$this->greenText(strtolower($data['name']));
85
                echo"\n";
86
                $this->orangeText('email : ').$this->greenText(strtolower($data['email']));
87
                echo"\n";
88
                $this->orangeText('password : ').$this->greenText($password['p1'].$password['p2']);
89
                echo"\n";
90
                echo "============[Account]============\n\n";
91
            }
92
            
93
        }
94
95
96
        $this->greenText('[ SEEDER DONE ]');
97
        echo"\n\n";
98
    }
99
100
    /* text color: orange */
101
    protected function orangeText($text)
102
    {    
103
        printf($this->ORANGE.$text.$this->NC);
104
    }
105
106
    /* text color: green */
107
    protected function greenText($text)
108
    {    
109
        printf($this->GRN.$text.$this->NC);
110
    }
111
112
    /* function read CSV file */
113
    protected function readCSV()
114
    {
115
116
        $file = fopen(database_path("seeds/".$this->fileName), "r");
117
        $all_data = array();
118
        $row = 1;
0 ignored issues
show
Unused Code introduced by
$row is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
        while(($data = fgetcsv($file, 1000, ",")) !== FALSE){            
120
            $all_data[] = ['name' => $data[0], 'email' => $data[1], 'sekolah_id' => $data[2]];
121
        }        
122
        fclose($file);
123
124
        return  $all_data;
125
    }
126
}
127