Component::archives()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 45
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 45
ccs 40
cts 40
cp 1
rs 8.5806
cc 4
eloc 36
nc 4
nop 2
crap 4
1
<?php
2
3
namespace BootPress\Blog;
4
5
use BootPress\Page\Component as Page;
6
use BootPress\Asset\Component as Asset;
7
use BootPress\Sitemap\Component as Sitemap;
8
use BootPress\Hierarchy\Component as Hierarchy;
9
10
class Component extends Blog
11
{
12 22
    public function page()
13
    {
14 22
        $page = Page::html();
15 22
        $listings = $this->config('blog', 'listings');
16 22
        if ($id = $this->file($page->url['path'])) {
17 8
            $params = array('id'=>$id, 'path'=>$page->url['path']);
18 8
            $method = 'blog';
19 22
        } elseif ($route = $page->routes(array(
20 15
            $listings,
21 15
            $listings.'/[archives:path]/[i:year]?/[i:month]?/[i:day]?',
22 15
            $listings.'/[authors|tags:path]/[:name]?',
23 15
            $listings.'/[feed:path].xml',
24 15
            '[**:category]',
25 15
        ))) {
26 15
            $params = $route['params'];
27 15
            $method = (isset($params['path'])) ? $params['path'] : 'listings';
28 15
            if ($method == 'listings' && $search = $page->request->query->get('search')) {
29 2
                $params['search'] = $search;
30 2
            }
31 15
        }
32 22
        if (!isset($method) || (isset($params['category']) && !is_dir($this->folder.'content/'.$params['category']))) {
33 1
            $template = false;
34 22
        } elseif ($template = $this->$method($params)) {
0 ignored issues
show
Bug introduced by
The variable $params 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...
35 22
            $template = array_combine(array('file', 'vars'), $template);
36 22
            $template['default'] = $page->dir($page->dirname(__CLASS__), 'theme');
37 22
            if ($template['file'] == 'blog-feed.tpl') {
38 1
                $type = 'xml';
39 1
                $xml = $this->theme->fetchSmarty($template);
40 1
                if (preg_match('/<\/(?P<type>(rss|feed))>\s*$/', $xml, $matches)) {
41 1
                    $type = ($matches['type'] == 'rss') ? 'rss' : 'atom';
42 1
                }
43 1
                $template = $page->send(Asset::dispatch($type, $xml));
0 ignored issues
show
Bug introduced by
It seems like $xml defined by $this->theme->fetchSmarty($template) on line 39 can also be of type boolean; however, BootPress\Asset\Component::dispatch() does only seem to accept array|string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44 1
            }
45 22
        }
46
47 22
        return $template;
48
    }
49
50 8
    private function blog($params, array $vars = array())
51
    {
52 8
        $page = Page::html();
53 8
        extract($params); // 'id' and 'path'
54 8
        $page->enforce($path);
55 8
        $info = $this->info($id);
56 8
        $this->theme->globalVars('blog', array('page' => ($info['page'] ? 'page' : 'post')));
57 8
        $vars['post'] = $info;
58 8
        if ($search = $page->request->query->get('search')) {
59 1
            $sitemap = new Sitemap();
60 1
            if ($docid = $sitemap->db->value('SELECT docid FROM sitemap WHERE path = ?', $path)) {
61 1
                $words = $sitemap->words($search, $docid);
62 1
                if (!empty($words)) {
63 1
                    $vars['search'] = $words;
64 1
                }
65 1
            }
66 1
            unset($sitemap);
67 1
        }
68 8
        $vars['breadcrumbs'] = $this->breadcrumbs();
69 8
        foreach ($vars['post']['categories'] as $category) {
70 4
            $vars['breadcrumbs'][$category['name']] = $category['url'];
71 8
        }
72 8
        $vars['breadcrumbs'][$vars['post']['title']] = $vars['post']['url'];
73
74 8
        return array('blog-post.tpl', $vars);
75
    }
76
77 5
    private function listings($params, array $vars = array())
78
    {
79 5
        $page = Page::html();
80 5
        extract($params); // 'path'?, 'category'? and 'search'?
81 5
        if (isset($category)) {
82 3
            $url = $page->url('base', $category);
83 3
        } else {
84 2
            $url = $page->url('blog/listings');
85
        }
86 5
        $page->enforce($url);
87 5
        $this->theme->globalVars('blog', array('page' => (isset($category) ? 'category' : 'index')));
88 5
        $vars['listings'] = array();
89 5
        $vars['breadcrumbs'] = $this->breadcrumbs();
90 5
        if (isset($category)) {
91 3
            $hier = new Hierarchy($this->db, 'categories');
92 3
            $path = $hier->path(array('path', 'name'), array('where' => 'path = '.$category));
93 3
            if (empty($path)) {
94 1
                return false;
95
            }
96 3
            $tree = $hier->tree(array('path', 'name'), array('where' => 'path = '.$category));
97 3
            $counts = $hier->counts('blog', 'category_id');
98 3
            foreach ($tree as $id => $fields) {
99 3
                $tree[$id]['count'] = $counts[$id];
100 3
            }
101 3
            foreach ($path as $row) {
102 3
                $vars['category'][] = $row['name'];
103 3
                $vars['breadcrumbs'][$row['name']] = $page->url('base', $row['path']);
104 3
            }
105 3
            $vars['categories'] = $this->query('categories', array('nest' => $hier->nestify($tree), 'tree' => $tree));
106 3
            $vars['listings']['categories'] = array_keys($tree);
107 3
        }
108 5
        if (isset($search) && !empty($search)) {
109 2
            $vars['search'] = $search;
110 2
            $vars['breadcrumbs']['Search'] = $page->url('add', $url, 'search', $search);
111 2
            $vars['listings']['search'] = $search;
112 2
        }
113
114 5
        return array('blog-listings.tpl', $vars);
115
    }
116
117 4
    private function archives($params, array $vars = array())
118
    {
119 4
        $page = Page::html();
120 4
        $this->theme->globalVars('blog', array('page' => 'archives'));
121 4
        list($path, $Y, $m, $d) = array_pad(array_values($params), 4, '');
122 4
        if (!empty($d)) {
123 1
            list($from, $to) = $this->range($Y, $m, $d);
124 1
            $page->enforce($page->url('blog/listings', $path, date('/Y/m/d', $from)));
125 1
            $vars['archive'] = array_combine(array('date', 'year', 'month', 'day'), explode(' ', date($from.' Y F j', $from)));
126 1
            $vars['breadcrumbs'] = $this->breadcrumbs(array(
127 1
                'Archives' => $path,
128 1
                $vars['archive']['year'] => $Y,
129 1
                $vars['archive']['month'] => $m,
130 1
                $vars['archive']['day'] => $d,
131 1
            ));
132 4
        } elseif (!empty($m)) {
133 1
            list($from, $to) = $this->range($Y, $m);
134 1
            $page->enforce($page->url('blog/listings', $path, date('/Y/m', $from)));
135 1
            $vars['archive'] = array_combine(array('date', 'year', 'month'), explode(' ', date($from.' Y F', $from)));
136 1
            $vars['breadcrumbs'] = $this->breadcrumbs(array(
137 1
                'Archives' => $path,
138 1
                $vars['archive']['year'] => $Y,
139 1
                $vars['archive']['month'] => $m,
140 1
            ));
141 3
        } elseif (!empty($Y)) {
142 1
            list($from, $to) = $this->range($Y);
143 1
            $page->enforce($page->url('blog/listings', $path, date('/Y', $from)));
144 1
            $vars['archive'] = array_combine(array('date', 'year'), explode(' ', date($from.' Y', $from)));
145 1
            $vars['breadcrumbs'] = $this->breadcrumbs(array(
146 1
                'Archives' => $path,
147 1
                $vars['archive']['year'] => $Y,
148 1
            ));
149 1
        } else {
150 1
            $page->enforce($page->url('blog/listings', $path));
151 1
            $vars['archives'] = $this->query('archives');
152 1
            $vars['breadcrumbs'] = $this->breadcrumbs(array(
153 1
                'Archives' => $path,
154 1
            ));
155
156 1
            return array('blog-archives.tpl', $vars);
157
        }
158 3
        $vars['listings']['archives'] = array($from, $to);
159
160 3
        return array('blog-listings.tpl', $vars);
161
    }
162
163 2
    private function authors($params, array $vars = array())
164
    {
165 2
        $page = Page::html();
166 2
        extract($params); // 'path' and 'name'?
167 2
        $this->theme->globalVars('blog', array('page' => 'authors'));
168 2
        $vars['breadcrumbs'] = $this->breadcrumbs(array('Authors' => $path));
169 2
        if (!isset($name)) { // just authors, no posts
170 1
            $page->enforce($page->url('blog/listings', $path));
171 1
            $vars['authors'] = $this->query('authors');
172
173 1
            return array('blog-authors.tpl', $vars);
174
        }
175 1
        $vars['author'] = $this->query('authors', $name);
176 1
        if (empty($vars['author'])) {
177 1
            $page->eject($page->url('blog/listings', $path));
178
            
179 1
            return false;
180
        }
181 1
        $vars['breadcrumbs'][$vars['author']['name']] = $page->url('blog/listings', $path, $vars['author']['path']);
182 1
        $vars['listings']['count'] = $vars['author']['count'];
183 1
        $vars['listings']['authors'] = $name;
184
185 1
        return array('blog-listings.tpl', $vars);
186
    }
187
188 2
    private function tags($params, array $vars = array())
189
    {
190 2
        $page = Page::html();
191 2
        extract($params); // 'path' and 'name'?
192 2
        $this->theme->globalVars('blog', array('page' => 'tags'));
193 2
        $vars['breadcrumbs'] = $this->breadcrumbs(array('Tags' => $path));
194 2
        if (!isset($name)) { // search all tags and get a frequency count
195 1
            $page->enforce($page->url('blog/listings', $path));
196 1
            $vars['tags'] = $this->query('tags');
197
198 1
            return array('blog-tags.tpl', $vars);
199
        }
200 1
        $vars['tag'] = $this->query('tags', $name);
201 1
        if (empty($vars['tag'])) {
202 1
            $page->eject($page->url('blog/listings', $path));
203
            
204 1
            return false;
205
        }
206 1
        $vars['breadcrumbs'][$vars['tag']['name']] = $page->url('blog/listings', $path, $vars['tag']['path']);
207 1
        $vars['listings']['count'] = $vars['tag']['count'];
208 1
        $vars['listings']['tags'] = $vars['tag']['path'];
209
210 1
        return array('blog-listings.tpl', $vars);
211
    }
212
213 1
    private function feed($params, array $vars = array())
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
    {
215 1
        Page::html()->enforce(Page::html()->url('blog/listings', 'feed.xml'));
216 1
        $this->theme->globalVars('blog', array('page' => 'feed'));
217 1
        $vars['listings'] = array();
218 1
        return array('blog-feed.tpl', $vars);
219
    }
220
221 21
    private function breadcrumbs(array $links = array())
222
    {
223 21
        $breadcrumbs = array();
224 21
        $breadcrumbs[$this->config('blog', 'breadcrumb')] = Page::html()->url('blog/listings');
225 21
        $previous = '';
226 21
        foreach ($links as $name => $path) {
227 8
            $breadcrumbs[$name] = Page::html()->url('blog/listings', $previous.$path);
228 8
            $previous .= $path.'/';
229 21
        }
230
231 21
        return $breadcrumbs;
232
    }
233
234 3
    private function range($Y, $m = null, $d = null)
235
    {
236 3
        if (!empty($d)) {
237 1
            $from = mktime(0, 0, 0, (int) $m, (int) $d, (int) $Y);
238 1
            $to = mktime(23, 59, 59, (int) $m, (int) $d, (int) $Y);
239 3
        } elseif (!empty($m)) {
240 1
            $from = mktime(0, 0, 0, (int) $m, 1, (int) $Y);
241 1
            $to = mktime(23, 59, 59, (int) $m + 1, 0, (int) $Y);
242 1
        } else {
243 1
            $from = mktime(0, 0, 0, 1, 1, (int) $Y);
244 1
            $to = mktime(23, 59, 59, 1, 0, (int) $Y + 1);
245
        }
246
247 3
        return array($from, $to);
248
    }
249
}
250