Skip to content

Fixed "Attempt to read property on array" error in ArrayDataSource #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/DataSource/ArrayDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Contributte\Datagrid\Utils\DateTimeHelper;
use Contributte\Datagrid\Utils\Sorting;
use DateTime;
use DateTimeInterface;

Check failure on line 18 in src/DataSource/ArrayDataSource.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Type DateTimeInterface is not used in this file.
use Nette\Utils\Strings;

class ArrayDataSource implements IDataSource
Expand Down Expand Up @@ -116,12 +116,11 @@
$data = [];

foreach ($this->data as $item) {
if (is_object($item->$column) && $item->$column instanceof \DateTimeInterface) {
$sort_by = $item->$column->format('Y-m-d H:i:s');
} elseif (is_object($item[$column]) && $item[$column] instanceof \DateTimeInterface) {
$sort_by = $item[$column]->format('Y-m-d H:i:s');
$value = is_object($item) ? $item->$column : $item[$column];

Check failure on line 119 in src/DataSource/ArrayDataSource.php

View workflow job for this annotation

GitHub Actions / Phpstan / Phpstan (8.3)

Variable property access on object.
Copy link
Preview

Copilot AI May 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a check to ensure that the expected property or key exists on $item before accessing it, to prevent potential undefined index or property notices.

Suggested change
$value = is_object($item) ? $item->$column : $item[$column];
if (is_object($item) && property_exists($item, $column)) {
$value = $item->$column;
} elseif (is_array($item) && array_key_exists($column, $item)) {
$value = $item[$column];
} else {
// Skip this item if the column does not exist
continue;
}

Copilot uses AI. Check for mistakes.

if ($value instanceof \DateTimeInterface) {

Check failure on line 120 in src/DataSource/ArrayDataSource.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.3)

Use ternary operator.
$sort_by = $value->format('Y-m-d H:i:s');
} else {
$sort_by = (string) $item[$column];
$sort_by = (string) $value;
}

$data[$sort_by][] = $item;
Expand Down
Loading