Conditions | 5 |
Paths | 3 |
Total Lines | 55 |
Code Lines | 22 |
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 |
||
71 | protected function executeSimpleCommand( |
||
72 | ConfigurationInterface $configuration, |
||
73 | InputInterface $input, |
||
74 | OutputInterface $output |
||
75 | ) { |
||
76 | |||
77 | // initialize the default CSV serializer |
||
78 | $serializer = new ValueCsvSerializer(); |
||
79 | $serializer->init($configuration); |
||
80 | |||
81 | // initialize the array for the values that has to be serialized |
||
82 | $serialize = array(); |
||
83 | |||
84 | // load the values that has to be serialized |
||
85 | $values = $input->getArgument(InputArgumentKeys::VALUES); |
||
86 | |||
87 | // simulate custom column handling |
||
88 | switch ($input->getArgument(InputArgumentKeys::COLUMN)) { |
||
89 | |||
90 | case 'categories': |
||
91 | // serialize the categories and use the default delimiter |
||
92 | $serialize[] = $serializer->serialize($values); |
||
|
|||
93 | break; |
||
94 | |||
95 | case 'path': |
||
96 | // categories use a slash (/) as delimiter for the first level of serialization |
||
97 | $delimiter = '/'; |
||
98 | // load the enclosure from the configuration |
||
99 | $enclosure = $configuration->getEnclosure(); |
||
100 | // iterate over the values and serialize them |
||
101 | for ($i = 0; $i < sizeof($values); $i++) { |
||
102 | // serialize the value and use a slash (/) as delimiter |
||
103 | $val = $serializer->serialize(array($values[$i]), $delimiter); |
||
104 | |||
105 | // clean-up (means to remove surrounding + double quotes) |
||
106 | // because we've no delimiter within the value |
||
107 | if (strstr($val, $delimiter) === false) { |
||
108 | $val = preg_replace("/^(\'(.*)\'|${enclosure}(.*)${enclosure})$/", '$2$3', $val); |
||
109 | $val = str_replace('""', '"', $val); |
||
110 | } |
||
111 | |||
112 | // append the cleaned value |
||
113 | $values[$i] = $val; |
||
114 | } |
||
115 | |||
116 | // implode the category's to get the complete path |
||
117 | $serialize[] = implode($delimiter, $values); |
||
118 | break; |
||
119 | |||
120 | default: |
||
121 | break; |
||
122 | } |
||
123 | |||
124 | // second serialization that simulates the framework parsing the CSV file |
||
125 | $output->write($serializer->serialize($serialize)); |
||
126 | } |
||
128 |