Conditions | 18 |
Paths | 162 |
Total Lines | 79 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 4 |
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 |
||
21 | public function run(InputArgs $inputArgs) |
||
22 | { |
||
23 | $workingDir = getcwd(); |
||
24 | if ($inputArgs->getOption('colors') !== NULL && !$inputArgs->getOption('colors')) { |
||
25 | Cli::$enableColors = FALSE; |
||
26 | } |
||
27 | if ($inputArgs->getOption('working-dir')) { |
||
28 | $workingDir = realpath($inputArgs->getOption('working-dir')); |
||
29 | if (!$workingDir) { |
||
30 | $this->log(sprintf("Working dir '%s' does not exists.", $inputArgs->getOption('working-dir')), 'red'); |
||
31 | exit(255); |
||
1 ignored issue
–
show
|
|||
32 | } |
||
33 | } |
||
34 | if ($workingDir === __DIR__) { |
||
35 | $this->log(sprintf("Working dir '%s' is directory with Genesis. You have to choose directory with build.", $workingDir), 'red'); |
||
36 | exit(255); |
||
1 ignored issue
–
show
|
|||
37 | } |
||
38 | |||
39 | $arguments = $inputArgs->getArguments(); |
||
40 | if(isset($arguments[0]) && $arguments[0] === 'self-init'){ |
||
41 | $directoryName = isset($arguments[1]) ? $arguments[1] : 'build'; |
||
42 | $selfInit = new Commands\SelfInit(); |
||
43 | $selfInit->setDistDirectory(__DIR__ . '/build-dist'); |
||
44 | $selfInit->setWorkingDirectory($workingDir); |
||
45 | $selfInit->setDirname($directoryName); |
||
46 | $selfInit->execute(); |
||
47 | exit(0); |
||
1 ignored issue
–
show
|
|||
48 | } |
||
49 | |||
50 | $bootstrapFile = $this->detectBootstrapFilename($workingDir); |
||
51 | $container = NULL; |
||
52 | if (is_file($bootstrapFile)) { |
||
53 | $this->log("Info: Found bootstrap.php in working directory.", 'dark_gray'); |
||
54 | $container = require_once $bootstrapFile; |
||
55 | if($container === 1 || $container === TRUE){ // 1 = success, TRUE = already required |
||
56 | $container = NULL; |
||
57 | }elseif(!($container instanceof Container)){ |
||
58 | $this->log("Returned value from bootstrap.php must be instance of 'Genesis\\Container\\Container' or nothing (NULL).", 'red'); |
||
59 | exit(255); |
||
1 ignored issue
–
show
|
|||
60 | } |
||
61 | } else { |
||
62 | $this->log("Info: bootstrap.php was not found in working directory.", 'dark_gray'); |
||
63 | } |
||
64 | |||
65 | $arguments = $inputArgs->getArguments(); |
||
66 | $configFile = $inputArgs->getOption('config') ? $inputArgs->getOption('config') : self::DEFAULT_CONFIG_FILE; |
||
67 | try { |
||
68 | $container = $this->createContainer($workingDir, $configFile, $container); |
||
69 | $build = $this->createBuild($container, $arguments); |
||
70 | } catch (\Exception $e) { |
||
71 | $this->log("Exited with ERROR:", 'red'); |
||
72 | $this->log($e->getMessage(), 'red'); |
||
73 | echo $e->getTraceAsString() . PHP_EOL; |
||
74 | exit(255); |
||
1 ignored issue
–
show
|
|||
75 | } |
||
76 | if (count($arguments) < 1) { |
||
77 | $this->log("Running default", 'green'); |
||
78 | $build->runDefault(); |
||
79 | exit(0); |
||
1 ignored issue
–
show
|
|||
80 | } |
||
81 | |||
82 | $method = 'run' . str_replace('-', '', ucfirst($arguments[0])); |
||
83 | if (!method_exists($build, $method)) { |
||
84 | $this->log("Task '$arguments[0]' does not exists.", 'red'); |
||
85 | exit(255); |
||
1 ignored issue
–
show
|
|||
86 | } |
||
87 | $this->log("Running [$arguments[0]]", 'green'); |
||
88 | try { |
||
89 | $build->$method(); |
||
90 | } catch (\Exception $e) { |
||
91 | $this->log("Exited with ERROR:", 'red'); |
||
92 | $this->log($e->getMessage(), 'red'); |
||
93 | echo $e->getTraceAsString() . PHP_EOL; |
||
94 | exit(255); |
||
1 ignored issue
–
show
|
|||
95 | } |
||
96 | $this->log("Exited with SUCCESS", 'black', 'green'); |
||
97 | echo PHP_EOL; |
||
98 | exit(0); |
||
1 ignored issue
–
show
|
|||
99 | } |
||
100 | |||
182 | } |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.