PageRowPageExtension   B
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 9
dl 0
loc 246
c 0
b 0
f 0
rs 8.8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A PageRows() 0 4 1
A getPageRows() 0 4 1
F onAfterWrite() 0 82 17
A removePageRowFromThisPage() 0 14 2
A DefaultPageRows() 0 7 2
A updateCMSFields() 0 10 4
B ContentBlocksFields() 0 40 4
B HasPageRows() 0 13 5
1
<?php
2
3
class PageRowPageExtension extends SiteTreeExtension
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...
4
{
5
6
7
    private static $many_many = [
0 ignored issues
show
Unused Code introduced by
The property $many_many 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...
8
        'PageRows' => 'PageRow'
9
    ];
10
11
    private static $many_many_extraFields = [
0 ignored issues
show
Unused Code introduced by
The property $many_many_extraFields 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...
12
        'PageRows' => [
13
            'SortOrder' => 'Int'
14
        ]
15
    ];
16
17
    public function PageRows()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
18
    {
19
        return $this->getPageRows();
20
    }
21
22
    public function getPageRows()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
23
    {
24
        return $this->owner->getManyManyComponents('PageRows')->sort(['SortOrder' => 'ASC']);
25
    }
26
27
    #######################
28
    ### Further DB Field Details
29
    #######################
30
31
    #######################
32
    ### Field Names and Presentation Section
33
    #######################
34
35
    private static $field_labels = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels 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...
36
        'PageRows' => 'Content Blocks'
37
    ];
38
39
    private static $field_labels_right = [
0 ignored issues
show
Unused Code introduced by
The property $field_labels_right 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...
40
        'PageRows' => 'Please edit with care! You can add content blocks that are not ready for publication, but they will not be visible until they are ticked as available.',
41
    ];
42
43
44
    #######################
45
    ### Casting Section
46
    #######################
47
48
49
    #######################
50
    ### can Section
51
    #######################
52
53
54
55
    #######################
56
    ### write Section
57
    #######################
58
59
60
61
    public function onAfterWrite()
62
    {
63
        if (Security::database_is_ready()  && $this->owner->HasPageRows()) {
64
            // debug::Log('-------------------------');
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
65
            $currentPageRows = [];
66
            if ($this->owner->PageRows()->count() > 0) {
67
                $sortOrder = 1;
68
                foreach ($this->owner->PageRows() as $pageRow) {
69
                    $currentPageRows[$sortOrder] = $pageRow;
70
                    $sortOrder++;
71
                }
72
            }
73
            $rowClassNames = $this->owner->DefaultPageRows();
74
            $sortOrder = 1;
75
            foreach ($rowClassNames as $className) {
76
                $childClassName = null;
77
                if (is_array($className)) {
78
                    $childClassName = $className['Child'];
79
                    // debug::log($childClassName);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
80
                    $className = $className['Parent'];
81
                }
82
                if (isset($currentPageRows[$sortOrder])) {
83
                    $row = $currentPageRows[$sortOrder];
84
                    if ($row->ClassName === $className) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
85
                        //all OK!
86
                    } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
87
                        // $this->owner->removePageRowFromThisPage($row);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
                        //we do not delete the row as it may be used somewhere else ...
89
                        // $this->owner->deletePageRowFromMe($row);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
90
                    }
91
                } else {
92
                    $row = $className::create();
93
                    $row->Title = 'Title '.$row->singular_name().' #'.$className::get()->count().' for '.$this->owner->MenuTitle;
94
                    $row->write();
95
                    $this->owner->PageRows()->add($row, ['SortOrder' => $sortOrder]);
96
                    $currentPageRows[$sortOrder] = $row;
97
                }
98
                DB::query('
99
                    UPDATE "Page_PageRows"
100
                    SET "SortOrder" = '.$sortOrder.'
101
                    WHERE
102
                        "PageID" ='.$this->owner->ID.' AND
103
                        "PageRowID" = '.$row->ID.'
104
                    LIMIT 1;
105
                ');
106
                if ($childClassName) {
107
                    $childClassMethod = $row->ChildClassMethodName();
108
                    if ($childClassMethod) {
109
                        $child = $row->$childClassMethod();
110
                        if ($child && $child->exists()) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
111
                        } else {
112
                            $childClassMethodFieldName = $childClassMethod.'ID';
113
                            $child = $childClassName::create();
114
                            $child->Title = 'New '.$child->singular_name().' for '.$row->getTitle();
115
                            $child->write();
116
                            // debug::log($childClassMethodFieldName);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
                            // debug::log($child->ID);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
118
                            // debug::log($child->Title);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
119
                            $row->$childClassMethodFieldName = $child->ID;
120
                            $row->write();
121
                        }
122
                    } else {
123
                        user_error('no childclass method set in '.$row->ClassName);
124
                    }
125
                }
126
                $sortOrder++;
127
            }
128
            $sortOrder = 1;
129
            foreach ($this->owner->PageRows() as $pageRow) {
130
                $sortOrder++;
131
                $delete = false;
132
                if (!isset($rowClassNames[$sortOrder])) {
133
                    $delete = true;
134
                } elseif ($rowClassNames[$sortOrder] !== $pageRow->ClassName) {
135
                    $delete = true;
136
                }
137
                if ($delete) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
138
                    // $this->owner->removePageRowFromThisPage($pageRow);
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
                }
140
            }
141
        }
142
    }
143
144
    protected function removePageRowFromThisPage($rowOrRowID)
145
    {
146
        if ($rowOrRowID instanceof PageRow) {
147
            $rowOrRowID = $rowOrRowID->ID;
148
        }
149
        DB::query('
150
            DELETE
151
            FROM "Page_PageRows"
152
            WHERE
153
                "PageID" ='.$this->owner->ID.' AND
154
                "PageRowID" = '.$rowOrRowID.'
155
            LIMIT 1;
156
        ');
157
    }
158
159
160
    public function DefaultPageRows()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
161
    {
162
        if($this->owner->hasMethod('MyDefaultPageRows')) {
163
            return $this->owner->MyDefaultPageRows();
164
        }
165
        return [];
166
    }
167
168
    #######################
169
    ### Import / Export Section
170
    #######################
171
172
173
174
    #######################
175
    ### CMS Edit Section
176
    #######################
177
178
    /**
179
     * Update Fields
180
     * @return FieldList
0 ignored issues
show
Documentation introduced by
Should the return type not be FieldList|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
181
     */
182
    public function updateCMSFields(FieldList $fields)
183
    {
184
        $list = $this->owner->PageRows();
0 ignored issues
show
Unused Code introduced by
$list 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...
185
        if($this->owner->canEdit() && $this->owner->exists() && $this->HasPageRows()) {
186
            $fields->addFieldsToTab(
187
                'Root.ContentBlocks',
188
                $this->owner->ContentBlocksFields()
189
            );
190
        }
191
    }
192
193
    public function ContentBlocksFields()
194
    {
195
        $conf = GridFieldConfig_RelationEditor::create(100);
196
        $conf->addComponent(new GridFieldSortableRows('SortOrder'));
197
        // switch ($this->owner->ClassName) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
198
        //     case 'HomePage':
199
        //         // leave as is ...
200
        //         $conf->removeComponentsByType('GridFieldAddExistingAutocompleter');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
201
        //         $conf->removeComponentsByType('GridFieldDeleteAction');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
202
        //         $conf->removeComponentsByType('GridFieldAddNewButton');
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
203
        //         break;
204
        //     default:
205
        // }
206
        //
207
        $conf->getComponentByType('GridFieldAddExistingAutocompleter')->setSearchFields(['Code', 'Title']);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface GridFieldComponent as the method setSearchFields() does only exist in the following implementations of said interface: GridFieldAddExistingAutocompleter.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
208
209
        $pageRowField = GridField::create(
210
            'PageRows',
211
            'Content Blocks',
212
            $this->owner->PageRows(),
213
            $conf
214
        );
215
        $array = [$pageRowField];
216
        $arrayRowList = [];
217
        foreach ($this->owner->DefaultPageRows() as $count => $className) {
218
            if (is_array($className)) {
219
                $className = $className['Parent'];
220
            }
221
            $humanCount = $count + 1;
222
            $arrayRowList[$className.'_'.$count] = $humanCount . ' - '.Injector::inst()->get($className)->singular_name().' ('.$className.')';
223
        }
224
        if(count($arrayRowList)) {
225
            $array[] = LiteralField::create(
226
                'ListOfContentBlocks',
227
                '<h2>By default, this page type ('.$this->owner->singular_name().') has the following content blocks:</h2><p>'.implode('</p><p>', $arrayRowList).'</p>'
228
            );
229
        }
230
231
        return $array;
232
    }
233
234
    public function HasPageRows()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
235
    {
236
        switch($this->owner->ClassName) {
237
            case 'ErrorPage':
238
            case 'RedirectorPage':
239
            case 'VirtualPage':
240
                return false;
241
        }
242
        if($this->owner->hasMethod('MyHasPageRows')) {
243
            return $this->owner->MyHasPageRows();
244
        }
245
        return true;
246
    }
247
248
}
249