Passed
Push — fix-8379 ( 67d6d1...73dd0e )
by Sam
07:16
created

Hierarchy::prepopulate_numchildren_cache()   C

Complexity

Conditions 12
Paths 25

Size

Total Lines 69
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 39
nc 25
nop 3
dl 0
loc 69
rs 6.9666
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SilverStripe\ORM\Hierarchy;
4
5
use SilverStripe\Admin\LeftAndMain;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Admin\LeftAndMain was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Control\Controller;
7
use SilverStripe\ORM\DataList;
8
use SilverStripe\ORM\SS_List;
9
use SilverStripe\ORM\ValidationResult;
10
use SilverStripe\ORM\ArrayList;
11
use SilverStripe\ORM\DataObject;
12
use SilverStripe\ORM\DataExtension;
13
use SilverStripe\ORM\DB;
14
use SilverStripe\Versioned\Versioned;
15
use SilverStripe\Core\Config\Config;
16
use SilverStripe\Core\Convert;
17
use Exception;
18
19
/**
20
 * DataObjects that use the Hierarchy extension can be be organised as a hierarchy, with children and parents. The most
21
 * obvious example of this is SiteTree.
22
 *
23
 * @property int $ParentID
24
 * @property DataObject|Hierarchy $owner
25
 * @method DataObject Parent()
26
 */
