Conditions | 15 |
Paths | 324 |
Total Lines | 113 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
18 | public function run() |
||
19 | { |
||
20 | $opts = getopt( |
||
21 | 'hi:o:', |
||
22 | array( |
||
23 | 'help', |
||
24 | 'input:', |
||
25 | 'output:', |
||
26 | 'dry-run', |
||
27 | 'keep-sourcemap', |
||
28 | 'keep-sourcemap-comment', |
||
29 | 'linebreak-position:', |
||
30 | 'memory-limit:', |
||
31 | 'pcre-backtrack-limit:', |
||
32 | 'pcre-recursion-limit:', |
||
33 | 'remove-important-comments' |
||
34 | ) |
||
35 | ); |
||
36 | |||
37 | $help = $this->getOpt(array('h', 'help'), $opts); |
||
38 | $input = $this->getOpt(array('i', 'input'), $opts); |
||
39 | $output = $this->getOpt(array('o', 'output'), $opts); |
||
40 | $dryrun = $this->getOpt('dry-run', $opts); |
||
41 | $keepSourceMapComment = $this->getOpt(array('keep-sourcemap', 'keep-sourcemap-comment'), $opts); |
||
42 | $linebreakPosition = $this->getOpt('linebreak-position', $opts); |
||
43 | $memoryLimit = $this->getOpt('memory-limit', $opts); |
||
44 | $backtrackLimit = $this->getOpt('pcre-backtrack-limit', $opts); |
||
45 | $recursionLimit = $this->getOpt('pcre-recursion-limit', $opts); |
||
46 | $removeImportantComments = $this->getOpt('remove-important-comments', $opts); |
||
47 | |||
48 | if (!is_null($help)) { |
||
49 | $this->showHelp(); |
||
50 | die(self::SUCCESS_EXIT); |
||
|
|||
51 | } |
||
52 | |||
53 | if (is_null($input)) { |
||
54 | fwrite(STDERR, '-i <file> argument is missing' . PHP_EOL); |
||
55 | $this->showHelp(); |
||
56 | die(self::FAILURE_EXIT); |
||
57 | } |
||
58 | |||
59 | if (!is_readable($input)) { |
||
60 | fwrite(STDERR, 'Input file is not readable' . PHP_EOL); |
||
61 | die(self::FAILURE_EXIT); |
||
62 | } |
||
63 | |||
64 | $css = file_get_contents($input); |
||
65 | |||
66 | if ($css === false) { |
||
67 | fwrite(STDERR, 'Input CSS code could not be retrieved from input file' . PHP_EOL); |
||
68 | die(self::FAILURE_EXIT); |
||
69 | } |
||
70 | |||
71 | $this->setStat('original-size', strlen($css)); |
||
72 | |||
73 | $cssmin = new Minifier; |
||
74 | |||
75 | if (!is_null($keepSourceMapComment)) { |
||
76 | $cssmin->keepSourceMapComment(); |
||
77 | } |
||
78 | |||
79 | if (!is_null($removeImportantComments)) { |
||
80 | $cssmin->removeImportantComments(); |
||
81 | } |
||
82 | |||
83 | if (!is_null($linebreakPosition)) { |
||
84 | $cssmin->setLineBreakPosition($linebreakPosition); |
||
85 | } |
||
86 | |||
87 | if (!is_null($memoryLimit)) { |
||
88 | $cssmin->setMemoryLimit($memoryLimit); |
||
89 | } |
||
90 | |||
91 | if (!is_null($backtrackLimit)) { |
||
92 | $cssmin->setPcreBacktrackLimit($backtrackLimit); |
||
93 | } |
||
94 | |||
95 | if (!is_null($recursionLimit)) { |
||
96 | $cssmin->setPcreRecursionLimit($recursionLimit); |
||
97 | } |
||
98 | |||
99 | $this->setStat('compression-time-start', microtime(true)); |
||
100 | |||
101 | $css = $cssmin->run($css); |
||
102 | |||
103 | $this->setStat('compression-time-end', microtime(true)); |
||
104 | $this->setStat('peak-memory-usage', memory_get_peak_usage(true)); |
||
105 | $this->setStat('compressed-size', strlen($css)); |
||
106 | |||
107 | if (!is_null($dryrun)) { |
||
108 | $this->showStats(); |
||
109 | die(self::SUCCESS_EXIT); |
||
110 | } |
||
111 | |||
112 | if (is_null($output)) { |
||
113 | fwrite(STDOUT, $css . PHP_EOL); |
||
114 | $this->showStats(); |
||
115 | die(self::SUCCESS_EXIT); |
||
116 | } |
||
117 | |||
118 | if (!is_writable(dirname($output))) { |
||
119 | fwrite(STDERR, 'Output file is not writable' . PHP_EOL); |
||
120 | die(self::FAILURE_EXIT); |
||
121 | } |
||
122 | |||
123 | if (file_put_contents($output, $css) === false) { |
||
124 | fwrite(STDERR, 'Compressed CSS code could not be saved to output file' . PHP_EOL); |
||
125 | die(self::FAILURE_EXIT); |
||
126 | } |
||
127 | |||
128 | $this->showStats(); |
||
129 | |||
130 | die(self::SUCCESS_EXIT); |
||
131 | } |
||
224 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.