| Conditions | 13 |
| Paths | 17 |
| Total Lines | 60 |
| Code Lines | 40 |
| 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 |
||
| 46 | public function index() |
||
| 47 | { |
||
| 48 | if (!Director::is_cli()) { |
||
| 49 | return "The SilverStripe Interactive Command-line doesn't work in a web browser." |
||
| 50 | . " Use 'sake interactive' from the command-line to run."; |
||
| 51 | } |
||
| 52 | |||
| 53 | |||
| 54 | /* Try using PHP_Shell if it exists */ |
||
| 55 | @include 'php-shell-cmd.php' ; |
||
| 56 | |||
| 57 | /* Fall back to our simpler interface */ |
||
| 58 | if (empty($__shell)) { |
||
| 59 | set_error_handler(array($this, 'error_handler')); |
||
| 60 | |||
| 61 | echo "SilverStripe Interactive Command-line (REPL interface). Type help for hints.\n\n"; |
||
| 62 | while (true) { |
||
| 63 | echo CLI::text("?> ", "cyan"); |
||
| 64 | echo CLI::start_colour("yellow"); |
||
| 65 | $command = trim(fgets(STDIN, 4096)); |
||
| 66 | echo CLI::end_colour(); |
||
| 67 | |||
| 68 | if ($command == 'help' || $command == '?') { |
||
| 69 | print "help or ? to exit\n" ; |
||
| 70 | print "quit or \q to exit\n" ; |
||
| 71 | print "install PHP_Shell for a more advanced interface with" |
||
| 72 | . " auto-completion and readline support\n\n" ; |
||
| 73 | continue ; |
||
| 74 | } |
||
| 75 | |||
| 76 | if ($command == 'quit' || $command == '\q') { |
||
| 77 | break ; |
||
| 78 | } |
||
| 79 | |||
| 80 | // Simple command processing |
||
| 81 | if (substr($command, -1) == ';') { |
||
| 82 | $command = substr($command, 0, -1); |
||
| 83 | } |
||
| 84 | $is_print = preg_match('/^\s*print/i', $command); |
||
| 85 | $is_return = preg_match('/^\s*return/i', $command); |
||
| 86 | if (!$is_print && !$is_return) { |
||
| 87 | $command = "return ($command)"; |
||
| 88 | } |
||
| 89 | $command .= ";"; |
||
| 90 | |||
| 91 | try { |
||
| 92 | $result = eval($command); |
||
| 93 | if (!$is_print) { |
||
| 94 | print_r($result); |
||
| 95 | } |
||
| 96 | echo "\n"; |
||
| 97 | } catch (Exception $__repl_exception) { |
||
| 98 | echo CLI::start_colour("red"); |
||
| 99 | printf( |
||
| 100 | '%s (code: %d) got thrown' . PHP_EOL, |
||
| 101 | get_class($__repl_exception), |
||
| 102 | $__repl_exception->getCode() |
||
| 103 | ); |
||
| 104 | print $__repl_exception; |
||
| 105 | echo "\n"; |
||
| 106 | } |
||
| 111 |