| Conditions | 7 |
| Paths | 9 |
| Total Lines | 57 |
| 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 generate(): void |
||
| 28 | { |
||
| 29 | if ($this->config->get('site.taxonomies')) { |
||
| 30 | /* @var $vocabulary Vocabulary */ |
||
| 31 | foreach ($this->builder->getTaxonomies() as $position => $vocabulary) { |
||
| 32 | $plural = $vocabulary->getId(); |
||
| 33 | $singular = $this->config->get("site.taxonomies.$plural"); |
||
| 34 | if (count($vocabulary) > 0) { |
||
| 35 | /* |
||
| 36 | * Creates $plural/$term pages (list of pages) |
||
| 37 | * ie: /tags/tag-1/ |
||
| 38 | */ |
||
| 39 | /* @var $pages PagesCollection */ |
||
| 40 | foreach ($vocabulary as $position => $term) { |
||
| 41 | $pageId = $path = Page::slugify(sprintf('%s/%s', $plural, $term->getId())); |
||
| 42 | $pages = $term->sortByDate(); |
||
| 43 | $date = $pages->first()->getVariable('date'); |
||
| 44 | $page = (new Page($pageId)) |
||
| 45 | ->setVariable('title', $term->getName()); |
||
| 46 | if ($this->pagesCollection->has($pageId)) { |
||
| 47 | $page = clone $this->pagesCollection->get($pageId); |
||
| 48 | } |
||
| 49 | $page |
||
| 50 | ->setType(Type::TERM) |
||
| 51 | ->setPath($path) |
||
| 52 | ->setVariable('date', $date) |
||
| 53 | ->setVariable('term', $term->getId()) |
||
| 54 | ->setVariable('plural', $plural) |
||
| 55 | ->setVariable('singular', $singular) |
||
| 56 | ->setVariable('pages', $pages) |
||
| 57 | ->setVariable('pagination', ['pages' => $pages]); |
||
| 58 | $this->generatedPages->add($page); |
||
| 59 | } |
||
| 60 | /* |
||
| 61 | * Creates $plural pages (list of terms) |
||
| 62 | * ex: /tags/ |
||
| 63 | */ |
||
| 64 | $pageId = $path = Page::slugify($plural); |
||
| 65 | $page = (new Page($pageId)) |
||
| 66 | ->setType(Type::VOCABULARY) |
||
| 67 | ->setPath($path) |
||
| 68 | ->setVariable('title', ucfirst($plural)) |
||
| 69 | ->setVariable('date', $date) |
||
|
|
|||
| 70 | ->setVariable('plural', $plural) |
||
| 71 | ->setVariable('singular', $singular) |
||
| 72 | ->setVariable('terms', $vocabulary); |
||
| 73 | // add page only if a template exist |
||
| 74 | try { |
||
| 75 | $this->generatedPages->add($page); |
||
| 76 | } catch (Exception $e) { |
||
| 77 | printf("%s\n", $e->getMessage()); |
||
| 78 | unset($page); // do not add page |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | } |
||
| 85 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: