TFBCLoadAdditionalContent   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 278
Duplicated Lines 11.87 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 33
loc 278
rs 9.0399
c 0
b 0
f 0
wmc 42
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
F loadAdditionalContent() 13 115 22
B findNextAndPrevious() 20 44 9
A orderToc() 0 27 4
B limitToc() 0 45 7

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like TFBCLoadAdditionalContent 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 TFBCLoadAdditionalContent, and based on these observations, apply Extract Interface, too.

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 TFBCLoadAdditionalContent
10
{
11
    /**
12
     * Load extra info into views based of meta information provided in each
13
     * view.
14
     *
15
     * @param array  &$views     with all views.
16
     * @param string $route      current route
17
     * @param string $routeIndex route with appended /index
18
     *
19
     * @throws NotFoundException when mapping can not be done.
20
     *
21
     * @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...
22
     */
23
    private function loadAdditionalContent(&$views, $route, $routeIndex)
24
    {
25
        foreach ($views as $id => $view) {
26
            $meta = isset($view["data"]["meta"])
27
                ? $view["data"]["meta"]
28
                : null;
29
30
            if (is_array($meta)) {
31
                switch ($meta["type"]) {
32
                    case "article-toc":
33
                        $content = $views["main"]["data"]["content"];
34
                        $views[$id]["data"]["articleToc"] = $this->di->textFilter->createToc($content);
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...
35
                        break;
36
37
                    case "breadcrumb":
38
                        $views[$id]["data"]["breadcrumb"] = $this->createBreadcrumb($route);
0 ignored issues
show
Bug introduced by
It seems like createBreadcrumb() 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...
39
                        break;
40
41 View Code Duplication
                    case "next-previous":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
42
                        $baseRoute = dirname($routeIndex);
43
                        list($next, $previous) = $this->findNextAndPrevious($routeIndex);
44
                        $views[$id]["data"]["next"] = $next;
45
                        $views[$id]["data"]["previous"] = $previous;
46
                        break;
47
48
                    case "single": // OBSOLETE
49
                    case "content":
50
                        $route = $this->getActiveRoute($meta["route"], $routeIndex);
0 ignored issues
show
Bug introduced by
It seems like getActiveRoute() 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...
51
52
                        // Load and parse route as view. Load meta view
53
                        // if any.
54
                        // Current view details preceds the loaded once.
55
                        $view = $this->loadAndParseRoute($route);
0 ignored issues
show
Bug introduced by
It seems like loadAndParseRoute() 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...
56
                        $views[$id] = array_merge_recursive_distinct($view, $views[$id]);
57
                        break;
58
59
                    case "columns":
60
                        // Each column is an own view set with details
61
                        // Process as meta view and load additional content
62
                        $template = isset($meta["template"])
63
                            ? $meta["template"]
64
                            : null;
65
                        $columns = $meta["columns"];
66
                        foreach ($columns as $key => $view) {
67
                            $views2 = [ "main" => $view ];
68
                            $this->loadAdditionalContent($views2, $route, $routeIndex);
69
                            $columns[$key] = $views2["main"];
70
                            
71
                            if ($template) {
72
                                $columns[$key]["template"] = $template;
73
                            }
74
                        }
75
                        $views[$id]["data"]["columns"] = $columns;
76
                        break;
77
78
                    case "toc-sort":
79
                        $baseRoute = dirname($routeIndex);
80
                        $this->orderToc($baseRoute, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array<string,?,{"type":"?"}>, 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...
81
                        break;
82
83 View Code Duplication
                    case "toc":
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
                        $baseRoute = dirname($routeIndex);
85
                        $toc = $this->meta[$baseRoute]["__toc__"];
0 ignored issues
show
Bug introduced by
The property meta 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...
86
                        $this->limitToc($toc, $meta);
87
                        $views[$id]["data"]["toc"] = $toc;
88
                        $views[$id]["data"]["meta"] = $meta;
89
                        break;
90
91
                    case "toc-route":
92
                        // Get the toc for a specific route
93
                        $route = $this->getActiveRoute($meta["route"], $routeIndex);
0 ignored issues
show
Bug introduced by
It seems like getActiveRoute() 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...
94
                        $routeIndex2 = $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...
95
                        $baseRoute = dirname($routeIndex2);
96
97
                        // Include support for ordering
98
                        if (isset($meta["orderby"])
99
                            || isset($meta["orderorder"])) {
100
                            $this->orderToc($baseRoute, $meta);
0 ignored issues
show
Documentation introduced by
$meta is of type array<string,?,{"type":"?","orderby":"?"}>, 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...
101
                        }
102
103
                        // Same as toc
104
                        $toc = $this->meta[$baseRoute]["__toc__"];
105
                        $this->limitToc($toc, $meta, $baseRoute);
106
                        $views[$id]["data"]["toc"] = $toc;
107
                        $views[$id]["data"]["meta"] = $meta;
108
                        break;
109
110
                    case "book-toc":
111
                        $toc = $this->meta[$baseRoute]["__toc__"];
0 ignored issues
show
Bug introduced by
The variable $baseRoute 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...
112
                        $views[$id]["data"]["toc"] = $toc;
113
                        break;
114
115
                    case "author":
116
                        if (isset($views["main"]["data"]["author"])) {
117
                            $views[$id]["data"]["author"] = $this->loadAuthorDetails($views["main"]["data"]["author"]);
0 ignored issues
show
Bug introduced by
It seems like loadAuthorDetails() 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...
118
                        }
119
                        break;
120
121
                    case "copy":
122
                        $viewToCopy = $views[$id]["data"]["meta"]["view"];
123
                        $views[$id]["data"] = array_merge_recursive_distinct(
124
                            $views[$viewToCopy]["data"],
125
                            $views[$id]["data"]
126
                        );
127
                        break;
128
129
                    default:
130
                        $msg = t("Unsupported data/meta/type '!TYPE' for additional content.", [
131
                            "!TYPE" => $meta["type"]
132
                        ]);
133
                        throw new Exception($msg);
134
                }
135
            }
136
        }
137
    }
138
139
140
141
    /**
142
     * Find next and previous links of current content.
143
     *
144
     * @param string $routeIndex target route to find next and previous for.
145
     *
146
     * @return array with next and previous if found.
147
     */
148
    private function findNextAndPrevious($routeIndex)
149
    {
150
        $key = dirname($routeIndex);
151
        if (!isset($this->meta[$key]["__toc__"])) {
152
            return [null, null];
153
        }
154
155
        $toc = $this->meta[$key]["__toc__"];
156
        if (!isset($toc[$routeIndex])) {
157
            return [null, null];
158
        }
159
160
        $index2Key = array_keys($toc);
161
        $keys = array_flip($index2Key);
162
        $values = array_values($toc);
163
        $count = count($keys);
164
165
        $current = $keys[$routeIndex];
166
        $previous = null;
167 View Code Duplication
        for ($i = $current - 1; $i >= 0; $i--) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
            $isSectionHeader = $values[$i]["sectionHeader"];
169
            $isInternal = $values[$i]["internal"];
170
            if ($isSectionHeader || $isInternal) {
171
                continue;
172
            }
173
            $previous = $values[$i];
174
            $previous["route"] = $index2Key[$i];
175
            break;
176
        }
177
        
178
        $next = null;
179 View Code Duplication
        for ($i = $current + 1; $i < $count; $i++) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
            $isSectionHeader = $values[$i]["sectionHeader"];
181
            $isInternal = $values[$i]["internal"];
182
            if ($isSectionHeader || $isInternal) {
183
                continue;
184
            }
185
            $next = $values[$i];
186
            $next["route"] = $index2Key[$i];
187
            break;
188
        }
189
190
        return [$next, $previous];
191
    }
