Conditions | 11 |
Paths | 28 |
Total Lines | 47 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 2 | Features | 2 |
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 |
||
20 | public function run(Storage $storage) |
||
21 | { |
||
22 | parent::run($storage); |
||
23 | |||
24 | if (isset($this->parameters['documentParameterName'])) { |
||
25 | $this->documentParameterName = $this->parameters['documentParameterName']; |
||
26 | } |
||
27 | |||
28 | if ($this->matchedSitemapItem === null) { // If no sitemapitem, its an application component |
||
29 | if (isset($this->parameters['document'])) { |
||
30 | $this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']); |
||
31 | } else { |
||
32 | throw new \Exception('When used as application component, you need to specify a document.'); |
||
33 | } |
||
34 | } else { |
||
35 | if ($this->matchedSitemapItem->regex == false) { |
||
36 | if (isset($this->parameters['document'])) { |
||
37 | $this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']); |
||
38 | } else { |
||
39 | throw new \Exception('When not using a regex, you need to set the parameter `document` with the path to the document in this sitemap item: ' . $this->matchedSitemapItem->title); |
||
40 | } |
||
41 | } else { |
||
42 | if (isset($this->parameters['document'])) { |
||
43 | $this->parameters[$this->documentParameterName] = $storage->getDocumentBySlug($this->parameters['document']); |
||
44 | } else { |
||
45 | $relativeDocumentUri = current($this->matchedSitemapItem->matches[1]); |
||
46 | if (isset($this->parameters['folder'])) { |
||
47 | if (substr($this->parameters['folder'], -1) !== '/') { |
||
48 | $this->parameters['folder'] = $this->parameters['folder'] . '/'; |
||
49 | } |
||
50 | $relativeDocumentUri = $this->parameters['folder'] . $relativeDocumentUri; |
||
51 | } |
||
52 | |||
53 | $document = $storage->getDocumentBySlug($relativeDocumentUri); |
||
54 | |||
55 | if ($document->type == 'folder') { |
||
56 | throw new \Exception('The found document is a folder.'); |
||
57 | } |
||
58 | |||
59 | if ($document->state != 'published') { |
||
60 | throw new \Exception('Found document is unpublished.'); |
||
61 | } |
||
62 | $this->parameters[$this->documentParameterName] = $document; |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | } |