Conditions | 6 |
Paths | 8 |
Total Lines | 52 |
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 |
||
101 | { |
||
102 | /* @var $vocabulary Vocabulary */ |
||
103 | foreach ($this->vocabulariesCollection as $position => $vocabulary) { |
||
104 | $plural = $vocabulary->getId(); |
||
105 | if (count($vocabulary) > 0) { |
||
106 | /* |
||
107 | * Creates $plural/$term pages (list of pages) |
||
108 | * ie: /tags/tag-1/ |
||
109 | */ |
||
110 | /* @var $pages PagesCollection */ |
||
111 | foreach ($vocabulary as $position => $term) { |
||
112 | $pages = $term->sortByDate(); |
||
113 | $pageId = $path = Page::slugify(sprintf('%s/%s', $plural, $term->getId())); |
||
114 | $page = (new Page($pageId))->setVariable('title', ucfirst($term->getId())); |
||
115 | if ($this->pagesCollection->has($pageId)) { |
||
116 | $page = clone $this->pagesCollection->get($pageId); |
||
117 | } |
||
118 | $date = $pages->first()->getVariable('date'); |
||
119 | $page->setPath($path) |
||
120 | ->setType(Type::TAXONOMY_VOCABULARY) |
||
121 | ->setVariable('pages', $pages) |
||
122 | ->setVariable('date', $date) |
||
123 | ->setVariable('singular', $this->config->get("site.taxonomies.$plural")) |
||
124 | ->setVariable('pagination', ['pages' => $pages]); |
||
125 | $this->generatedPages->add($page); |
||
126 | } |
||
127 | /* |
||
128 | * Creates $plural pages (list of terms) |
||
129 | * ex: /tags/ |
||
130 | */ |
||
131 | $page = (new Page(Page::slugify($plural))) |
||
132 | ->setPath(strtolower($plural)) |
||
133 | ->setVariable('title', $plural) |
||
134 | ->setType(Type::TAXONOMY_TERMS) |
||
135 | ->setVariable('plural', $plural) |
||
136 | ->setVariable('singular', $this->config->get("site.taxonomies.$plural")) |
||
137 | ->setVariable('terms', $vocabulary) |
||
138 | ->setVariable('date', $date); |
||
139 | // add page only if a template exist |
||
140 | try { |
||
141 | $this->generatedPages->add($page); |
||
142 | } catch (Exception $e) { |
||
143 | printf("%s\n", $e->getMessage()); |
||
144 | unset($page); // do not add page |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: