Conditions | 10 |
Paths | 16 |
Total Lines | 60 |
Code Lines | 37 |
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 |
||
65 | public function initialize(Context $context, array $parameters = array()) |
||
66 | { |
||
67 | parent::initialize($context, $parameters); |
||
68 | |||
69 | $argv = self::getSourceValue('argv', array()); |
||
70 | // get rid of the script name |
||
71 | array_shift($argv); |
||
72 | |||
73 | $parameters = array(); |
||
74 | $input = array(); |
||
75 | |||
76 | $prev = ''; |
||
77 | foreach ($argv as $arg) { |
||
78 | if ($arg[0] == '-') { |
||
79 | // name |
||
80 | $parameters[$arg] = true; |
||
81 | } else { |
||
82 | if ($prev && $prev[0] == '-') { |
||
83 | $parameters[$prev] = $arg; |
||
84 | } else { |
||
85 | $input[] = $arg; |
||
86 | } |
||
87 | } |
||
88 | $prev = $arg; |
||
89 | } |
||
90 | |||
91 | $files = array(); |
||
92 | if ($this->getParameter('read_stdin', true) && defined('STDIN') && ($stdinMeta = stream_get_meta_data(STDIN)) && !$stdinMeta['seekable']) { |
||
93 | // if stream_get_meta_data() reports STDIN as not seekable, that means something was piped into our process, and we should put that into a file |
||
94 | // the alternative method to determine this is via posix_isatty(STDIN) which returns false in the same situation, but that requires the posix extension and also doesn't work on Windows |
||
95 | $stdinName = $this->getParameter('stdin_file_name', 'stdin_file'); |
||
96 | |||
97 | $ufc = $this->getParameter('uploaded_file_class', 'AgaviUploadedFile'); |
||
98 | $files = array( |
||
99 | $stdinName => new $ufc(array( |
||
100 | 'name' => $stdinName, |
||
101 | 'type' => 'application/octet-stream', |
||
102 | 'size' => -1, // we're not buffering, so -1 is a good choice probably (better than 0 anyway) |
||
103 | 'stream' => STDIN, |
||
104 | 'error' => UPLOAD_ERR_OK, |
||
105 | 'is_uploaded_file' => false, |
||
106 | )) |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | $rdhc = $this->getParameter('request_data_holder_class'); |
||
111 | $this->setRequestData(new $rdhc(array( |
||
112 | constant("$rdhc::SOURCE_PARAMETERS") => array(), |
||
113 | constant("$rdhc::SOURCE_FILES") => $files, |
||
114 | ))); |
||
115 | $rd = $this->getRequestData(); |
||
116 | |||
117 | foreach ($parameters as $name => $value) { |
||
118 | $rd->setParameter(substr($name, 1), $value); |
||
119 | } |
||
120 | |||
121 | $this->input = implode(' ', $input); |
||
122 | |||
123 | $this->setMethod($this->getParameter('default_method', 'read')); |
||
124 | } |
||
125 | |||
157 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: