Conditions | 5 |
Paths | 7 |
Total Lines | 66 |
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 |
||
70 | protected function stateMachineInDotFormat(array $config) |
||
71 | { |
||
72 | // Output image mime types. |
||
73 | $mimeTypes = [ |
||
74 | 'png' => 'image/png', |
||
75 | 'jpg' => 'image/jpeg', |
||
76 | 'gif' => 'image/gif', |
||
77 | 'svg' => 'image/svg+xml', |
||
78 | ]; |
||
79 | |||
80 | $format = $this->option('format'); |
||
81 | |||
82 | if (empty($mimeTypes[$format])) { |
||
83 | throw new \Exception(sprintf("Format '%s' is not supported", $format)); |
||
84 | } |
||
85 | |||
86 | $dotPath = $this->option('dot-path'); |
||
87 | |||
88 | // Temporary files. |
||
89 | $dotFile = tempnam(sys_get_temp_dir(), 'smv'); |
||
90 | $outputImage = $this->option('output'); |
||
91 | |||
92 | // Display settings |
||
93 | $layout = $this->option('direction'); |
||
94 | $layout = $layout === 'TB' ? 'TB' : 'LR'; |
||
95 | |||
96 | $nodeShape = $this->option('shape'); |
||
97 | |||
98 | // Build dot file content. |
||
99 | $result = []; |
||
100 | $result[] = 'digraph finite_state_machine {'; |
||
101 | $result[] = "rankdir=$layout;"; |
||
102 | $result[] = 'node [shape = point]; _start_'; // Input node |
||
103 | |||
104 | // Use first value from 'states' as start. |
||
105 | $start = $config['states'][0]['name']; |
||
106 | $result[] = "node [shape = $nodeShape];"; // Default nodes |
||
107 | $result[] = '_start_ -> '.$start.';'; // Input node -> starting node. |
||
108 | |||
109 | foreach ($config['transitions'] as $name => $transition) { |
||
110 | foreach ($transition['from'] as $from) { |
||
111 | // TODO: Add customization. |
||
112 | $result[] = $from.' -> '.$transition['to'].'[ label = "'.$name.'" ];'; |
||
113 | } |
||
114 | } |
||
115 | |||
116 | $result[] = '}'; |
||
117 | |||
118 | $result = implode(PHP_EOL, $result); |
||
119 | |||
120 | // Save dot file for input. |
||
121 | file_put_contents($dotFile, $result); |
||
122 | |||
123 | // Dot command. |
||
124 | $cmd = sprintf( |
||
125 | "%s -T%s -o %s %s", |
||
126 | $dotPath, |
||
127 | $format, |
||
128 | $outputImage, // Output file |
||
129 | $dotFile // Input file |
||
130 | ); |
||
131 | |||
132 | |||
133 | $process = new Process($cmd); |
||
134 | $process->run(); |
||
135 | } |
||
136 | } |
||
137 |
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: