TocProcess   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 2
Metric Value
wmc 15
c 7
b 0
f 2
lcom 1
cbo 3
dl 0
loc 53
ccs 29
cts 29
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 13 3
C addTocEntries() 0 21 11
1
<?php
2
namespace Bookdown\Bookdown\Process\Toc;
3
4
use Psr\Log\LoggerInterface;
5
use Bookdown\Bookdown\Content\Page;
6
use Bookdown\Bookdown\Content\IndexPage;
7
use Bookdown\Bookdown\Process\ProcessInterface;
8
9
class TocProcess implements ProcessInterface
10
{
11
    protected $logger;
12
    protected $tocEntries;
13
14 12
    public function __construct(LoggerInterface $logger)
15
    {
16 12
        $this->logger = $logger;
17 12
    }
18
19 11
    public function __invoke(Page $page)
20
    {
21 11
        if (! $page->isIndex()) {
22 3
            $this->logger->info("    Skipping TOC entries for non-index {$page->getTarget()}");
23 3
            return;
24
        }
25
26 10
        $this->logger->info("    Adding TOC entries for {$page->getTarget()}");
27 10
        $this->tocEntries = array();
28
        // if there are multiple books, ensure correct toc level
29 10
        $this->addTocEntries($page, $page->getConfig()->getTocDepth(), $page->isRoot() ? 0 : 1);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Bookdown\Bookdown\Content\Page as the method getConfig() does only exist in the following sub-classes of Bookdown\Bookdown\Content\Page: Bookdown\Bookdown\Content\IndexPage, Bookdown\Bookdown\Content\RootPage. 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...
Compatibility introduced by
$page of type object<Bookdown\Bookdown\Content\Page> is not a sub-type of object<Bookdown\Bookdown\Content\IndexPage>. It seems like you assume a child class of the class Bookdown\Bookdown\Content\Page to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
30 10
        $page->setTocEntries($this->tocEntries);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Bookdown\Bookdown\Content\Page as the method setTocEntries() does only exist in the following sub-classes of Bookdown\Bookdown\Content\Page: Bookdown\Bookdown\Content\IndexPage, Bookdown\Bookdown\Content\RootPage. 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...
31 10
    }
32
33
    /**
34
     * A toc depth of 0 means render all headings. A toc depth of 1 is a special case
35
     *
36
     * @param IndexPage $index
37
     * @param $tocDepth
38
     * @param int $level
39
     */
40 10
    protected function addTocEntries(IndexPage $index, $tocDepth, $level = 0)
41
    {
42 10
        $maxLevel = $level + $tocDepth;
43
44 10
        if ($tocDepth !== 1 && $tocDepth && $index->isRoot()) {
45 2
            $maxLevel --;
46 2
        }
47
48 10
        foreach ($index->getChildren() as $child) {
49 10
            $headings = $child->getHeadings();
50 10
            foreach ($headings as $heading) {
51 10
                if ($tocDepth && $heading->getLevel() > $maxLevel) {
52 5
                    continue;
53
                }
54 10
                $this->tocEntries[] = $heading;
55 10
            }
56 10
            if ($child->isIndex() && $tocDepth !== 1) {
57 5
                $this->addTocEntries($child, $tocDepth, $index->isRoot() ? $level : ($level + 1));
0 ignored issues
show
Compatibility introduced by
$child of type object<Bookdown\Bookdown\Content\Page> is not a sub-type of object<Bookdown\Bookdown\Content\IndexPage>. It seems like you assume a child class of the class Bookdown\Bookdown\Content\Page to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
58 5
            }
59 10
        }
60 10
    }
61
}
62