27
class Hierarchy extends DataExtension
28
{
29
    /**
30
     * The lower bounds for the amount of nodes to mark. If set, the logic will expand nodes until it reaches at least
31
     * this number, and then stops. Root nodes will always show regardless of this settting. Further nodes can be
32
     * lazy-loaded via ajax. This isn't a hard limit. Example: On a value of 10, with 20 root nodes, each having 30
33
     * children, the actual node count will be 50 (all root nodes plus first expanded child).
34
     *
35
     * @config
36
     * @var int
37
     */
38
    private static $node_threshold_total = 50;
0 ignored issues
show
introduced by
The private property $node_threshold_total is not used, and could be removed.
Loading history...
39
40
    /**
41
     * Limit on the maximum children a specific node can display. Serves as a hard limit to avoid exceeding available
42
     * server resources in generating the tree, and browser resources in rendering it. Nodes with children exceeding
43
     * this value typically won't display any children, although this is configurable through the $nodeCountCallback
44
     * parameter in {@link getChildrenAsUL()}. "Root" nodes will always show all children, regardless of this setting.
45
     *
46
     * @config
47
     * @var int
48
     */
49
    private static $node_threshold_leaf = 250;
0 ignored issues
show
introduced by
The private property $node_threshold_leaf is not used, and could be removed.
Loading history...
50
51
    /**
52
     * A list of classnames to exclude from display in both the CMS and front end
53
     * displays. ->Children() and ->AllChildren affected.
54
     * Especially useful for big sets of pages like listings
55
     * If you use this, and still need the classes to be editable
56
     * then add a model admin for the class
57
     * Note: Does not filter subclasses (non-inheriting)
58
     *
59
     * @var array
60
     * @config
61
     */
62
    private static $hide_from_hierarchy = array();
63
64
    /**
65
     * A list of classnames to exclude from display in the page tree views of the CMS,
66
     * unlike $hide_from_hierarchy above which effects both CMS and front end.
67
     * Especially useful for big sets of pages like listings
68
     * If you use this, and still need the classes to be editable
69
     * then add a model admin for the class
70
     * Note: Does not filter subclasses (non-inheriting)
71
     *
72
     * @var array
73
     * @config
74
     */
75
    private static $hide_from_cms_tree = array();
76
77
    /**
78
     * Used to enable or disable the prepopulation of the numchildren cache.
79
     * Defaults to true.
80
     *
81
     * @config
82
     * @var boolean
83
     */
84
    private static $prepopulate_numchildren_cache = true;
0 ignored issues
show
introduced by
The private property $prepopulate_numchildren_cache is not used, and could be removed.
Loading history...
85
86
    /**
87
     * Prevent virtual page virtualising these fields
88
     *
89
     * @config
90
     * @var array
91
     */
92
    private static $non_virtual_fields = [
0 ignored issues
show
introduced by
The private property $non_virtual_fields is not used, and could be removed.
Loading history...
93
        '_cache_children',
94
    ];
95
96
    /**
97
     * A cache used by numChildren().
98
     * Clear through {@link flushCache()}.
99
     * version (int)0 means not on this stage.
100
     *
101
     * @var array
102
     */
103
    protected static $cache_numChildren = [];
104
105
    public static function get_extra_config($class, $extension, $args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
    public static function get_extra_config($class, $extension, /** @scrutinizer ignore-unused */ $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $extension is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

105
    public static function get_extra_config($class, /** @scrutinizer ignore-unused */ $extension, $args)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
106
    {
107
        return array(
108
            'has_one' => array('Parent' => $class)
109
        );
110
    }
111
112
    /**
113
     * Validate the owner object - check for existence of infinite loops.
114
     *
115
     * @param ValidationResult $validationResult
116
     */
117
    public function validate(ValidationResult $validationResult)
118
    {
119
        // The object is new, won't be looping.
120
        /** @var DataObject|Hierarchy $owner */
121
        $owner = $this->owner;
122
        if (!$owner->ID) {
123
            return;
124
        }
125
        // The object has no parent, won't be looping.
126
        if (!$owner->ParentID) {
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
127
            return;
128
        }
129
        // The parent has not changed, skip the check for performance reasons.
130
        if (!$owner->isChanged('ParentID')) {
0 ignored issues
show
Bug introduced by
The method isChanged() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

130
        if (!$owner->/** @scrutinizer ignore-call */ isChanged('ParentID')) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
            return;
132
        }
133
134
        // Walk the hierarchy upwards until we reach the top, or until we reach the originating node again.
135
        $node = $owner;
136
        while ($node && $node->ParentID) {
137
            if ((int)$node->ParentID === (int)$owner->ID) {
138
                // Hierarchy is looping.
139
                $validationResult->addError(
140
                    _t(
141
                        __CLASS__ . '.InfiniteLoopNotAllowed',
142
                        'Infinite loop found within the "{type}" hierarchy. Please change the parent to resolve this',
143
                        'First argument is the class that makes up the hierarchy.',
144
                        array('type' => get_class($owner))
145
                    ),
146
                    'bad',
147
                    'INFINITE_LOOP'
148
                );
149
                break;
150
            }
151
            $node = $node->Parent();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
            /** @scrutinizer ignore-call */ 
152
            $node = $node->Parent();
Loading history...
152
        }
153
    }
154
155
156
    /**
157
     * Get a list of this DataObject's and all it's descendants IDs.
158
     *
159
     * @return int[]
160
     */
161
    public function getDescendantIDList()
162
    {
163
        $idList = array();
164
        $this->loadDescendantIDListInto($idList);
165
        return $idList;
166
    }
167
168
    /**
169
     * Get a list of this DataObject's and all it's descendants ID, and put them in $idList.
170
     *
171
     * @param array $idList Array to put results in.
172
     * @param DataObject|Hierarchy $node
173
     */
174
    protected function loadDescendantIDListInto(&$idList, $node = null)
175
    {
176
        if (!$node) {
177
            $node = $this->owner;
178
        }
179
        $children = $node->AllChildren();
0 ignored issues
show
Bug introduced by
The method AllChildren() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        /** @scrutinizer ignore-call */ 
180
        $children = $node->AllChildren();
Loading history...
180
        foreach ($children as $child) {
181
            if (!in_array($child->ID, $idList)) {
182
                $idList[] = $child->ID;
183
                $this->loadDescendantIDListInto($idList, $child);
184
            }
185
        }
186
    }
187
188
    /**
189
     * Get the children for this DataObject filtered by canView()
190
     *
191
     * @return SS_List
192
     */
193
    public function Children()
194
    {
195
        $children = $this->owner->_cache_children;
0 ignored issues
show
Bug Best Practice introduced by
The property _cache_children does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
196
        if ($children) {
197
            return $children;
198
        }
199
200
        $children = $this
201
            ->owner
202
            ->stageChildren(false)
0 ignored issues
show
Bug introduced by
The method stageChildren() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

202
            ->/** @scrutinizer ignore-call */ stageChildren(false)
Loading history...
203
            ->filterByCallback(function (DataObject $record) {
204
                return $record->canView();
205
            });
206
        $this->owner->_cache_children = $children;
0 ignored issues
show
Bug Best Practice introduced by
The property _cache_children does not exist on SilverStripe\ORM\DataObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property _cache_children does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
207
        return $children;
208
    }
209
210
    /**
211
     * Return all children, including those 'not in menus'.
212
     *
213
     * @return DataList
214
     */
215
    public function AllChildren()
216
    {
217
        return $this->owner->stageChildren(true);
218
    }
219
220
    /**
221
     * Return all children, including those that have been deleted but are still in live.
222
     * - Deleted children will be marked as "DeletedFromStage"
223
     * - Added children will be marked as "AddedToStage"
224
     * - Modified children will be marked as "ModifiedOnStage"
225
     * - Everything else has "SameOnStage" set, as an indicator that this information has been looked up.
226
     *
227
     * @return ArrayList
228
     */
229
    public function AllChildrenIncludingDeleted()
230
    {
231
        /** @var DataObject|Hierarchy|Versioned $owner */
232
        $owner = $this->owner;
233
        $stageChildren = $owner->stageChildren(true);
234
235
        // Add live site content that doesn't exist on the stage site, if required.
236
        if ($owner->hasExtension(Versioned::class) && $owner->hasStages()) {
0 ignored issues
show
Bug introduced by
The method hasExtension() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
        if ($owner->/** @scrutinizer ignore-call */ hasExtension(Versioned::class) && $owner->hasStages()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method hasStages() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
        if ($owner->hasExtension(Versioned::class) && $owner->/** @scrutinizer ignore-call */ hasStages()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method hasStages() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

236
        if ($owner->hasExtension(Versioned::class) && $owner->/** @scrutinizer ignore-call */ hasStages()) {
Loading history...
237
            // Next, go through the live children.  Only some of these will be listed
238
            $liveChildren = $owner->liveChildren(true, true);
0 ignored issues
show
Bug introduced by
The method liveChildren() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

238
            /** @scrutinizer ignore-call */ 
239
            $liveChildren = $owner->liveChildren(true, true);
Loading history...
239
            if ($liveChildren) {
240
                $merged = new ArrayList();
241
                $merged->merge($stageChildren);
242
                $merged->merge($liveChildren);
243
                $stageChildren = $merged;
244
            }
245
        }
246
        $owner->extend("augmentAllChildrenIncludingDeleted", $stageChildren);
0 ignored issues
show
Bug introduced by
The method extend() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

246
        $owner->/** @scrutinizer ignore-call */ 
247
                extend("augmentAllChildrenIncludingDeleted", $stageChildren);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
247
        return $stageChildren;
248
    }
249
250
    /**
251
     * Return all the children that this page had, including pages that were deleted from both stage & live.
252
     *
253
     * @return DataList
254
     * @throws Exception
255
     */
256
    public function AllHistoricalChildren()
257
    {
258
        /** @var DataObject|Versioned|Hierarchy $owner */
259
        $owner = $this->owner;
260
        if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) {
261
            throw new Exception(
262
                'Hierarchy->AllHistoricalChildren() only works with Versioned extension applied with staging'
263
            );
264
        }
265
266
        $baseTable = $owner->baseTable();
0 ignored issues
show
Bug introduced by
The method baseTable() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

266
        /** @scrutinizer ignore-call */ 
267
        $baseTable = $owner->baseTable();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
267
        $parentIDColumn = $owner->getSchema()->sqlColumnForField($owner, 'ParentID');
0 ignored issues
show
Bug introduced by
It seems like $owner can also be of type SilverStripe\ORM\Hierarchy\Hierarchy; however, parameter $class of SilverStripe\ORM\DataObj...ma::sqlColumnForField() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

267
        $parentIDColumn = $owner->getSchema()->sqlColumnForField(/** @scrutinizer ignore-type */ $owner, 'ParentID');
Loading history...
Bug introduced by
The method getSchema() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

267
        $parentIDColumn = $owner->/** @scrutinizer ignore-call */ getSchema()->sqlColumnForField($owner, 'ParentID');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
268
        return Versioned::get_including_deleted(
269
            $owner->baseClass(),
0 ignored issues
show
Bug introduced by
The method baseClass() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

269
            $owner->/** @scrutinizer ignore-call */ 
270
                    baseClass(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
270
            [ $parentIDColumn => $owner->ID ],
0 ignored issues
show
Bug introduced by
array($parentIDColumn => $owner->ID) of type array<string,mixed|integer> is incompatible with the type string expected by parameter $filter of SilverStripe\Versioned\V...get_including_deleted(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

270
            /** @scrutinizer ignore-type */ [ $parentIDColumn => $owner->ID ],
Loading history...
271
            "\"{$baseTable}\".\"ID\" ASC"
272
        );
273
    }
274
275
    /**
276
     * Return the number of children that this page ever had, including pages that were deleted.
277
     *
278
     * @return int
279
     */
280
    public function numHistoricalChildren()
281
    {
282
        return $this->AllHistoricalChildren()->count();
283
    }
284
285
    /**
286
     * Return the number of direct children. By default, values are cached after the first invocation. Can be
287
     * augumented by {@link augmentNumChildrenCountQuery()}.
288
     *
289
     * @param bool $cache Whether to retrieve values from cache
290
     * @return int
291
     */
292
    public function numChildren($cache = true)
293
    {
294
295
        $baseClass = $this->owner->baseClass();
296
        $stage = 'Stage';
297
        $id = $this->owner->ID;
298
299
        // cached call
300
        if ($cache && isset(self::$cache_numChildren[$baseClass][$stage][$id])) {
301
            return self::$cache_numChildren[$baseClass][$stage][$id];
302
        }
303
304
        // We call stageChildren(), because Children() has canView() filtering
305
        $numChildren = (int)$this->owner->stageChildren(true)->Count();
306
307
        // Save if caching
308
        if ($cache) {
309
            self::$cache_numChildren[$baseClass][$stage][$id] = $numChildren;
310
        }
311
312
        return $numChildren;
313
    }
314
315
    /**
316
     * Pre-populate the cache for Versioned::get_versionnumber_by_stage() for
317
     * a list of record IDs, for more efficient database querying.  If $idList
318
     * is null, then every record will be pre-cached.
319
     *
320
     * @param string $class
321
     * @param string $stage
322
     * @param array $idList
323
     */
324
    public static function prepopulate_numchildren_cache($baseClass, $stage, $idList = null)
325
    {
326
        if (!Config::inst()->get(static::class, 'prepopulate_numchildren_cache')) {
327
            return;
328
        }
329
330
        /** @var Versioned|DataObject $singleton */
331
        $dummyObject = new $baseClass();
332
        $dummyObject->ID = -23478234; // going to use this for search & replace
333
        $baseTable = $dummyObject->baseTable();
334
335
        $idColumn = Convert::symbol2sql("{$baseTable}.ID");
336
        $parentIdColumn = Convert::symbol2sql("ParentID");
337
338
339
        // Get the stageChildren() result of a dummy object and break down into a generic query
340
        $query = $dummyObject->stageChildren(true)->dataQuery()->query();
341
        $where = $query->getWhere();
342
343
        $newWhere = [];
344
345
        foreach ($where as $i => $compoundClause) {
346
            foreach ($compoundClause as $clause => $params) {
347
                // Skip any "WHERE ParentID = [marker]" clauses as this will be replaced with a GROUP BY
348
                if (!(preg_match('/^"[^"]+"\."ParentID" = \?$/', $clause) && $clause[1] == $dummyObject->ID)) {
349
                    // Replace any marker IDs with "<basetable>"."ID"
350
                    $paramNum = 0;
351
                    foreach ($params as $j => $param) {
352
                        if ($param == $dummyObject->ID) {
353
                            unset($params[$j]);
354
                            $clause = DB::replace_parameter($clause, $paramNum, $parentIdColumn, true);
0 ignored issues
show
Bug introduced by
$parentIdColumn of type string is incompatible with the type array expected by parameter $replacement of SilverStripe\ORM\DB::replace_parameter(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

354
                            $clause = DB::replace_parameter($clause, $paramNum, /** @scrutinizer ignore-type */ $parentIdColumn, true);
Loading history...
355
                        } else {
356
                            $paramNum++;
357
                        }
358
                    }
359
360
                    $newWhere[] = [ $clause => $params ];
361
                }
362
            }
363
        }
364
365
        // optional ID-list filter
366
        if ($idList) {
367
            // Validate the ID list
368
            foreach ($idList as $id) {
369
                if (!is_numeric($id)) {
370
                    user_error(
371
                        "Bad ID passed to Versioned::prepopulate_numchildren_cache() in \$idList: " . $id,
372
                        E_USER_ERROR
373
                    );
374
                }
375
            }
376
            $newWhere[] = ['"ParentID" IN (' . DB::placeholders($idList) . ')' => $idList];
377
        }
378
379
        $query->setWhere($newWhere);
380
        $query->setOrderBy(null);
381
382
        $query->setSelect([
383
            '"ParentID"',
384
            "COUNT(DISTINCT $idColumn) AS \"NumChildren\"",
385
        ]);
386
        $query->setGroupBy([
387
            "ParentID
388
        "]);
389
390
        $numChildrens = $query->execute()->map();
391
        foreach ($numChildrens as $id => $numChildren) {
392
            self::$cache_numChildren[$baseClass][$stage][$id] = $numChildren;
393
        }
394
    }
395
396
    /**
397
     * Checks if we're on a controller where we should filter. ie. Are we loading the SiteTree?
398
     *
399
     * @return bool
400
     */
401
    public function showingCMSTree()
402
    {
403
        if (!Controller::has_curr() || !class_exists(LeftAndMain::class)) {
404
            return false;
405
        }
406
        $controller = Controller::curr();
407
        return $controller instanceof LeftAndMain
408
            && in_array($controller->getAction(), array("treeview", "listview", "getsubtree"));
409
    }
410
411
    /**
412
     * Return children in the stage site.
413
     *
414
     * @param bool $showAll Include all of the elements, even those not shown in the menus. Only applicable when
415
     *                      extension is applied to {@link SiteTree}.
416
     * @return DataList
417
     */
418
    public function stageChildren($showAll = false)
419
    {
420
        $hideFromHierarchy = $this->owner->config()->hide_from_hierarchy;
0 ignored issues
show
Bug Best Practice introduced by
The property hide_from_hierarchy does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
The method config() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. Did you maybe mean get_extra_config()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

420
        $hideFromHierarchy = $this->owner->/** @scrutinizer ignore-call */ config()->hide_from_hierarchy;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
421
        $hideFromCMSTree = $this->owner->config()->hide_from_cms_tree;
0 ignored issues
show
Bug Best Practice introduced by
The property hide_from_cms_tree does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
422
        $baseClass = $this->owner->baseClass();
423
        $staged = DataObject::get($baseClass)
424
                ->filter('ParentID', (int)$this->owner->ID)
425
                ->exclude('ID', (int)$this->owner->ID);
426
        if ($hideFromHierarchy) {
427
            $staged = $staged->exclude('ClassName', $hideFromHierarchy);
428
        }
429
        if ($hideFromCMSTree && $this->showingCMSTree()) {
430
            $staged = $staged->exclude('ClassName', $hideFromCMSTree);
431
        }
432
        if (!$showAll && DataObject::getSchema()->fieldSpec($this->owner, 'ShowInMenus')) {
0 ignored issues
show
Bug introduced by
It seems like $this->owner can also be of type SilverStripe\ORM\Hierarchy\Hierarchy; however, parameter $classOrInstance of SilverStripe\ORM\DataObjectSchema::fieldSpec() does only seem to accept SilverStripe\ORM\DataObject|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

432
        if (!$showAll && DataObject::getSchema()->fieldSpec(/** @scrutinizer ignore-type */ $this->owner, 'ShowInMenus')) {
Loading history...
433
            $staged = $staged->filter('ShowInMenus', 1);
434
        }
435
        $this->owner->extend("augmentStageChildren", $staged, $showAll);
436
        return $staged;
437
    }
438
439
    /**
440
     * Return children in the live site, if it exists.
441
     *
442
     * @param bool $showAll              Include all of the elements, even those not shown in the menus. Only
443
     *                                   applicable when extension is applied to {@link SiteTree}.
444
     * @param bool $onlyDeletedFromStage Only return items that have been deleted from stage
445
     * @return DataList
446
     * @throws Exception
447
     */
448
    public function liveChildren($showAll = false, $onlyDeletedFromStage = false)
449
    {
450
        /** @var Versioned|DataObject|Hierarchy $owner */
451
        $owner = $this->owner;
452
        if (!$owner->hasExtension(Versioned::class) || !$owner->hasStages()) {
453
            throw new Exception('Hierarchy->liveChildren() only works with Versioned extension applied with staging');
454
        }
455
456
        $hideFromHierarchy = $owner->config()->hide_from_hierarchy;
0 ignored issues
show
Bug Best Practice introduced by
The property hide_from_hierarchy does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
457
        $hideFromCMSTree = $owner->config()->hide_from_cms_tree;
0 ignored issues
show
Bug Best Practice introduced by
The property hide_from_cms_tree does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
458
        $children = DataObject::get($owner->baseClass())
459
            ->filter('ParentID', (int)$owner->ID)
460
            ->exclude('ID', (int)$owner->ID)
461
            ->setDataQueryParam(array(
462
                'Versioned.mode' => $onlyDeletedFromStage ? 'stage_unique' : 'stage',
463
                'Versioned.stage' => 'Live'
464
            ));
465
        if ($hideFromHierarchy) {
466
            $children = $children->exclude('ClassName', $hideFromHierarchy);
467
        }
468
        if ($hideFromCMSTree && $this->showingCMSTree()) {
469
            $children = $children->exclude('ClassName', $hideFromCMSTree);
470
        }
471
        if (!$showAll && DataObject::getSchema()->fieldSpec($owner, 'ShowInMenus')) {
0 ignored issues
show
Bug introduced by
It seems like $owner can also be of type SilverStripe\ORM\Hierarchy\Hierarchy; however, parameter $classOrInstance of SilverStripe\ORM\DataObjectSchema::fieldSpec() does only seem to accept SilverStripe\ORM\DataObject|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

471
        if (!$showAll && DataObject::getSchema()->fieldSpec(/** @scrutinizer ignore-type */ $owner, 'ShowInMenus')) {
Loading history...
472
            $children = $children->filter('ShowInMenus', 1);
473
        }
474
475
        return $children;
476
    }
477
478
    /**
479
     * Get this object's parent, optionally filtered by an SQL clause. If the clause doesn't match the parent, nothing
480
     * is returned.
481
     *
482
     * @param string $filter
483
     * @return DataObject
484
     */
485
    public function getParent($filter = null)
486
    {
487
        $parentID = $this->owner->ParentID;
0 ignored issues
show
Bug Best Practice introduced by
The property ParentID does not exist on SilverStripe\ORM\DataObject. Since you implemented __get, consider adding a @property annotation.
Loading history...
488
        if (empty($parentID)) {
489
            return null;
490
        }
491
        $baseClass = $this->owner->baseClass();
492
        $idSQL = $this->owner->getSchema()->sqlColumnForField($baseClass, 'ID');
493
        return DataObject::get_one($baseClass, [
494
            [$idSQL => $parentID],
495
            $filter
496
        ]);
497
    }
498
499
    /**
500
     * Return all the parents of this class in a set ordered from the closest to furtherest parent.
501
     *
502
     * @param bool $includeSelf
503
     * @return ArrayList
504
     */
505
    public function getAncestors($includeSelf = false)
506
    {
507
        $ancestors = new ArrayList();
508
        $object = $this->owner;
509
510
        if ($includeSelf) {
511
            $ancestors->push($object);
512
        }
513
        while ($object = $object->getParent()) {
0 ignored issues
show
Bug introduced by
The method getParent() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

513
        while ($object = $object->/** @scrutinizer ignore-call */ getParent()) {
Loading history...
514
            $ancestors->push($object);
515
        }
516
517
        return $ancestors;
518
    }
519
520
    /**
521
     * Returns a human-readable, flattened representation of the path to the object, using its {@link Title} attribute.
522
     *
523
     * @param string $separator
524
     * @return string
525
     */
526
    public function getBreadcrumbs($separator = ' &raquo; ')
527
    {
528
        $crumbs = array();
529
        $ancestors = array_reverse($this->owner->getAncestors()->toArray());
0 ignored issues
show
Bug introduced by
The method getAncestors() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

529
        $ancestors = array_reverse($this->owner->/** @scrutinizer ignore-call */ getAncestors()->toArray());
Loading history...
530
        /** @var DataObject $ancestor */
531
        foreach ($ancestors as $ancestor) {
532
            $crumbs[] = $ancestor->getTitle();
533
        }
534
        $crumbs[] = $this->owner->getTitle();
0 ignored issues
show
Bug introduced by
The method getTitle() does not exist on SilverStripe\ORM\Hierarchy\Hierarchy. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

534
        /** @scrutinizer ignore-call */ 
535
        $crumbs[] = $this->owner->getTitle();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
535
        return implode($separator, $crumbs);
536
    }
537
538
    /**
539
     * Flush all Hierarchy caches:
540
     * - Children (instance)
541
     * - NumChildren (instance)
542
     */
543
    public function flushCache()
544
    {
545
        $this->owner->_cache_children = null;
0 ignored issues
show
Bug Best Practice introduced by
The property _cache_children does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug Best Practice introduced by
The property _cache_children does not exist on SilverStripe\ORM\DataObject. Since you implemented __set, consider adding a @property annotation.
Loading history...
546
        self::$cache_numChildren = [];
547
    }
548
}
549