Completed
Push — taxonomies ( 2adc93...017915 )
by Arnaud
07:56
created

Taxonomy   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 63
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 57 7
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Generator;
10
11
use Cecil\Collection\Page\Collection as PagesCollection;
12
use Cecil\Collection\Page\Page;
13
use Cecil\Collection\Page\Type;
14
use Cecil\Collection\Taxonomy\Collection as VocabulariesCollection;
15
use Cecil\Collection\Taxonomy\Term as Term;
16
use Cecil\Collection\Taxonomy\Vocabulary as Vocabulary;
17
use Cecil\Exception\Exception;
18
19
/**
20
 * Class Taxonomy.
21
 */
22
class Taxonomy extends AbstractGenerator implements GeneratorInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
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)
0 ignored issues
show
Bug introduced by
The variable $date does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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