mediawiki-extensions-Echo/includes/Bundler.php
Umherirrender dbce3cc026 build: Swap deprecated @codingStandardsIgnore to phpcs:ignore
Fixed a suppressed sniff issue

Bug: T278594
Change-Id: I5a3ae09cd0dd6a27699557e472d2ff74ef03a731
2021-03-27 01:04:58 +01:00

48 lines
1.2 KiB
PHP

<?php
class Bundler {
private function sort( &$array ) {
// We have to ignore the error here (use @usort)
// otherwise this code fails when executed by unit tests
// See: https://bugs.php.net/bug.php?id=50688
// phpcs:ignore Generic.PHP.NoSilencedErrors
@usort( $array, function ( Bundleable $a, Bundleable $b ) {
return strcmp( $b->getSortingKey(), $a->getSortingKey() );
} );
}
/**
* Bundle bundleable elements that can be bundled by their bundling keys
*
* @param Bundleable[] $bundleables
* @return Bundleable[] Grouped notifications sorted by timestamp DESC
*/
public function bundle( array $bundleables ) {
$groups = [];
$bundled = [];
/** @var Bundleable $element */
foreach ( $bundleables as $element ) {
if ( $element->canBeBundled() && $element->getBundlingKey() ) {
$groups[ $element->getBundlingKey() ][] = $element;
} else {
$bundled[] = $element;
}
}
foreach ( $groups as $bundlingKey => $group ) {
$this->sort( $group );
/** @var Bundleable $base */
$base = array_shift( $group );
$base->setBundledElements( $group );
$bundled[] = $base;
}
$this->sort( $bundled );
return $bundled;
}
}