192
193
194
195
    /**
196
     * Order toc items.
197
     *
198
     * @param string $baseRoute route to use to find __toc__.
199
     * @param string $meta on how to order toc.
200
     *
201
     * @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...
202
     */
203
    private function orderToc($baseRoute, $meta)
204
    {
205
        $defaults = [
206
            "orderby" => "section",
207
            "orderorder" => "asc",
208
        ];
209
        $options = array_merge($defaults, $meta);
210
        $orderby = $options["orderby"];
211
        $order   = $options["orderorder"];
212
        $toc = $this->meta[$baseRoute]["__toc__"];
213
214
        uksort($toc, function ($a, $b) use ($toc, $orderby, $order) {
215
                $a = $toc[$a][$orderby];
216
                $b = $toc[$b][$orderby];
217
218
                $asc = $order == "asc" ? 1 : -1;
219
                
220
            if ($a == $b) {
221
                return 0;
222
            } elseif ($a > $b) {
223
                return $asc;
224
            }
225
                return -$asc;
226
        });
227
        
228
        $this->meta[$baseRoute]["__toc__"] = $toc;
229
    }
230
231
232
    /**
233
     * Limit and paginate toc items.
234
     *
235
     * @param string &$toc      array with current toc.
236
     * @param string &$meta     on how to order and limit toc.
237
     * @param string $baseRoute prepend to next & previous urls.
238
     *
239
     * @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...
240
     */
241
    private function limitToc(&$toc, &$meta, $baseRoute = null)
242
    {
243
        $defaults = [
244
            "items" => 7,
245
            "offset" => 0,
246
        ];
247
        $options = array_merge($defaults, $meta);
248
249
        // Check if pagination is currently used
250
        if ($this->currentPage) {
251
            $options["offset"] = ($this->currentPage - 1) * $options["items"];
0 ignored issues
show
Bug introduced by
The property currentPage 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...
252
        }
253
254
        $meta["totalItems"] = count($toc);
255
        $meta["currentPage"] = (int) floor($options["offset"] / $options["items"]) + 1;
256
        $meta["totalPages"] = (int) floor($meta["totalItems"] / $options["items"] + 1);
257
258
        // Next and previous page
259
        $pagination = $this->config["pagination"];
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...
260
        $meta["nextPageUrl"] = null;
261
        $meta["previousPageUrl"] = null;
262
        $baseRoute = isset($baseRoute)
263
            ? $baseRoute
264
            : $this->baseRoute;
0 ignored issues
show
Bug introduced by
The property baseRoute 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...
265
266
        if ($meta["currentPage"] > 1 && $meta["totalPages"] > 1) {
267
            $previousPage = $meta["currentPage"] - 1;
268
            $previous = "";
269
            if ($previousPage != 1) {
270
                $previous = "$pagination/$previousPage";
271
            }
272
            $meta["previousPageUrl"] = "$baseRoute/$previous";
273
        }
274
275
        if ($meta["currentPage"] < $meta["totalPages"]) {
276
            $nextPage = $meta["currentPage"] + 1;
277
            $meta["nextPageUrl"] = "$baseRoute/$pagination/$nextPage";
278
        }
279
280
281
        // Only use slice of toc
282
        $startSlice = ($meta["currentPage"] - 1) * $options["items"];
283
        $toc = array_slice($toc, $startSlice, $options["items"]);
284
        $meta["displayedItems"] = count($toc);
285
    }
286
}
287