Blog   D
last analyzed

Complexity

Total Complexity 208

Size/Duplication

Total Lines 930
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 208
c 10
b 0
f 0
lcom 1
cbo 7
dl 0
loc 930
ccs 774
cts 774
cp 1
rs 4.4444

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 8 2
B __construct() 0 66 4
B configInfo() 0 22 5
D blogInfo() 0 44 11
C getId() 0 58 18
F query() 0 381 101
C file() 0 67 12
F info() 0 74 15
D config() 0 45 17
C updateDatabase() 0 42 7
F updateConfig() 0 83 16

How to fix   Complexity   

Complex Class

Complex classes like Blog often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Blog, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace BootPress\Blog;
4
5
use BootPress\Page\Component as Page;
6
use BootPress\Theme\Component as Theme;
7
use BootPress\SQLite\Component as SQLite;
8
use BootPress\Sitemap\Component as Sitemap;
9
use BootPress\Hierarchy\Component as Hierarchy;
10
use BootPress\Pagination\Component as Pagination;
11
use Symfony\Component\Finder\Finder;
12
use Symfony\Component\Yaml\Yaml;
13
14
/*
15
$html = '';
16
$page = Page::html();
17
$theme = new Theme;
18
$blog = new Blog($theme);
19
if ($template = $blog->page()) {
20
    $html = $theme->fetchSmarty($template);
21
}
22
$html = $page->display($theme->layout($html));
23
*/
24
25
class Blog
26
{
27
    public $db;
28
    protected $theme;
29
    protected $folder;
30
    private $config;
31
    private $ids;
32
    
33
    /**
34
     * Blog folder getter.
35
     * 
36
     * @param string $name 
37
     * 
38
     * @return null|string
39
     */
40 1
    public function __get($name)
41
    {
42
        switch ($name) {
43 1
            case 'folder':
44 1
                return $this->$name;
45
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
46
        }
47 1
    }
48
49 24
    public function __construct(Theme $theme, $folder = 'blog')
50
    {
51 24
        $page = Page::html();
52
53
        // $this->folder
54 24
        $this->folder = $page->dir($folder);
55 24
        if (!is_dir($this->folder.'content')) {
56 1
            mkdir($this->folder.'content', 0755, true);
57 1
        }
58
        
59
        // $this->theme
60 24
        $this->theme = $theme;
61 24
        $this->theme->globalVars('blog', $this->config('blog'));
62 24
        $this->theme->addPageMethod('blog', array($this, 'query'));
63
        
64
        // set 'blog/listings' and 'blog/config' url's
65 24
        $blog = $page->url['base'];
66 24
        $listings = $this->config('blog', 'listings');
67 24
        $page->url('set', 'blog/listings', ($listings ? $blog.$listings.'/' : $blog));
68 24
        $page->url('set', 'blog/config', $blog.'page/'.substr($this->folder, strlen($page->dir['page'])));
69
70
        // $this->db
71 24
        $this->db = new SQLite($this->folder.'Blog.db');
72 24
        if ($this->db->created) {
73 2
            $this->db->create('blog', array(
74 2
                'id' => 'INTEGER PRIMARY KEY',
75 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
76 2
                'title' => 'TEXT NOT NULL DEFAULT ""',
77 2
                'description' => 'TEXT NOT NULL DEFAULT ""',
78 2
                'keywords' => 'TEXT NOT NULL DEFAULT ""',
79 2
                'theme' => 'TEXT NOT NULL DEFAULT ""',
80 2
                'thumb' => 'TEXT NOT NULL DEFAULT ""',
81 2
                'featured' => 'INTEGER NOT NULL DEFAULT 0',
82 2
                'published' => 'INTEGER NOT NULL DEFAULT 0',
83 2
                'updated' => 'INTEGER NOT NULL DEFAULT 0',
84 2
                'author_id' => 'INTEGER NOT NULL DEFAULT 0',
85 2
                'category_id' => 'INTEGER NOT NULL DEFAULT 0',
86 2
                'search' => 'INTEGER NOT NULL DEFAULT 1',
87 2
                'content' => 'TEXT NOT NULL DEFAULT ""',
88 2
            ), array('featured, published, updated, author_id', 'category_id'));
89 2
            $this->db->create('authors', array(
90 2
                'id' => 'INTEGER PRIMARY KEY',
91 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
92 2
                'name' => 'TEXT NOT NULL COLLATE NOCASE DEFAULT ""',
93 2
            ));
94 2
            $this->db->create('categories', array(
95 2
                'id' => 'INTEGER PRIMARY KEY',
96 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
97 2
                'name' => 'TEXT NOT NULL COLLATE NOCASE DEFAULT ""',
98 2
                'parent' => 'INTEGER NOT NULL DEFAULT 0',
99 2
                'level' => 'INTEGER NOT NULL DEFAULT 0',
100 2
                'lft' => 'INTEGER NOT NULL DEFAULT 0',
101 2
                'rgt' => 'INTEGER NOT NULL DEFAULT 0',
102 2
            ));
103 2
            $this->db->create('tags', array(
104 2
                'id' => 'INTEGER PRIMARY KEY',
105 2
                'path' => 'TEXT UNIQUE COLLATE NOCASE',
106 2
                'name' => 'TEXT NOT NULL COLLATE NOCASE DEFAULT ""',
107 2
            ));
108 2
            $this->db->create('tagged', array(
109 2
                'blog_id' => 'INTEGER NOT NULL DEFAULT 0',
110 2
                'tag_id' => 'INTEGER NOT NULL DEFAULT 0',
111 2
            ), array('unique' => 'blog_id, tag_id'));
112 2
            $this->updateDatabase();
113 2
        }
114 24
    }
115
116 21
    public function query($type, $params = null)
117
    {
118 18
        $posts = array();
119 18
        if (is_array($type)) { // listings
120 12
            $vars = array();
121 12
            foreach (array('archives', 'authors', 'tags', 'categories', 'default') as $query) {
122 12
                if (isset($type[$query])) {
123 9
                    $vars = $type[$query];
124
                    switch ($query) {
125 9
                        case 'archives':
126 4
                            $vars = (is_array($vars) && count($vars) == 2) ? $vars : false; // array(from, to)
127 4
                            break;
128 6
                        case 'authors':
129 6
                        case 'tags':
130 3
                            $vars = (is_string($vars)) ? $vars : false; // path
131 3
                            break;
132 4
                        case 'categories':
133 4
                            $vars = (is_string($vars) || (is_array($vars) && ctype_digit(implode('', $vars)))) ? $vars : false;
134 4
                            break; // path (string) or category_id's (array)
135
                    }
136 9
                    break;
137
                }
138 12
            }
139 12
            if ($vars === false) {
140 1
                return;
141
            }
142 12
            $count = (is_object($params) && empty($params->limit)) ? false : true; // $params instanceof Pagination
143 21
            if ($count && isset($type['count'])) {
144 2
                return $type['count'];
145
            }
146
            switch ($query) {
0 ignored issues
show
Bug introduced by
The variable $query seems to be defined by a foreach iteration on line 121. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
147 12
                case 'archives':
148 3
                    list($from, $to) = $vars;
149 3
                    $vars = array($to * -1, $from * -1);
150 3
                    if ($count) {
151 12
                        return $this->db->value(array(
152 3
                            'SELECT COUNT(*) FROM blog',
153 3
                            'WHERE featured <= 0 AND published >= ? AND published <= ?',
154 3
                        ), $vars);
155
                    } else {
156 3
                        $posts = $this->db->ids(array(
157 3
                            'SELECT id FROM blog',
158 3
                            'WHERE featured <= 0 AND published >= ? AND published <= ?',
159 3
                            'ORDER BY featured, published ASC'.$params->limit,
160 3
                        ), $vars);
161
                    }
162 3
                    break;
163
164 9
                case 'authors':
165 1
                    if ($count) {
166 1
                        return $this->db->value(array(
167 1
                            'SELECT COUNT(*) FROM blog AS b',
168 1
                            'INNER JOIN authors ON b.author_id = authors.id',
169 1
                            'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path = ?',
170 1
                        ), $vars);
171 1
                    } else {
172 1
                        $posts = $this->db->ids(array(
173 1
                            'SELECT b.id FROM blog AS b',
174 1
                            'INNER JOIN authors ON b.author_id = authors.id',
175 1
                            'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path = ?',
176 1
                            'ORDER BY b.featured, b.published ASC'.$params->limit,
177 1
                        ), $vars);
178
                    }
179 1
                    break;
180
181 8
                case 'tags':
182 1
                    if ($count) {
183 1
                        return $this->db->value(array(
184 1
                            'SELECT COUNT(*) FROM tagged AS t',
185 1
                            'INNER JOIN blog AS b ON t.blog_id = b.id',
186 1
                            'INNER JOIN tags ON t.tag_id = tags.id',
187 1
                            'WHERE b.featured <= 0 AND b.published != 0 AND tags.path = ?',
188 1
                            'GROUP BY tags.id',
189 1
                        ), $vars);
190
                    } else {
191 1
                        $posts = $this->db->ids(array(
192 1
                            'SELECT b.id FROM tagged AS t',
193 1
                            'INNER JOIN blog AS b ON t.blog_id = b.id',
194 1
                            'INNER JOIN tags ON t.tag_id = tags.id',
195 1
                            'WHERE b.featured <= 0 AND b.published != 0 AND tags.path = ?',
196 1
                            'ORDER BY b.featured, b.published, b.updated ASC'.$params->limit,
197 1
                        ), $vars);
198
                    }
199 1
                    break;
200
201 7
                case 'categories':
202 4
                    if (isset($type['search'])) {
203 2
                        if (!is_string($vars)) {
204 1
                            $vars = $this->db->value('SELECT path FROM categories WHERE id = ?', array_shift($vars));
205 1
                        }
206 2
                        if (empty($vars)) {
207 1
                            return;
208
                        }
209 1
                        $phrase = $type['search'];
210 1
                        $category = 'blog/'.$vars;
211 2
                        $weights = (isset($type['weights']) && is_array($type['weights'])) ? $type['weights'] : array();
212 1
                        if ($count) {
213 1
                            $sitemap = new Sitemap();
214 1
                            $count = $sitemap->count($phrase, $category);
215 1
                            unset($sitemap);
216
217 1
                            return $count;
218
                        } else {
219 1
                            $sitemap = new Sitemap();
220 1
                            $includes = array();
221 1
                            foreach ($sitemap->search($phrase, $category, $params->limit, $weights) as $row) {
222 1
                                $posts[] = $row['id'];
223 1
                                $includes[$row['id']] = array('snippet' => $row['snippet'], 'words' => $row['words']);
224 1
                            }
225 1
                            unset($sitemap);
226 1
                            $posts = $this->info($posts);
227 1
                            foreach ($posts as $id => $row) {
228 1
                                $posts[$id] = array_merge($row, $includes[$id]);
229 1
                            }
230
231 1
                            return $posts;
232
                        }
233
                    }
234 3
                    if (!is_array($vars)) {
235 2
                        $hier = new Hierarchy($this->db, 'categories');
236 2
                        $vars = array_keys($hier->tree(array('path'), array('where' => 'path = '.$vars)));
237 2
                        unset($hier);
238 2
                    }
239 3
                    if (empty($vars)) {
240 1
                        return;
241
                    }
242 2
                    $categories = implode(', ', $vars);
243 2
                    if ($count) {
244 2
                        return $this->db->value(array(
245 2
                            'SELECT COUNT(*) FROM blog',
246 2
                            'WHERE featured <= 0 AND published != 0 AND category_id IN('.$categories.')',
247 2
                        ), $vars);
248
                    } else {
249 2
                        $posts = $this->db->ids(array(
250 2
                            'SELECT id FROM blog',
251 2
                            'WHERE featured <= 0 AND published != 0 AND category_id IN('.$categories.')',
252 2
                            'ORDER BY featured, published, updated ASC'.$params->limit,
253 2
                        ), $vars);
254
                    }
255 2
                    break;
256
257 4
                default:
258 4
                    if (isset($type['search'])) {
259 1
                        $phrase = $type['search'];
260 1
                        $weights = (isset($type['weights']) && is_array($type['weights'])) ? $type['weights'] : array();
261 1
                        if ($count) {
262 1
                            $sitemap = new Sitemap();
263 1
                            $count = $sitemap->count($phrase, 'blog');
264 1
                            unset($sitemap);
265
266 1
                            return $count;
267
                        } else {
268 1
                            $sitemap = new Sitemap();
269 1
                            $includes = array();
270 1
                            foreach ($sitemap->search($phrase, 'blog', $params->limit, $weights) as $row) {
271 1
                                $posts[] = $row['id'];
272 1
                                $includes[$row['id']] = array('snippet' => $row['snippet'], 'words' => $row['words']);
273 1
                            }
274 1
                            unset($sitemap);
275 1
                            $posts = $this->info($posts);
276 1
                            foreach ($posts as $id => $row) {
277 1
                                $posts[$id] = array_merge($row, $includes[$id]);
278 1
                            }
279
280 1
                            return $posts;
281
                        }
282
                    }
283 3
                    if ($count) {
284 2
                        return $this->db->value(array(
285 2
                            'SELECT COUNT(*) FROM blog',
286 2
                            'WHERE featured <= 0 AND published < 0',
287 2
                        ), $vars);
288
                    } else {
289 2
                        $posts = $this->db->ids(array(
290 2
                            'SELECT id FROM blog',
291 2
                            'WHERE featured <= 0 AND published < 0',
292 2
                            'ORDER BY featured, published ASC'.$params->limit,
293 2
                        ), $vars);
294
                    }
295 2
                    break;
296 4
            }
297
298 9
            return $this->info($posts);
0 ignored issues
show
Documentation introduced by
$posts is of type array|false, but the function expects a integer|array<integer,integer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
299
        }
300
        switch ($type) {
301 11
            case 'similar': // optional (string|array) keywords (ordered by RANK) - default Page::html()->keywords
302 1
                $keywords = Page::html()->keywords;
303 1
                $limit = $params;
304 1
                if (is_array($params)) {
305 1
                    list($limit, $keywords) = (count($params) == 1) ? each($params) : $params;
306 1
                }
307 1
                if (!empty($keywords) && !empty($limit) && is_numeric($limit)) {
308 1
                    if (!is_array($keywords)) {
309 1
                        $keywords = array_map('trim', explode(',', $keywords));
310 1
                    }
311 1
                    if (!empty($keywords)) {
312 1
                        $current = Page::html()->url['path'];
313 1
                        if (empty($current)) {
314 1
                            $current = 'index';
315 1
                        }
316 1
                        $sitemap = new Sitemap();
317 1
                        foreach ($sitemap->search('"'.implode('" OR "', $keywords).'"', 'blog', $limit, array(0, 0, 0, 1, 0), 'AND m.path != "'.$current.'"') as $row) {
318 1
                            $posts[] = $row['id'];
319 1
                        }
320 1
                        unset($sitemap);
321 1
                        $posts = $this->info($posts);
322 1
                    }
323 1
                }
324 1
                break;
325
326 10
            case 'posts': // (array) paths (limit and order inherent)
327 1
                if (!empty($params)) {
328 1
                    foreach ((array) $params as $path) {
329 1
                        $posts[$path] = '';
330 1
                    }
331 1
                    foreach ($this->db->all(array(
332 1
                        'SELECT path, id',
333 1
                        'FROM blog',
334 1
                        'WHERE path IN('.implode(', ', array_fill(0, count($posts), '?')).')',
335 1
                    ), array_keys($posts), 'assoc') as $row) {
336 1
                        $posts[$row['path']] = $row['id'];
337 1
                    }
338 1
                    $posts = $this->info(array_values(array_filter($posts)));
339 1
                }
340 1
                break;
341
342 9
            case 'archives': // optional (array) years (ordered by month DESC, and only starts if count > 0) - default all, no limit
343 4
                $years = (is_array($params)) ? $params : array();
344 1
                if (empty($years)) {
345 1
                    $times = $this->db->row('SELECT ABS(MAX(published)) AS begin, ABS(MIN(published)) AS end FROM blog WHERE featured <= 0 AND published < 0', '', 'assoc');
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
346 1
                    if (!is_null($times['end'])) {
347 1
                        $years = range(date('Y', $times['begin']), date('Y', $times['end']));
348 1
                    }
349 1
                }
350 1
                $months = array('Jan' => 1, 'Feb' => 2, 'Mar' => 3, 'Apr' => 4, 'May' => 5, 'Jun' => 6, 'Jul' => 7, 'Aug' => 8, 'Sep' => 9, 'Oct' => 10, 'Nov' => 11, 'Dec' => 12);
351 1
                $archives = array();
352 1
                foreach ($years as $Y) {
353 1
                    foreach ($months as $M => $n) {
354 1
                        $to = mktime(23, 59, 59, $n + 1, 0, $Y) * -1;
355 1
                        $from = mktime(0, 0, 0, $n, 1, $Y) * -1;
356 1
                        $archives[] = "SUM(CASE WHEN featured <= 0 AND published >= {$to} AND published <= {$from} THEN 1 ELSE 0 END) AS {$M}{$Y}";
357 1
                    }
358 1
                }
359 1
                if (!empty($archives) && $archives = $this->db->row(array('SELECT', implode(",\n", $archives), 'FROM blog'), '', 'assoc')) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
360 1
                    $page = Page::html();
361 1
                    foreach ($archives as $date => $count) {
362 1
                        $time = mktime(0, 0, 0, $months[substr($date, 0, 3)], 15, substr($date, 3));
363 1
                        list($Y, $M, $m) = explode(' ', date('Y M m', $time));
364 1
                        if (!isset($posts[$Y])) {
365 1
                            $posts[$Y] = array('count' => 0, 'url' => $page->url('blog/listings', 'archives', $Y));
366 1
                        }
367 1
                        $posts[$Y]['months'][$M] = array('url' => $page->url('blog/listings', 'archives', $Y, $m), 'count' => $count, 'time' => $time);
368 1
                        $posts[$Y]['count'] += $count;
369 1
                    }
370 1
                }
371 1
                break;
372
373 8
            case 'authors': // optional (int) limit or (string) path (ordered by count DESC, then author name ASC)
374 2
                $path = (!is_int($params) && !empty($params)) ? (string) $params : '';
375 2
                $operator = (!empty($path)) ? '=' : '!=';
376 2
                $authors = $this->db->all(array(
377 2
                    'SELECT COUNT(*) AS count, authors.path, authors.name, ABS(MIN(b.published)) AS latest',
378 2
                    'FROM blog AS b',
379 2
                    'INNER JOIN authors ON b.author_id = authors.id',
380 2
                    'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND authors.path '.$operator.' ?',
381 2
                    'GROUP BY authors.id',
382 2
                    'ORDER BY authors.name ASC',
383 2
                ), $path, 'assoc');
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
384 2
                $authored = array();
385 2
                foreach ($authors as $author) {
386 2
                    $authored[$author['path']] = $author['count'];
387 2
                }
388 2
                arsort($authored);
389 2
                if (is_int($params)) {
390 1
                    $authored = array_slice($authored, 0, $params, true);
391 1
                }
392 2
                foreach ($authors as $author) {
393 2
                    if (isset($authored[$author['path']])) {
394 2
                        $info = $this->configInfo('authors', $author['path'], $author['name']);
395 2
                        $info['latest'] = $author['latest'];
396 2
                        $info['count'] = $author['count'];
397 2
                        $posts[] = $info;
398 2
                    }
399 2
                }
400 2
                if (!empty($path) && !empty($posts)) {
401 1
                    $posts = array_shift($posts);
402 1
                }
403 2
                break;
404
405 6
            case 'tags': // optional (int) limit or (string) path (ordered by count DESC, then tag name ASC)
406 2
                $path = (!is_int($params) && !empty($params)) ? (string) $params : '';
407 2
                $operator = (!empty($path)) ? '=' : '!=';
408 2
                $tags = $this->db->all(array(
409 2
                    'SELECT COUNT(*) AS count, tags.path, tags.name, ABS(MIN(b.published)) AS latest',
410 2
                    'FROM tagged AS t',
411 2
                    'INNER JOIN blog AS b ON t.blog_id = b.id',
412 2
                    'INNER JOIN tags ON t.tag_id = tags.id',
413 2
                    'WHERE b.featured <= 0 AND b.published != 0 AND tags.path '.$operator.' ?',
414 2
                    'GROUP BY tags.id',
415 2
                    'ORDER BY tags.name ASC',
416 2
                ), $path, 'assoc');
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
417 2
                $tagged = array();
418 2
                foreach ($tags as $tag) {
419 2
                    $tagged[$tag['path']] = $tag['count'];
420 2
                }
421 2
                arsort($tagged);
422 2
                if (is_int($params)) {
423 1
                    $tagged = array_slice($tagged, 0, $params, true);
424 1
                }
425 2
                if (count($tagged) > 0) {
426
                    // http://en.wikipedia.org/wiki/Tag_cloud
427
                    // http://stackoverflow.com/questions/18790677/what-algorithm-can-i-use-to-sort-tags-for-tag-cloud?rq=1
428
                    // http://stackoverflow.com/questions/227/whats-the-best-way-to-generate-a-tag-cloud-from-an-array-using-h1-through-h6-fo
429 2
                    $min = min($tagged);
430 2
                    $range = max(.01, max($tagged) - $min) * 1.0001;
431 2
                    foreach ($tags as $tag) {
432 2
                        if (isset($tagged[$tag['path']])) {
433 2
                            $info = $this->configInfo('tags', $tag['path'], $tag['name']);
434 2
                            $info['latest'] = $tag['latest'];
435 2
                            $info['count'] = $tag['count'];
436 2
                            if (!empty($path)) {
437 1
                                $posts = $info;
438 1
                                break;
439
                            }
440 1
                            $info['rank'] = ceil(((4 * ($tag['count'] - $min)) / $range) + 1);
441 1
                            $posts[] = $info;
442 1
                        }
443 2
                    }
444 2
                }
445 2
                break;
446
447 4
            case 'categories': // (ordered by category name ASC) - no limit
448
                // http://www.smarty.net/docs/en/language.function.function.tpl
449 4
                if (is_array($params) && isset($params['nest']) && isset($params['tree'])) {
450 4
                    foreach ($params['nest'] as $id => $subs) {
451 4
                        $category = $params['tree'][$id];
452 4
                        $posts[$id] = $this->configInfo('categories', $category['path'], $category['name']);
453 4
                        $posts[$id]['count'] = $category['count'];
454 4
                        if (!empty($subs)) {
455 3
                            $posts[$id]['subs'] = $this->query('categories', array(
456 3
                                'nest' => $subs,
457 3
                                'tree' => $params['tree'],
458 3
                            ));
459 3
                        }
460 4
                    }
461
462 4
                    return array_values($posts);
463
                }
464 1
                $hier = new Hierarchy($this->db, 'categories');
465 1
                $tree = $hier->tree(array('path', 'name'));
466 1
                $counts = $hier->counts('blog', 'category_id');
467 1
                foreach ($tree as $id => $fields) {
468 1
                    $tree[$id]['count'] = $counts[$id];
469 1
                }
470 1
                $nest = $hier->nestify($tree);
471 1
                $slice = array();
472 1
                foreach ($nest as $id => $subs) {
473 1
                    if ($tree[$id]['count'] > 0) {
474 1
                        $slice[$id] = $tree[$id]['count'];
475 1
                    }
476 1
                }
477 1
                arsort($slice);
478 1
                if (is_int($params)) {
479 1
                    $slice = array_slice($slice, 0, $params, true);
480 1
                }
481 1
                foreach ($nest as $id => $subs) {
482 1
                    if (!isset($slice[$id])) {
483 1
                        unset($nest[$id]);
484 1
                    }
485 1
                }
486 1
                if (!empty($slice)) {
487 1
                    $posts = $this->query('categories', array(
488 1
                        'nest' => $nest,
489 1
                        'tree' => $tree,
490 1
                    ));
491 1
                }
492 1
                break;
493
        }
494
495 8
        return $posts;
496
    }
