Passed
Pull Request — master (#12)
by Kris
05:47
created

writeIndex()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 56
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 31
nc 3
nop 0
dl 0
loc 56
rs 8.8017
c 2
b 0
f 0

How to fix   Long Method   

Long Method

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:

1
<?php
2
require_once __DIR__ .'/../vendor/autoload.php';
3
use Kristuff\Mishell\Console;
4
5
Console::clear();
6
printLoader();
7
goIndex();
8
9
function goIndex()
10
{
11
    printHeader();
12
    printIndex();
13
    askIndex();
14
}
15
16
function printLoader()
17
{
18
19
    Console::log();
20
    Console::log();
21
    Console::log('    '. Console::text("                _  ", 'black', 'white') . Console::text("  ____  _          _ _    ", 'magenta', 'white'));
22
    Console::log('    '. Console::text("      _ __ ___ (_) ", 'black', 'white') . Console::text(" / ___|| |__   ___| | |   ", 'magenta', 'white'));
23
    Console::log('    '. Console::text("     | '_ ` _ \| | ", 'black', 'white') . Console::text(" \___ \| '_ \ / _ \ | |   ", 'magenta', 'white'));
24
    Console::log('    '. Console::text("     | | | | | | | ", 'black', 'white') . Console::text("  ___) | | | |  __/ | |   ", 'magenta', 'white'));
25
    Console::log('    '. Console::text("     |_| |_| |_|_| ", 'black', 'white') . Console::text(" |____/|_| |_|\___|_|_|   ", 'magenta', 'white'));
26
    Console::log('    '. Console::text("                   ", 'black', 'white') . Console::text("                          ", 'magenta', 'white'));
27
    Console::log('    '. Console::text("                   ", 'black', 'white') . Console::text("        Version 1.0       ", 'black', 'white'));
28
    Console::log('    '. Console::text("                   ", 'black', 'white') . Console::text("        © 2020 Kristuff   ", 'black', 'white'));
29
    Console::log('    '. Console::text("                   ", 'black', 'white') . Console::text("                          ", 'magenta', 'white'));
30
    Console::log();
31
    Console::log();
32
 
33
    $pad = '    ';
34
35
    // fake progress message 
36
    for ($i=0 ; $i<=100 ; $i++) {
37
38
        // Overwrite progress message. 
39
        Console::relog($pad.Console::text('Loading... [', 'white').
40
                            Console::text($i .'%', 'green').
41
                            Console::text('] completed', 'white'));
42
43
        // wait for a while, so we see the animation
44
        usleep(15000); 
45
    }
46
    // Overwrite progress message. 
47
    Console::relog($pad.Console::text('Done!', 'white', 'green', 'underline').str_pad(' ', 150));
48
    usleep(45000); 
49
    Console::clear();
50
}
51
52
function getIndex()
53
{
54
    $index = [];
55
56
57
    
58
    $index[1] =   ['Styles',        'How to get available basic styles',                'demo.styles.php'];
59
    $index[2] =   ['Colors',        'How to get available foreground colors',           'demo.colors.php'];
60
    $index[3] =   ['Backgrounds',   'How to get available background colors',           'demo.bgcolors.php'];
61
   
62
    $index[11] =  ['Ask',           'How to ask? (get user input)',                     'demo.ask.php'];
63
    $index[12] =  ['Ask Number',    'How to ask and expect a number?',                  'demo.askint.php'];
64
    $index[13] =  ['Ask Password',  'How to ask a password? (do not print user input)', 'demo.askpassword.php'];
65
   
66
    $index[14] =  ['Table',         'How to print a table?',                            'demo.table.php'];
67
    $index[15] =  ['Bell',          'How to run the bell?',                             'demo.bell.php'];
68
    $index[16] =  ['Progress',      'How to output progress message?',                  'demo.progress.php'];
69
    $index[17] =  ['New window',    'How to open new/restore window?',                  'demo.window.php'];
70
71
//  $index[4] = ['Text',  'Console::text() overview',    'demo.log.php'];      
72
//  $index[5] = ['Log',   'Console::log() overview',     'demo.log.php'];      
73
//  $index[6] = ['ReLog', 'Console::relog() overview',   'demo.relog.php'];    
74
    $index[7] = ['Pad',             'Console::pad() overview',                          'demo.pad.php'];
75
    $index[8] = ['Size',  'How to get the number of columns and lines in terminal',     'demo.size.php'];
76
    $index[9] = ['BlueScreen',  'Really nice full screen centered message sample',      'demo.bluescreen.php'];
77
  
78
    return $index;
79
}
80
81
function printHeader()
82
{
83
    Console::clear();
84
    Console::log();
85
    Console::log(' '.Console::text('                                                   ', 'white', 'bold', 'underline'));
86
    Console::log(' '.Console::text('                                                   ', 'white', 'cyan', 'bold'));
87
    Console::log(' '.Console::text('   --- Interactive ~Mishell^^ Console sample ---   ', 'white', 'cyan', 'bold'));
88
    Console::log(' '.Console::text('                                                   ', 'white', 'cyan', 'bold', 'underline'));
89
    Console::log();
90
}
91
92
function printSampleHeader($index, $title)
93
{
94
    Console::log('      ' . Console::text(Console::pad('',      20, ' ', STR_PAD_BOTH), 'white', 'bold', 'underline'));
0 ignored issues
show
Bug Best Practice introduced by
The method Kristuff\Mishell\ShellColoredPrinter::pad() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

94
    Console::log('      ' . Console::text(Console::/** @scrutinizer ignore-call */ pad('',      20, ' ', STR_PAD_BOTH), 'white', 'bold', 'underline'));
Loading history...
95
    Console::log('      ' . Console::text(Console::pad('',      20, ' ', STR_PAD_BOTH), 'white', 'yellow', 'bold'));
96
    Console::log('      ' . Console::text(Console::pad($index .' - ' . $title,  20, ' ', STR_PAD_BOTH), 'white', 'yellow', 'bold'));
97
    Console::log('      ' . Console::text(Console::pad('',      20, ' ', STR_PAD_BOTH), 'white', 'yellow', 'bold', 'underline'));
98
    Console::log();
99
}
100
101
function printIndex()
102
{
103
    Console::log(' '. Console::text('Index:', 'underline', 'bold'));
104
    $rowHeaders = ['Index' => 10, 'Item'  => 25, 'Description' => 70];
105
106
    // customize table separator
107
    // customize table row style (color, bg...)
108
    Console::$verticalSeparator = '';
109
    Console::$verticalInnerSeparator = '';
110
    Console::$horizontalSeparator = ' ';
111
112
    //Console::log(' '.Console::tableRowSeparator($rowHeaders, 'grey'));
113
114
    Console::$horizontalSeparator = '_';
115
    Console::log(' '.Console::tableRowSeparator($rowHeaders, 'white', 'bold'));
116
117
    Console::$horizontalSeparator = ' ';
118
    //Console::$verticalSeparator = '|';
119
    Console::log(' '.Console::tableRowSeparator($rowHeaders, 'white', 'blue', 'bold'));
120
121
122
    Console::log(' '.Console::tableRow($rowHeaders, 'white', 'blue', 'bold'));
123
    Console::$horizontalSeparator = '_';
124
    Console::log(' '.Console::tableRowSeparator($rowHeaders, 'white', 'blue', 'bold'));
125
    Console::log(' '.Console::tableRowEmpty($rowHeaders, 'white', 'black'));
126
127
    $i = 0;
128
    foreach (getIndex() as $key => $value){
129
       
130
       if (file_exists( __DIR__ . '/'. $value[2])) {
131
            Console::log(' '.
132
                Console::tableRow([
133
                    $key        => 10, 
134
                    $value[0]   => 25, 
135
                    $value[1]   => 70
136
                ], 
137
                'white', 'black'
138
139
                 //   ($i % 2 == 1) ? 'white' :  'white',  
140
                 //   ($i % 2 == 1) ? 'cyan' :   'lightgrey',
141
                 //   ($i % 2 == 1) ? ''      :  'bold'
142
                )
143
            );
144
            $i++;
145
        }
146
        
147
    }
148
    Console::log(' '.Console::tableRowSeparator($rowHeaders, 'white', 'black', 'bold'));
149
150
    // reset table sseparators to defaults
151
    Console::resetDefaults();
152
153
    // --------------------
154
155
    Console::log('');
156
    Console::log(' ' .Console::text('Tips:', 'underline', 'bold'));
157
    Console::log(' '. Console::text('- At any time you can stop this program using [', 'green') .Console::text('Ctrl+C', 'black','white') .Console::text(']', 'green'));
158
    Console::log('');
159
}
160
161
function askIndex()
162
{
163
    $base = Console::text('~miShell^^' , 'yellow');
164
    $base .= Console::text(' $ ' , 'grey');
165
    $selectedIndex = Console::askInt($base . Console::text('Enter desired index then press [Enter] to run sample > ', 'white'));
166
    $index = getIndex();
167
168
    switch($selectedIndex){
169
        case 0: 
170
            Console::log();
171
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
172
            break;
173
174
        default:
175
            if (array_key_exists($selectedIndex, getIndex())) {
176
177
                $title      = $index[$selectedIndex][0];
178
                $fileName   = $index[$selectedIndex][2];
179
                $filePath   = __DIR__ . '/'. $fileName;
180
                
181
                Console::clear();
182
                printHeader();
183
                printSampleHeader($selectedIndex, $title);
184
185
                Console::log($base . Console::text('Start running [', 'white') . 
186
                                     Console::text( $title, 'lightcyan') . 
187
                                     Console::text('] in file [', 'white')  .
188
                                     Console::text( $fileName, 'lightcyan') . 
189
                                     Console::text(']', 'white'));
190
191
                if (file_exists($filePath)){
192
                    Console::log();
193
                    include $filePath;
194
                    Console::log();
195
                    Console::log($base . Console::text('End running [', 'white')  .
196
                                     Console::text( $title, 'lightcyan') . 
197
                                     Console::text(']', 'white')); 
198
                    $response = Console::ask($base . Console::text('Do you want to see the code that has been executed? (type y/Y to see the code) > ', 'white'));
199
                     if (strtoupper($response) === 'Y') {
200
                        Console::log($base . Console::text('The code in file [', 'white')  .
201
                                     Console::text( $fileName, 'lightcyan') . 
202
                                     Console::text('] is:', 'white')); 
203
                        Console::log();
204
205
                        $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
206
                        $count = 1;
207
                        foreach($lines as $line){
208
                            $codeLine = rtrim($line);
209
                            $isComment = substr(ltrim($codeLine), 0, 2) === '//';
210
                            Console::log(
211
                                Console::pad($count . ' ', 3, ' ', STR_PAD_LEFT) . 
0 ignored issues
show
Bug Best Practice introduced by
The method Kristuff\Mishell\ShellColoredPrinter::pad() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
                                Console::/** @scrutinizer ignore-call */ 
212
                                         pad($count . ' ', 3, ' ', STR_PAD_LEFT) . 
Loading history...
212
                                Console::text(Console::pad($codeLine, 150), $isComment ? 'magenta' : 'black', 'white')
213
                            );
214
                            $count++;
215
                        }
216
                        Console::log();
217
                    }
218
               } else {
219
                    Console::log($base . Console::text('Error' , 'red'));
220
                    Console::log($base . Console::text(' => File missing [' . $fileName . ']' , 'red'));
221
               }
222
223
            } else {
224
                Console::log($base . Console::text('Error:', 'red'));
225
                Console::log($base . Console::text('=> the value you entered is not a valid index number.', 'red'));
226
                askIndex();
227
            }
228
229
            Console::ask($base . Console::text('Press [Enter] to go back to index > ', 'white'));
230
            goIndex();
231
    }
232
}