| Conditions | 6 |
| Paths | 8 |
| Total Lines | 54 |
| 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 |
||
| 89 | ->get($plural) |
||
| 90 | ->get($term) |
||
| 91 | ->add($page); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Creates taxonomies pages. |
||
| 100 | */ |
||
| 101 | protected function createTaxonomiesPages() |
||
| 102 | { |
||
| 103 | /* @var $vocabulary VocabularyCollection */ |
||
| 104 | foreach ($this->taxonomiesCollection as $position => $vocabulary) { |
||
| 105 | $plural = $vocabulary->getId(); |
||
| 106 | if (count($vocabulary) > 0) { |
||
| 107 | /* |
||
| 108 | * Creates $plural/$term pages (list of pages) |
||
| 109 | * ie: /tags/tag-1/ |
||
| 110 | */ |
||
| 111 | /* @var $pages PagesCollection */ |
||
| 112 | foreach ($vocabulary as $position => $term) { |
||
| 113 | $pages = $term; |
||
| 114 | $term = $term->getId(); |
||
| 115 | $pages = $pages->sortByDate(); |
||
| 116 | $pageId = $path = Page::slugify(sprintf('%s/%s', $plural, $term)); |
||
| 117 | $page = (new Page($pageId))->setVariable('title', ucfirst($term)); |
||
| 118 | if ($this->pagesCollection->has($pageId)) { |
||
| 119 | $page = clone $this->pagesCollection->get($pageId); |
||
| 120 | } |
||
| 121 | $date = $pages->first()->getVariable('date'); |
||
| 122 | $page->setPath($path) |
||
| 123 | ->setType(Type::TAXONOMY) |
||
| 124 | ->setVariable('pages', $pages) |
||
| 125 | ->setVariable('date', $date) |
||
| 126 | ->setVariable('singular', $this->config->get('site.taxonomies')[$plural]) |
||
| 127 | ->setVariable('pagination', ['pages' => $pages]); |
||
| 128 | $this->generatedPages->add($page); |
||
| 129 | } |
||
| 130 | /* |
||
| 131 | * Creates $plural pages (list of terms) |
||
| 132 | * ex: /tags/ |
||
| 133 | */ |
||
| 134 | $page = (new Page(Page::slugify($plural))) |
||
| 135 | ->setPath(strtolower($plural)) |
||
| 136 | ->setVariable('title', $plural) |
||
| 137 | ->setType(Type::TERMS) |
||
| 138 | ->setVariable('plural', $plural) |
||
| 139 | ->setVariable('singular', $this->config->get('site.taxonomies')[$plural]) |
||
| 140 | ->setVariable('terms', $vocabulary) |
||
| 141 | ->setVariable('date', $date); |
||
| 142 | // add page only if a template exist |
||
| 143 | try { |
||
| 154 |
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: