PreviewPageRow   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 81
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A preview() 0 4 1
A Link() 0 9 2
A view() 0 4 1
D processRequest() 0 38 9
A PageRowsReadyForPublication() 0 4 1
1
<?php
2
3
4
class PreviewPageRow extends ContentController
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
{
6
    private static $url_segment = 'preview-page-row';
0 ignored issues
show
Unused Code introduced by
The property $url_segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
7
8
    private static $allowed_actions = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
        'preview' => 'ADMIN',
10
        'view' => true
11
    ];
12
13
    private $pageRows = null;
14
15
    public function preview($request)
16
    {
17
        return $notFoundResponse = $this->processRequest($request, false);
0 ignored issues
show
Unused Code introduced by
$notFoundResponse is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
18
    }
19
20
    /**
21
     *
22
     * @param string | null $action
23
     * @return string
24
     */
25
    public function Link($action = null)
26
    {
27
        $link = '/'.$this->Config()->get('url_segment').'/';
28
        if ($action) {
29
            $link .= $action . '/';
30
        }
31
32
        return $link;
33
    }
34
35
    public function view($request)
36
    {
37
        return $this->processRequest($request, true);
38
    }
39
40
    protected function processRequest($request, $readyForPublic)
41
    {
42
        $id = $request->param('ID');
43
        $pageRowClassNames = ClassInfo::subclassesFor('PageRow');
44
        $listOfClasses = [];
45
        foreach ($pageRowClassNames as $key => $class) {
0 ignored issues
show
Bug introduced by
The expression $pageRowClassNames of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
46
            $listOfClasses[strtolower($class)] = $class;
47
        }
48
        $errorResponse = null;
49
        PageRow::set_current_page_object(Page::get()->first());
50
        if ($id == 'all') {
51
            $this->pageRows = PageRow::get();
52
        } elseif ($id == 'oneofeach') {
53
            $tempList = PageRow::get()->sort('RAND()');
54
            $oneIDPerClassNameArray = [];
55
            foreach ($tempList as $obj) {
56
                $oneIDPerClassNameArray[$obj->ClassName] = $obj->ID;
57
            }
58
            $this->pageRows = PageRow::get()->filter(['ID' => $oneIDPerClassNameArray]);
59
        } elseif (isset($listOfClasses[$id])) {
60
            $this->pageRows = PageRow::get()->filter(['ClassName' => $listOfClasses[$id]]);
61
        } else {
62
            $id = intval($id);
63
            $this->pageRows = PageRow::get()->filter(['ID' =>$id]);
64
        }
65
        if ($readyForPublic) {
66
            $this->pageRows = $this->pageRows->filter(['ReadyForPublication' => 1]);
67
        }
68
        $this->pageRows = $this->pageRows->exclude(['ClassName' => 'DownloadBlock']);
69
        if (! $this->pageRows->count()) {
70
            $errorResponse = $this->httpError(404, 'The requested page block could not be found.');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $errorResponse is correct as $this->httpError(404, 'T...k could not be found.') (which targets ContentController::httpError()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
        }
72
        if ($errorResponse) {
73
            return $errorResponse;
74
        } else {
75
            return $this->renderWith('PreviewPageRow');
76
        }
77
    }
78
79
80
    public function PageRowsReadyForPublication()
81
    {
82
        return $this->pageRows;
83
    }
84
}
85