Completed
Push — master ( b5a796...c9a850 )
by Mikael
02:54
created

TFBCUtilities::getActiveRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Anax\Content;
4
5
/**
6
 * File Based Content, code for loading additional content into view through 
7
 * data["meta"].
8
 */
9
trait TFBCUtilities
10
{
11
    /**
12
     * Support relative routes.
13
     *
14
     * @param string $route      to load.
15
     * @param string $routeIndex to use.
16
     *
17
     * @return string with active route.
18
     */
19
    private function getActiveRoute($route, $routeIndex)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
20
    {
21
        if (substr_compare($route, "./", 0, 2) === 0) {
22
            $route = dirname($routeIndex) . "/" . substr($route, 2);
23
        }
24
25
        return $route;
26
    }
27
28
29
30
    /**
31
     * Load view data for additional route, merged with meta if any.
32
     *
33
     * @param string $route to load.
34
     *
35
     * @return array with view data details.
36
     */
37
    private function getDataForAdditionalRoute($route)
38
    {
39
        // From configuration
40
         $filter = $this->config["textfilter"];
0 ignored issues
show
Bug introduced by
The property config 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...
41
42
        // Get filtered content from route
43
        list(, , $filtered) =
44
            $this->mapRoute2Content($route);
0 ignored issues
show
Bug introduced by
It seems like mapRoute2Content() 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
46
        // Set data to be content of frontmatter, merged with meta
47
        $meta = $this->getMetaForRoute($route);
0 ignored issues
show
Bug introduced by
It seems like getMetaForRoute() 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...
48
        $data = $filtered->frontmatter;
49
        $data = array_merge_recursive_distinct($meta, $data);
50
        unset($data["__toc__"]);
51
        unset($data["views"]);
52
53
        // Do phase 2 processing
54
        $new = $this->di->get("textFilter")->parse($filtered->text, $filter);
0 ignored issues
show
Bug introduced by
The property di 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...
55
        
56
        // Creates urls based on baseurl
57
        $baseurl = isset($data["baseurl"])
58
            ? isset($data["baseurl"])
59
            : null;
60
        $this->addBaseurl2AnchorUrls($new, $baseurl);
0 ignored issues
show
Documentation introduced by
$baseurl is of type boolean|null, but the function expects a string.

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...
61
        $data["content"] = $new->text;
62
63
        return $data;
64
    }
65
66
67
68
    /**
69
     * Parse text, find and update all a href to use baseurl.
70
     *
71
     * @param object &$filtered with text and excerpt to process.
72
     * @param string $baseurl   add as baseurl for all relative urls.
73
     *
74
     * @return void.
0 ignored issues
show
Documentation introduced by
The doc-type void. could not be parsed: Unknown type name "void." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
75
     */
76
    private function addBaseurl2AnchorUrls(&$filtered, $baseurl)
77
    {
78
        $textf  = $this->di->get("textFilter");
79
        $url    = $this->di->get("url");
80
81
        // Use callback to url->create() instead of string concat
82
        $callback = function ($route) use ($url, $baseurl) {
83
            return $url->create($route, $baseurl);
84
        };
85
86
        $filtered->text =
87
            $textf->addBaseurlToRelativeLinks($filtered->text, $baseurl, $callback);
88
    }
89
90
91
92
    /**
93
     * Get published date.
94
     *
95
     * @param array $frontmatter with details on dates.
96
     *
97
     * @return integer as time for publish time.
98
     */
99
    private function getPublishTime($frontmatter)
100
    {
101
        list(, $date) = $this->di->get("view")->getPublishedDate($frontmatter);
102
        return strtotime($date);
103
    }
104
}
105