Conditions | 10 |
Paths | 13 |
Total Lines | 55 |
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 |
||
53 | public function execute($inputLine) |
||
54 | { |
||
55 | $output = ''; |
||
56 | |||
57 | $tokens = explode(' ', $inputLine); |
||
58 | $command = array_shift($tokens); |
||
59 | $parameters = $tokens; |
||
60 | |||
61 | switch ($command) { |
||
62 | case 'insert': |
||
63 | try { |
||
64 | if (count($parameters) != 2) { |
||
65 | throw new Exception('Incorrect number of parameters for command "insert".'); |
||
66 | } |
||
67 | $collection = $parameters[0]; |
||
68 | $documentData = json_decode($parameters[1], true); |
||
69 | if ($documentData !== null) { |
||
70 | $document = new Document($documentData); |
||
71 | } else { |
||
72 | throw new Exception('The description of the document is not a valid json.'); |
||
73 | } |
||
74 | |||
75 | $this->documentManager->insert($collection, $document); |
||
76 | $output .= "Inserted document in the $collection collection.\n"; |
||
77 | } |
||
78 | catch (Exception $exception) { |
||
79 | $output .= "Error: ".$exception->getMessage()."\n"; |
||
80 | } |
||
81 | break; |
||
82 | case 'find': |
||
83 | $collection = $parameters[0]; |
||
84 | $filter = count($parameters) == 2 ? json_decode($parameters[1], true) : array(); |
||
85 | $documents = $this->documentManager->find($collection, $filter); |
||
86 | foreach ($documents as $document) { |
||
87 | $output .= json_encode($document)."\n"; |
||
88 | } |
||
89 | break; |
||
90 | case 'pwd': |
||
91 | $output .= $this->documentManager->getDatabasePath()."\n"; |
||
92 | break; |
||
93 | case 'help': |
||
94 | $output .= "Available commands:\n"; |
||
95 | $output .= " insert <collection> <document> Insert the document <document> into the collection <collection>.\n"; |
||
96 | $output .= " find <collection> <filter> Find all documents into the collection <collection> that match <filter>.\n"; |
||
97 | $output .= " pwd Print working directory.\n"; |
||
98 | $output .= " help Display this help.\n"; |
||
99 | $output .= " exit Exit this client.\n"; |
||
100 | break; |
||
101 | default: |
||
102 | $output .= "Syntax error: Unknown command '".$command."'.\n"; |
||
103 | } |
||
104 | $output .= "\n"; |
||
105 | |||
106 | return $output; |
||
107 | } |
||
108 | } |
||
109 |