| Conditions | 6 |
| Paths | 13 |
| Total Lines | 70 |
| 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 |
||
| 71 | protected function stateMachineInDotFormat(array $config) |
||
| 72 | { |
||
| 73 | // Output image mime types. |
||
| 74 | $mimeTypes = [ |
||
| 75 | 'png' => 'image/png', |
||
| 76 | 'jpg' => 'image/jpeg', |
||
| 77 | 'gif' => 'image/gif', |
||
| 78 | 'svg' => 'image/svg+xml', |
||
| 79 | ]; |
||
| 80 | |||
| 81 | $format = $this->option('format'); |
||
| 82 | |||
| 83 | if (empty($mimeTypes[$format])) { |
||
| 84 | throw new \Exception(sprintf("Format '%s' is not supported", $format)); |
||
| 85 | } |
||
| 86 | |||
| 87 | $dotPath = $this->option('dot-path'); |
||
| 88 | |||
| 89 | // Temporary files. |
||
| 90 | $dotFile = tempnam(sys_get_temp_dir(), 'smv'); |
||
| 91 | $outputImage = $this->option('output'); |
||
| 92 | |||
| 93 | // Display settings |
||
| 94 | $layout = $this->option('direction'); |
||
| 95 | $layout = $layout === 'TB' ? 'TB' : 'LR'; |
||
| 96 | |||
| 97 | $nodeShape = $this->option('shape'); |
||
| 98 | |||
| 99 | // Build dot file content. |
||
| 100 | $result = []; |
||
| 101 | $result[] = 'digraph finite_state_machine {'; |
||
| 102 | $result[] = "rankdir=$layout;"; |
||
| 103 | $result[] = 'node [shape = point]; _start_'; // Input node |
||
| 104 | |||
| 105 | // Use first value from 'states' as start. |
||
| 106 | $start = $config['states'][0]['name']; |
||
| 107 | $result[] = "node [shape = $nodeShape];"; // Default nodes |
||
| 108 | $result[] = '_start_ -> ' . $start . ';'; // Input node -> starting node. |
||
| 109 | |||
| 110 | foreach ($config['transitions'] as $name => $transition) { |
||
| 111 | foreach ($transition['from'] as $from) { |
||
| 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([$dotPath, '-T' . $format, '-o', $outputImage, $dotFile]); |
||
| 134 | $process->run(); |
||
| 135 | |||
| 136 | // executes after the command finishes |
||
| 137 | if (!$process->isSuccessful()) { |
||
| 138 | throw new ProcessFailedException($process); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | } |
||
| 142 |
If you implement
__calland 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
__callis implemented by a parent class and only the child class knows which methods exist: