nnx-framework /
data-grid
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * @company MTE Telecom, Ltd. |
||
| 4 | * @author Roman Malashin <[email protected]> |
||
| 5 | */ |
||
| 6 | |||
| 7 | namespace Nnx\DataGrid; |
||
| 8 | |||
| 9 | use Nnx\DataGrid\Column\ColumnInterface; |
||
| 10 | use Nnx\DataGrid\Mutator\MutatorInterface; |
||
| 11 | use Traversable; |
||
| 12 | use ArrayAccess; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Class SimpleGrid |
||
| 16 | * @package Nnx\DataGrid |
||
| 17 | */ |
||
| 18 | class SimpleGrid extends AbstractGrid implements ColumHidebleProviderInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Коллекция колонок таблицы скрытых/показаннах пользователем |
||
| 22 | * @var array | Traversable |
||
| 23 | */ |
||
| 24 | protected $userHiddenColums; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Флаг указывающий на изменеи списка колонок используется для проверки необходимости переустановки скрытия колонок пользователем |
||
|
0 ignored issues
–
show
|
|||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $columsChanged = false; |
||
| 31 | /** |
||
| 32 | * Данные в гриде |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected $rowSet = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Возвращает массив строк таблицы |
||
| 39 | * @return array |
||
| 40 | */ |
||
| 41 | public function getRowSet() |
||
| 42 | { |
||
| 43 | if (count($this->rowSet) === 0) { |
||
| 44 | $data = $this->getAdapter()->getData(); |
||
| 45 | $this->buildRowSet($data); |
||
| 46 | } |
||
| 47 | |||
| 48 | return $this->rowSet; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Добавление колонок в таблицу |
||
| 53 | * @param array | Traversable $columns |
||
| 54 | * @return $this |
||
| 55 | */ |
||
| 56 | public function setColumns($columns) |
||
| 57 | { |
||
| 58 | $this->setColumsChanged(true); |
||
| 59 | return parent::setColumns($columns); |
||
| 60 | } |
||
| 61 | /** |
||
| 62 | * Добавление колонки в таблицу |
||
| 63 | * @param ColumnInterface|array|ArrayAccess $column |
||
| 64 | * @return $this |
||
| 65 | * @throws Column\Exception\InvalidColumnException |
||
| 66 | * @throws Exception\InvalidArgumentException |
||
| 67 | * @throws Exception\RuntimeException |
||
| 68 | */ |
||
| 69 | public function add($column) |
||
| 70 | { |
||
| 71 | $this->setColumsChanged(true); |
||
| 72 | return parent::add($column); |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | /** |
||
| 77 | * Создает из данных адаптера rowSet |
||
| 78 | * @param array | ArrayAccess $data |
||
| 79 | * @return $this |
||
| 80 | * @throws Exception\RuntimeException |
||
| 81 | */ |
||
| 82 | protected function buildRowSet($data) |
||
| 83 | { |
||
| 84 | if (!is_array($data) && $data instanceof Traversable) { |
||
| 85 | throw new Exception\RuntimeException( |
||
| 86 | sprintf('Данные должны быть массивом или %s', ArrayAccess::class) |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | $columns = $this->getColumns(); |
||
| 90 | |||
| 91 | foreach ($data as $item) { |
||
| 92 | $mutators = $this->getMutators(); |
||
| 93 | $item = $this->mutate($mutators, $item); |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
It seems like
$mutators defined by $this->getMutators() on line 92 can also be of type object<ArrayAccess>; however, Nnx\DataGrid\SimpleGrid::mutate() does only seem to accept array, maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. Loading history...
|
|||
| 94 | /** @var ColumnInterface $column */ |
||
| 95 | foreach ($columns as $column) { |
||
| 96 | $columnName = $column->getName(); |
||
| 97 | $mutators = $column->getMutators(); |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 98 | if (array_key_exists($columnName, $item)) { |
||
| 99 | $item = $this->mutate($mutators, $item, $columnName); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | $this->rowSet[] = new Row($item); |
||
| 103 | } |
||
| 104 | return $this; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Метод непосредственно осуществляет мутацию данных |
||
| 109 | * @param array $mutators |
||
| 110 | * @param array | Row $row |
||
| 111 | * @param string $name |
||
|
0 ignored issues
–
show
Should the type for parameter
$name not be string|null?
This check looks for It makes a suggestion as to what type it considers more descriptive. Most often this is a case of a parameter that can be null in addition to its declared types. Loading history...
|
|||
| 112 | * @return mixed |
||
| 113 | */ |
||
| 114 | protected function mutate(array $mutators, $row, $name = null) |
||
| 115 | { |
||
| 116 | /** @var MutatorInterface $mutator */ |
||
| 117 | foreach ($mutators as $mutator) { |
||
| 118 | if ($mutator instanceof MutatorInterface) { |
||
| 119 | $mutator->setRowData($row); |
||
| 120 | if ($mutator->validate()) { |
||
| 121 | if ($name) { |
||
|
0 ignored issues
–
show
The expression
$name of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
| 122 | $row[$name] = $mutator->change($row[$name]); |
||
| 123 | } else { |
||
| 124 | $row = $mutator->change($row); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | return $row; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param array $rowSet |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function setRowSet($rowSet) |
||
| 137 | { |
||
| 138 | $this->rowSet = $rowSet; |
||
| 139 | return $this; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Функция инициализации колонок |
||
| 144 | * @return void |
||
| 145 | */ |
||
| 146 | public function init() |
||
| 147 | { |
||
| 148 | } |
||
| 149 | /** |
||
| 150 | * Возвращает коллекцию колонок |
||
| 151 | * @return array | Traversable |
||
| 152 | */ |
||
| 153 | public function getColumns() |
||
| 154 | { |
||
| 155 | $colums = parent::getColumns(); |
||
| 156 | if ($this->getUserHiddenColums() && $this->isColumsChanged()) { |
||
| 157 | $newColums = []; |
||
|
0 ignored issues
–
show
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line. To visualize $a = "a";
$ab = "ab";
$abc = "abc";
will produce issues in the first and second line, while this second example $a = "a";
$ab = "ab";
$abc = "abc";
will produce no issues. Loading history...
|
|||
| 158 | $addedColums = []; |
||
| 159 | foreach ($this->getUserHiddenColums() as $i => $userColum) { |
||
| 160 | /** @var ColumnInterface $colum */ |
||
| 161 | if (!empty($userColum['n']) && $colum = $this->get($userColum['n'])) { |
||
| 162 | $addedColums[$userColum['n']] = true; |
||
| 163 | $colum->setOrder($i); |
||
| 164 | if (!empty($userColum['h'])) { |
||
| 165 | $colum->setAttribute('hidden', (bool)$userColum['h']); |
||
| 166 | } |
||
| 167 | $newColums[] = $colum; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | $i++; |
||
|
0 ignored issues
–
show
The variable
$i seems to be defined by a foreach iteration on line 159. Are you sure the iterator is never empty, otherwise this variable is not defined?
It seems like you are relying on a variable being defined by an iteration: foreach ($a as $b) {
}
// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.
// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}
// $b is now guaranteed to be defined here.
Loading history...
|
|||
| 171 | /** @var ColumnInterface $colum */ |
||
| 172 | foreach ($colums as $colum) { |
||
| 173 | if (empty($addedColums[$colum->getName()])) { |
||
| 174 | $colum->setOrder($i); |
||
| 175 | $newColums[] = $colum; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | return $newColums; |
||
| 179 | } |
||
| 180 | return $colums; |
||
| 181 | } |
||
| 182 | |||
| 183 | |||
| 184 | /** |
||
| 185 | * получить коллекцию колонок таблицы скрытых/показаннах пользователем |
||
| 186 | * @return array|Traversable |
||
| 187 | */ |
||
| 188 | public function getUserHiddenColums() |
||
| 189 | { |
||
| 190 | return $this->userHiddenColums; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * установить коллекцию колонок таблицы скрытых/показаннах пользователем |
||
| 195 | * @param array|Traversable $userHiddenColums |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setUserHiddenColums($userHiddenColums) |
||
| 199 | { |
||
| 200 | $this->setColumsChanged(true); |
||
| 201 | $this->userHiddenColums = $userHiddenColums; |
||
|
0 ignored issues
–
show
It seems like
$userHiddenColums can also be of type object<Traversable>. However, the property $userHiddenColums 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 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...
|
|||
| 202 | return $this; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return boolean |
||
| 207 | */ |
||
| 208 | public function isColumsChanged() |
||
| 209 | { |
||
| 210 | return $this->columsChanged; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param boolean $columsChanged |
||
| 215 | * @return $this |
||
| 216 | */ |
||
| 217 | public function setColumsChanged($columsChanged) |
||
| 218 | { |
||
| 219 | $this->columsChanged = $columsChanged; |
||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | } |
||
|
0 ignored issues
–
show
|
|||
| 223 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.