Problem/Motivation
In \Drupal\jsonapi\Query\Sort, the property $fields is defined as a string, when it is an array. This causes IDEs such as VSCode to flag iteration of the array as an error when it is not.
Steps to reproduce
In an IDE, write something like
$fields = [
['path' => 'changed', 'direction' => 'DESC'],
['path' => 'title', 'direction' => 'ASC'],
];
$sort = new \Drupal\jsonapi\Query\Sort($fields);
foreach ($sort->fields() as $field) {
echo $field['path'];
}
The IDE will complain "Expected type 'iterable|object'. Found 'string'."
Proposed resolution
Define the $fields property as @var array instead of string.
Issue fork drupal-3453068
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #4
kalpanajaiswal commentedCreated a MR and now $fields type is an array.
MR: https://git.drupalcode.org/project/drupal/-/merge_requests/8325
Comment #5
kalpanajaiswal commentedComment #6
smustgrave commentedSeems straightforward but MR should be against 11.x
Comment #9
kalpanajaiswal commentedMR https://git.drupalcode.org/project/drupal/-/merge_requests/8325 has been updated against 11.x
Comment #10
kalpanajaiswal commentedComment #13
ankitv18 commentedMR!8367 change looking as expected as per proposed solution.
Comment #14
bbralaYes, i think this is fine like this since it is only as docblock.
Comment #16
longwaveCrediting @BenStallings for the original report and @kalpanajaiswal for the fix.
Backported down to 10.3.x as a docs-only fix.
Committed and pushed 9f6143c129 to 11.x and 6f54f45dc2 to 11.0.x and d5deca47b1 to 10.4.x and 35848a5ec5 to 10.3.x. Thanks!
Comment #20
longwave