Completed
Push — feature-output-formats ( 8e593c...845a2b )
by Arnaud
02:32
created

Section   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 3
dl 0
loc 59
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B generate() 0 53 8
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\Page\Type;
14
15
/**
16
 * Class Section.
17
 */
18
class Section extends AbstractGenerator implements GeneratorInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function generate(PagesCollection $pagesCollection, \Closure $messageCallback)
24
    {
25
        $generatedPages = new PagesCollection('sections');
26
        $sectionsList = [];
27
        $sections = [];
28
29
        // identify sections
30
        /* @var $page Page */
31
        foreach ($pagesCollection as $page) {
32
            if ($page->getSection()) {
33
                // ie:
34
                // [blog][0] = blog/post-1
35
                // [blog][1] = blog/post-2
36
                $sectionsList[$page->getSection()][] = $page->getId();
37
            }
38
        }
39
40
        // sections collections
41
        // ie:
42
        // [blog] = Collection(blog)
43
        foreach ($sectionsList as $sectionName => $pagesList) {
44
            $sections[$sectionName] = new PagesCollection($sectionName);
45
            foreach ($pagesList as $pageId) {
46
                $sections[$sectionName]->add($pagesCollection->get($pageId));
0 ignored issues
show
Bug introduced by
It seems like $pagesCollection->get($pageId) targeting Cecil\Collection\Collection::get() can also be of type boolean; however, Cecil\Collection\Collection::add() does only seem to accept object<Cecil\Collection\ItemInterface>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
47
            }
48
        }
49
50
        // adds section to pages collection
51
        if (count($sections) > 0) {
52
            $menuWeight = 100;
53
            foreach ($sections as $section => $pages) {
54
                $pageId = $pathname = Page::slugify($section);
55
                if (!$pagesCollection->has($pageId)) {
56
                    //usort($pages, 'Cecil\Util::sortByDate');
57
                    $pages = $pages->sortByDate();
58
                    $page = (new Page())
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Cecil\Collection\Item as the method setPathname() does only exist in the following sub-classes of Cecil\Collection\Item: Cecil\Collection\Page\Page. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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 sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
59
                        ->setId($pageId)
60
                        ->setPathname($pathname)
61
                        ->setType(Type::SECTION)
62
                        ->setVariable('title', ucfirst($section))
63
                        ->setVariable('pages', $pages)
64
                        ->setVariable('date', $pages->getIterator()->current()->getVariable('date'))
65
                        ->setVariable('menu', [
66
                            'main' => ['weight' => $menuWeight],
67
                        ]);
68
                    $generatedPages->add($page);
69
                }
70
                $menuWeight += 10;
71
            }
72
        }
73
74
        return $generatedPages;
75
    }
76
}
77