497
498
    /**
499
     * Analyzes the blog file $path and performes any database CRUD operations that may be needed.  We do this every time a blog page is visited, but this helps to speed up the process if you are doing things programatically.  If you are making lots of changes, then just delete the Blog.db file and everything will be updated.
500
     * 
501
     * @param string $path The url (seo) path, NOT a file system path.
502
     * 
503
     * @return false|int False if the $path does not exist, or the database id if it does.
504
     */
505 22
    public function file($path)
506
    {
507 22
        if (empty($path)) {
508 2
            $path = 'index';
509 2
        }
510 22
        $blog = $this->db->row('SELECT id, path, updated, search, content FROM blog WHERE path = ?', $path, 'assoc');
0 ignored issues
show
Documentation introduced by
$path is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
511 22
        if (!$current = $this->blogInfo($path)) {
512 15
            if ($blog) { // then remove
513 2
                $this->db->exec('DELETE FROM blog WHERE id = ?', $blog['id']);
514 1
                $this->db->exec('DELETE FROM tagged WHERE blog_id = ?', $blog['id']);
515 1
                $sitemap = new Sitemap();
516 1
                $sitemap->delete($blog['path']);
517 1
                unset($sitemap);
518 1
            }
519
520 15
            return false;
521
        }
522
        
523 8
        if ($blog) { // update maybe
524 8
            foreach (array('updated', 'search', 'content') as $field) {
525 8
                if ($current[$field] != $blog[$field]) {
526 1
                    $updated = $this->db->update('blog', 'id', array($blog['id'] => $current));
527 1
                    $this->db->exec('DELETE FROM tagged WHERE blog_id = ?', $blog['id']);
528 1
                    break;
529
                }
530 8
            }
531 8
            if (!isset($updated)) {
532 8
                return $blog['id'];
533
            }
534 1
        } else { // insert
535 1
            $blog = $current;
536 1
            $blog['id'] = $this->db->insert('blog', $current);
537
        }
538
        
539 1
        if (!empty($current['keywords'])) {
540 1
            $tags = array_filter(array_map('trim', explode(',', $current['keywords'])));
541 1
            foreach ($tags as $tag) {
542 1
                $this->db->insert('tagged', array(
543 1
                    'blog_id' => $blog['id'],
544 1
                    'tag_id' => $this->getId('tags', $tag),
545 1
                ));
546 1
            }
547 1
        }
548 1
        $sitemap = new Sitemap();
549 1
        if (!$current['search']) {
550 1
            $sitemap->delete($blog['path']);
551 1
        } else {
552 1
            $category = 'blog';
553 1
            if ($current['category_id'] > 0) {
554 1
                $category .= '/'.array_search($current['category_id'], $this->ids['categories']);
555 1
            }
556 1
            $sitemap->upsert($category, array(
557 1
                'id' => $blog['id'],
558 1
                'path' => $blog['path'],
559 1
                'title' => $current['title'],
560 1
                'description' => $current['description'],
561 1
                'keywords' => $current['keywords'],
562 1
                'thumb' => $current['thumb'],
563 1
                'content' => $current['content'],
564 1
                'updated' => $current['updated'],
565 1
            ));
566
        }
567 1
        unset($sitemap);
568 1
        $this->updateConfig();
569
570 1
        return $blog['id'];
571
    }
