Tree::setPrefix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace XoopsModules\Newbb;
4
5
/*
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * @copyright    XOOPS Project (https://xoops.org)/
17
 * @license      GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
18
 * @author       phppp (D.J., [email protected])
19
 * @author       XOOPS Development Team
20
 */
21
22
use XoopsTree;
23
24
require_once $GLOBALS['xoops']->path('class/xoopstree.php');
25
26
/**
27
 * Class Tree
28
 */
29
class Tree extends XoopsTree
30
{
31
    /** @var string */
32
    private string $prefix = '&nbsp;&nbsp;';
33
    /** @var string */
34
    private string $increment = '&nbsp;&nbsp;';
35
    /** @var array */
36
    private array $postArray = [];
37
38
    /**
39
     * @param string $table_name
40
     * @param string $id_name
41
     * @param string $pid_name
42
     */
43
    public function __construct($table_name, $id_name = 'post_id', $pid_name = 'pid')
44
    {
45
        parent::__construct($table_name, $id_name, $pid_name);
46
    }
47
48
    /**
49
     * @param string $val
50
     */
51
    public function setPrefix(string $val = ''): void
52
    {
53
        $this->prefix    = $val;
54
        $this->increment = $val;
55
    }
56
57
    /**
58
     * @param int    $sel_id
59
     * @param string $order
60
     */
61
    public function getAllPostArray(int $sel_id, string $order = ''): void
62
    {
63
        $this->postArray = $this->getAllChild($sel_id, $order);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getAllChild($sel_id, $order) can also be of type mixed. However, the property $postArray is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
64
    }
65
66
    /**
67
     * @param array $postArray
68
     */
69
    public function setPostArray(array $postArray): void
70
    {
71
        $this->postArray = $postArray;
72
    }
73
74
    // returns an array of first child objects for a given id($sel_id)
75
76
    /**
77
     * @param mixed  $postTree_array
78
     * @param int    $pid
79
     * @param string $prefix
80
     * @return bool
81
     */
82
    public function getPostTree(&$postTree_array, int $pid = 0, string $prefix = '&nbsp;&nbsp;'): bool
83
    {
84
        if (!\is_array($postTree_array)) {
85
            $postTree_array = [];
86
        }
87
88
        $newPostArray = [];
89
        $prefix       .= $this->increment;
90
        foreach ($this->postArray as $post) {
91
            if ($post->getVar('pid') == $pid) {
92
                $postTree_array[] = [
93
                    'prefix'      => $prefix,
94
                    'icon'        => $post->getVar('icon'),
95
                    'post_time'   => $post->getVar('post_time'),
96
                    'post_id'     => $post->getVar('post_id'),
97
                    'forum_id'    => $post->getVar('forum_id'),
98
                    'subject'     => $post->getVar('subject'),
99
                    'poster_name' => $post->getVar('poster_name'),
100
                    'uid'         => $post->getVar('uid'),
101
                ];
102
                $this->getPostTree($postTree_array, $post->getVar('post_id'), $prefix);
103
            } else {
104
                $newPostArray[] = $post;
105
            }
106
        }
107
        $this->postArray = $newPostArray;
108
        unset($newPostArray);
109
110
        return true;
111
    }
112
}
113