FBCBreadcrumbTrait::createBreadcrumb()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 0
cts 10
cp 0
rs 9.7666
cc 3
nc 2
nop 1
crap 12
1
<?php
2
3
namespace Anax\Content;
4
5
/**
6
 * File Based Content, code for creating breadcrumb.
7
 */
8
trait FBCBreadcrumbTrait
9
{
10
    /**
11
     * Create a breadcrumb, append slash / to all dirs.
12
     *
13
     * @param string $route      current route.
14
     *
15
     * @return array with values for the breadcrumb.
16
     */
17
    public function createBreadcrumb($route)
18
    {
19
        $breadcrumbs = [];
20
21
        while ($route !== "./" && $route !== "/") {
22
            $routeIndex = $this->mapRoute2IndexKey($route);
0 ignored issues
show
Bug introduced by
It seems like mapRoute2IndexKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
23
            $item["url"] = $route;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$item was never initialized. Although not strictly required by PHP, it is generally a good practice to add $item = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
24
            $item["text"] = $this->getBreadcrumbTitle($this->index[$routeIndex]["file"]);
0 ignored issues
show
Bug introduced by
The property index does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $item 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...
25
            $breadcrumbs[] = $item;
26
            $route = dirname($route) . "/";
27
        }
28
29
        krsort($breadcrumbs);
30
        return $breadcrumbs;
31
    }
32
33
34
35
    /**
36
     * Get the title of a document to use for breadcrumb.
37
     *
38
     * @param string $file to get title from.
39
     *
40
     * @return string as the breadcrumb title.
41
     */
42
    private function getBreadcrumbTitle($file)
43
    {
44
        $frontmatter = $this->getFrontmatter($file);
0 ignored issues
show
Bug introduced by
It seems like getFrontmatter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
45
        $title = $frontmatter["title"];
46
        if (isset($frontmatter["titleBreadcrumb"])) {
47
            $title = $frontmatter["titleBreadcrumb"];
48
        }
49
50
        return $title;
51
    }
52
}
53