572
573
    /**
574
     * Gets all of the information we have for the blog $id(s) you supply.
575
     * 
576
     * @param int|int[] $ids That correspond to the database.
577
     * 
578
     * @return array A single row of information if $ids is not an array, or multiple rows foreach id in the same order given that you can loop through.
579
     */
580 20
    public function info($ids)
581
    {
582 20
        $page = Page::html();
583 20
        $single = (is_array($ids)) ? false : true;
584 20
        if (empty($ids)) {
585 1
            return array();
586
        }
587 20
        $ids = (array) $ids;
588 20
        $posts = array_flip($ids);
589 20
        foreach ($this->db->all(array(
590 20
            'SELECT b.id, b.path, b.thumb, b.title, b.description, b.content, ABS(b.updated) AS updated, ABS(b.featured) AS featured, ABS(b.published) AS published,',
591 20
            '  a.id AS author_id, a.path AS author_path, a.name AS author_name,',
592 20
            '  (SELECT p.path || "," || p.title FROM blog AS p WHERE p.featured = b.featured AND p.published > b.published AND p.published < 0 ORDER BY p.featured, p.published ASC LIMIT 1) AS previous,',
593 20
            '  (SELECT n.path || "," || n.title FROM blog AS n WHERE n.featured = b.featured AND n.published < b.published AND n.published < 0 ORDER BY n.featured, n.published DESC LIMIT 1) AS next,',
594 20
            '  (SELECT GROUP_CONCAT(p.path) FROM categories AS c INNER JOIN categories AS p WHERE c.lft BETWEEN p.lft AND p.rgt AND c.id = b.category_id ORDER BY c.lft) AS category_paths,',
595 20
            '  (SELECT GROUP_CONCAT(p.name) FROM categories AS c INNER JOIN categories AS p WHERE c.lft BETWEEN p.lft AND p.rgt AND c.id = b.category_id ORDER BY c.lft) AS category_names,',
596 20
            '  (SELECT GROUP_CONCAT(t.path) FROM tagged INNER JOIN tags AS t ON tagged.tag_id = t.id WHERE tagged.blog_id = b.id) AS tag_paths,',
597 20
            '  (SELECT GROUP_CONCAT(t.name) FROM tagged INNER JOIN tags AS t ON tagged.tag_id = t.id WHERE tagged.blog_id = b.id) AS tag_names',
598 20
            'FROM blog AS b',
599 20
            'LEFT JOIN authors AS a ON b.author_id = a.id',
600 20
            'WHERE b.id IN('.implode(', ', $ids).')',
601 20
        ), '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
602
            $post = array(
603 20
                'page' => true, // until proven otherwise
604 20
                'path' => $row['path'],
605 20
                'url' => $page->url('base', $row['path']),
606 20
                'thumb' => $row['thumb'],
607 20
                'title' => $row['title'],
608 20
                'description' => $row['description'],
609 20
                'content' => $row['content'],
610 20
                'updated' => $row['updated'],
611 20
                'featured' => ($row['featured'] > 0) ? true : false,
612 20
                'published' => ($row['published'] > 1) ? $row['published'] : (($row['published'] == 1) ? true : false),
613 20
                'categories' => array(),
614 20
                'tags' => array(),
615 20
            );
616 20
            if (!empty($row['category_paths'])) {
617 16
                $cats = array_combine(explode(',', $row['category_paths']), explode(',', $row['category_names']));
618 16
                foreach ($cats as $path => $name) {
619 16
                    $post['categories'][] = $this->configInfo('categories', $path, $name);
620 16
                }
621 16
            }
622 20
            if (!empty($row['tag_paths'])) {
623 17
                $tags = array_combine(explode(',', $row['tag_paths']), explode(',', $row['tag_names']));
624 17
                foreach ($tags as $path => $name) {
625 17
                    $post['tags'][] = $this->configInfo('tags', $path, $name);
626 17
                }
627 17
            }
628 20
            if ($row['published'] > 1) {
629 17
                $post['page'] = false;
630 17
                $post['author'] = $this->configInfo('authors', $row['author_path'], $row['author_name']);
631 17
                $post['archive'] = $page->url('blog/listings', 'archives', date('Y/m/d/', $row['published']));
632 17
                $post['previous'] = $row['previous'];
633 17
                $post['next'] = $row['next'];
634 17
                if ($post['previous']) {
635 11
                    $previous = explode(',', $post['previous']);
636 11
                    $post['previous'] = array(
637 11
                        'url' => $page->url('base', array_shift($previous)),
638 11
                        'title' => implode(',', $previous),
639
                    );
640 11
                }
641 17
                if ($post['next']) {
642 14
                    $next = explode(',', $post['next']);
643 14
                    $post['next'] = array(
644 14
                        'url' => $page->url('base', array_shift($next)),
645 14
                        'title' => implode(',', $next),
646
                    );
647 14
                }
648 17
            }
649 20
            $posts[$row['id']] = $post;
650 20
        }
651
652 20
        return ($single) ? array_shift($posts) : $posts;
653
    }
654
655 27
    public function config($table = null, $path = null, $value = null)
656
    {
657 27
        if (is_null($this->config)) {
658 24
            $file = $this->folder.'config.yml';
659 24
            $this->config = (is_file($file)) ? (array) Yaml::parse(file_get_contents($file)) : array();
660
            foreach (array(
661 24
                'listings' => 'blog',
662 24
                'breadcrumb' => 'Blog',
663 24
                'name' => '',
664 24
                'thumb' => '',
665 24
                'summary' => '',
666 24
            ) as $key => $val) {
667 24
                if (!isset($this->config['blog'][$key]) || is_array($this->config['blog'][$key])) {
668 2
                    $this->config['blog'][$key] = $val;
669 2
                }
670 24
            }
671 24
            $page = Page::html();
0 ignored issues
show
Unused Code introduced by
$page is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
672 24
            foreach (array('authors', 'categories', 'tags') as $param) {
673 24
                if (!isset($this->config[$param]) || !is_array($this->config[$param])) {
674 1
                    $this->config[$param] = array();
675 1
                }
676 24
                foreach ($this->config[$param] as $key => $val) {
677 23
                    if (is_string($val)) {
678 23
                        $this->config[$param][$key] = array('name' => $val);
679 23
                    }
680 24
                }
681 24
            }
682 24
        }
683 27
        switch (func_num_args()) {
684 27
            case 0:
685 1
                $param = $this->config;
686 1
                break;
687 27
            case 1:
688 25
                $param = (isset($this->config[$table])) ? $this->config[$table] : null;
689 25
                break;
690 27
            case 2:
691 27
                $param = (isset($this->config[$table][$path])) ? $this->config[$table][$path] : null;
692 27
                break;
693 3
            default:
694 3
                $param = (isset($this->config[$table][$path][$value])) ? $this->config[$table][$path][$value] : null;
695 3
                break;
696 27
        }
697
698 27
        return $param;
699
    }
700
701 22
    private function configInfo($table, $path, $name)
702
    {
703 22
        if (empty($path)) {
704 11
            return array();
705
        }
706 20
        if (!$config = $this->config($table, $path)) {
707 1
            $config = array();
708 1
        }
709 20
        unset($config['path'], $config['url']);
710 20
        $page = Page::html();
711 20
        $config = array_merge(array(
712 20
            'name' => $name,
713 20
            'path' => $path,
714 20
            'url' => ($table == 'categories') ? $page->url('base', $path) : $page->url('blog/listings', $table, $path),
715 20
            'thumb' => '',
716 20
        ), $config);
717 20
        if (!empty($config['thumb'])) {
718 14
            $config['thumb'] = $page->url('blog/config', $config['thumb']);
719 14
        }
720
        
721 20
        return $config;
722
    }
723
724 23
    private function blogInfo($path)
725
    {
726 23
        $page = Page::html();
727 23
        $file = $this->folder.'content/'.$path.'/index.tpl';
728 23
        if (!is_file($file)) {
729 15
            return false;
730
        }
731 9
        $page->set(array(), 'reset');
732 9
        if (preg_match('/^\s*{\*(?P<meta>.*)\*}/sU', file_get_contents($file), $matches)) {
733 9
            $values = Yaml::parse($matches['meta']);
734 9
            if (is_array($values)) {
735 9
                $page->set($values);
736 9
            }
737 9
        }
738 9
        $content = trim($this->theme->fetchSmarty($file));
739 9
        if ($page->markdown === true) {
740 5
            $content = $page->format('markdown', $content);
741 5
        }
742 9
        $page->markdown = null;
743 9
        $published = $page->published;
744 9
        if (is_string($published) && ($date = strtotime($published))) {
745 5
            $published = $date * -1; // a post
746 9
        } elseif ($published === true) {
747 5
            $published = 1; // a page
748 5
        } else {
749 1
            $published = 0; // unpublished
750
        }
751
752
        return array(
753 9
            'path' => $path,
754 9
            'title' => (string) $page->title,
755 9
            'description' => (string) $page->description,
756 9
            'keywords' => (string) $page->keywords,
757 9
            'theme' => (string) $page->theme,
758 9
            'thumb' => (string) $page->thumb,
759 9
            'featured' => ($page->featured === true) ? -1 : 0,
760 9
            'published' => $published,
761 9
            'updated' => filemtime($file) * -1,
762 9
            'author_id' => $this->getId('authors', (string) $page->author),
763 9
            'category_id' => $this->getId('categories', $path),
764 9
            'search' => ($published === 0 || $page->robots === false) ? 0 : 1,
765 9
            'content' => $content,
766 9
        );
767
    }
768
769 2
    private function updateDatabase()
770
    {
771 2
        $blog = $this->db->insert('blog', array('path', 'title', 'description', 'keywords', 'theme', 'thumb', 'featured', 'published', 'updated', 'author_id', 'category_id', 'search', 'content'));
772 2
        $tagged = $this->db->insert('tagged', array('blog_id', 'tag_id'));
773 2
        $sitemap = new Sitemap();
774 2
        $sitemap->reset('blog');
775 2
        $finder = new Finder();
776 2
        $finder->files()->in($this->folder.'content')->name('index.tpl')->sortByName();
777 2
        foreach ($finder as $file) {
778 1
            $path = str_replace('\\', '/', $file->getRelativePath());
779 1
            if ($info = $this->blogInfo($path)) {
780 1
                $id = $this->db->insert($blog, array_values($info));
781 1
                if (!empty($info['keywords'])) {
782 1
                    $tags = array_filter(array_map('trim', explode(',', $info['keywords'])));
783 1
                    foreach ($tags as $tag) {
784 1
                        $this->db->insert($tagged, array($id, $this->getId('tags', $tag)));
785 1
                    }
786 1
                }
787 1
                $category = 'blog';
788 1
                if ($info['category_id'] > 0) {
789 1
                    $category .= '/'.array_search($info['category_id'], $this->ids['categories']);
790 1
                }
791 1
                if ($info['search']) {
792 1
                    $sitemap->upsert($category, array(
793 1
                        'id' => $id,
794 1
                        'path' => $info['path'],
795 1
                        'title' => $info['title'],
796 1
                        'description' => $info['description'],
797 1
                        'keywords' => $info['keywords'],
798 1
                        'thumb' => $info['thumb'],
799 1
                        'content' => $info['content'],
800 1
                        'updated' => $info['updated'],
801 1
                    ));
802 1
                }
803 1
            }
804 2
        }
805 2
        $sitemap->delete();
806 2
        unset($sitemap);
807 2
        $this->db->close($blog);
808 2
        $this->db->close($tagged);
809 2
        $this->updateConfig();
810 2
    }
811
812 10
    private function getId($table, $value)
813
    {
814 10
        if (is_null($this->ids)) {
815 10
            $this->ids = array(
816
                'updated' => array(
817 10
                    'categories' => false,
818 10
                    'authors' => false,
819 10
                    'tags' => false,
820 10
                ),
821
            );
822 10
        }
823 10
        if ($table == 'updated') {
824 3
            if (isset($this->ids['updated'][$value])) {
825 2
                return $this->ids['updated'][$value];
826
            }
827
828 3
            return (in_array(true, $this->ids['updated'])) ? true : false;
829
        }
830 9
        if ($table == 'categories') {
831 9
            $value = (($slash = strrpos($value, '/')) !== false) ? substr($value, 0, $slash) : '';
832 9
        }
833 9
        if (!isset($this->ids['updated'][$table]) || empty($value)) {
834 7
            return 0;
835
        }
836 5
        if (!isset($this->ids[$table])) {
837 5
            $this->ids[$table] = array('' => 0);
838 5
            foreach ($this->db->all('SELECT path, id FROM '.$table, '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
839 4
                $this->ids[$table][$row['path']] = $row['id'];
840 5
            }
841 5
        }
842 5
        $path = Page::html()->format('url', $value, ($table == 'categories') ? 'slashes' : false);
843 5
        if (!isset($this->ids[$table][$path])) {
844 2
            $this->ids['updated'][$table] = true;
845 2
            if ($table == 'categories') {
846 1
                $parent = 0;
847 1
                $previous = '';
848 1
                foreach (explode('/', $path) as $uri) {
849 1
                    if (!isset($this->ids['categories'][$previous.$uri])) {
850 1
                        $category = ($name = $this->config($table, $previous.$uri, 'name')) ? $name : ucwords(str_replace('-', ' ', $uri));
851 1
                        $this->ids['categories'][$previous.$uri] = $this->db->insert('categories', array(
852 1
                            'path' => $previous.$uri,
853 1
                            'name' => $category,
854 1
                            'parent' => $parent,
855 1
                        ));
856 1
                    }
857 1
                    $parent = $this->ids['categories'][$previous.$uri];
858 1
                    $previous .= $uri.'/';
859 1
                }
860 1
            } else {
861 2
                if ($name = $this->config($table, $path, 'name')) {
862 1
                    $value = $name;
863 1
                }
864 2
                $this->ids[$table][$path] = $this->db->insert($table, array('path' => $path, 'name' => $value));
865
            }
866 2
        }
867
868 5
        return $this->ids[$table][$path];
869
    }
870
871 3
    private function updateConfig()
872
    {
873 3
        if ($this->getId('updated', 'anything') === false) {
874 2
            return;
875
        }
876 2
        $yaml = array();
877
878
        // Blog
879 2
        $yaml['blog'] = $this->config('blog');
880
881
        // Authors
882 2
        $yaml['authors'] = array();
883 2
        $authors = $this->config('authors');
884 2
        foreach ($this->db->all(array(
885 2
            'SELECT authors.path, authors.name',
886 2
            'FROM blog AS b',
887 2
            'INNER JOIN authors ON b.author_id = authors.id',
888 2
            'WHERE b.featured <= 0 AND b.published < 0 AND b.updated < 0 AND b.author_id != 0',
889 2
            'GROUP BY authors.id',
890 2
            'ORDER BY authors.name ASC',
891 2
        ), '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
892 2
            $merge = (isset($authors[$row['path']])) ? $authors[$row['path']] : array();
893 2
            $yaml['authors'][$row['path']] = array_merge(array(
894 2
                'name' => $row['name'],
895 2
                'thumb' => '',
896 2
            ), $merge);
897 2
            unset($authors[$row['path']]);
898 2
        }
899 2
        foreach ($authors as $path => $values) {
900 2
            $yaml['authors'][$path] = $values;
901 2
        }
902
903
        // Categories
904 2
        $yaml['categories'] = array();
905 2
        $categories = $this->config('categories');
906 2
        $hier = new Hierarchy($this->db, 'categories');
907 2
        if ($this->getId('updated', 'categories')) {
908 1
            $hier->refresh('name');
909 1
        }
910 2
        $tree = $hier->tree(array('path', 'name'));
911 2
        unset($hier);
912 2
        foreach ($tree as $row) {
913 2
            $merge = (isset($categories[$row['path']])) ? $categories[$row['path']] : array();
914 2
            $yaml['categories'][$row['path']] = array_merge(array(
915 2
                'name' => $row['name'],
916 2
            ), $merge);
917 2
            unset($categories[$row['path']]);
918 2
        }
919 2
        foreach ($categories as $path => $values) {
920 2
            $yaml['categories'][$path] = $values;
921 2
        }
922 2
        foreach ($yaml['categories'] as $path => $values) {
923 2
            if (count($values) == 1) {
924 2
                $yaml['categories'][$path] = array_shift($values);
925 2
            }
926 2
        }
927
928
        // Tags
929 2
        $yaml['tags'] = array();
930 2
        $tags = $this->config('tags');
931 2
        foreach ($this->db->all(array(
932 2
            'SELECT tags.path, tags.name',
933 2
            'FROM tagged AS t',
934 2
            'INNER JOIN tags ON t.tag_id = tags.id',
935 2
            'GROUP BY tags.id',
936 2
            'ORDER BY tags.name ASC',
937 2
        ), '', 'assoc') as $row) {
0 ignored issues
show
Documentation introduced by
'' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
938 2
            $merge = (isset($tags[$row['path']])) ? $tags[$row['path']] : array();
939 2
            $yaml['tags'][$row['path']] = array_merge(array(
940 2
                'name' => $row['name'],
941 2
            ), $merge);
942 2
            unset($tags[$row['path']]);
943 2
        }
944 2
        foreach ($tags as $path => $values) {
945 2
            $yaml['tags'][$path] = $values;
946 2
        }
947 2
        foreach ($yaml['tags'] as $path => $values) {
948 2
            if (count($values) == 1) {
949 2
                $yaml['tags'][$path] = array_shift($values);
950 2
            }
951 2
        }
952 2
        file_put_contents($this->folder.'config.yml', Yaml::dump($yaml, 3));
953 2
    }
954
}
955