Conditions | 12 |
Paths | 354 |
Total Lines | 59 |
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 |
||
27 | public function processCommand() |
||
28 | { |
||
29 | $this->name = $this->getRoute()->getMatchedParam('name'); |
||
30 | $this->force = $this->getRoute()->getMatchedParam('force', false); |
||
31 | |||
32 | try { |
||
33 | // file name (without extension) |
||
34 | // todo: should use config array instead of just '.md' |
||
35 | if (false !== $extPos = strripos($this->name, '.md')) { |
||
36 | $this->name = substr($this->name, 0, $extPos); |
||
37 | } |
||
38 | // find archetype |
||
39 | $section = strstr($this->name, '/', true); |
||
40 | $fileContent = <<<'EOT' |
||
41 | --- |
||
42 | title: '%title%' |
||
43 | date: '%date%' |
||
44 | draft: true |
||
45 | --- |
||
46 | |||
47 | EOT; |
||
48 | if ($section && file_exists($archetype = sprintf('%s/archetypes/%s.md', $this->getPath(), $section))) { |
||
49 | $fileContent = file_get_contents($archetype); |
||
50 | } else { |
||
51 | if (file_exists($archetype = sprintf('%s/archetypes/default.md', $this->getPath()))) { |
||
52 | $fileContent = file_get_contents($archetype); |
||
53 | } |
||
54 | } |
||
55 | // path |
||
56 | $fileRelativePath = $this->getPHPoole()->getConfig()->get('content.dir').'/'.$this->name.'.md'; |
||
57 | $filePath = $this->getPath().'/'.$fileRelativePath; |
||
58 | // file already exists? |
||
59 | if ($this->fs->exists($filePath) && !$this->force) { |
||
60 | if (!Confirm::prompt('This page already exists. Do you want to override it? [y/n]', 'y', 'n')) { |
||
61 | exit(0); |
||
62 | } |
||
63 | } |
||
64 | // create new file |
||
65 | $title = $this->name; |
||
66 | if (false !== strrchr($this->name, '/')) { |
||
67 | $title = substr(strrchr($this->name, '/'), 1); |
||
68 | } |
||
69 | $date = date('Y-m-d'); |
||
70 | $fileContent = str_replace(['%title%', '%date%'], [$title, $date], $fileContent); |
||
71 | $this->fs->dumpFile($filePath, $fileContent); |
||
72 | $this->wlDone(sprintf('File "%s" created!', $fileRelativePath)); |
||
73 | // open editor? |
||
74 | if ($editor = $this->phpoole->getConfig()->get('editor')) { |
||
75 | $command = sprintf('%s %s', $editor, $filePath); |
||
76 | $process = new Process($command); |
||
77 | $process->run(); |
||
78 | if (!$process->isSuccessful()) { |
||
79 | throw new \Exception(sprintf("Can't open '%s' editor.", $editor)); |
||
80 | } |
||
81 | } |
||
82 | } catch (\Exception $e) { |
||
83 | throw new \Exception(sprintf($e->getMessage())); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 |