Skip to content
← Back to Snippets
Code

Avoid Arrow Functions with Named Callbacks

Shows the named-callback alternative to arrow functions for clearer procedural code review.

Purpose

Shows the named-callback alternative to arrow functions for clearer procedural code review.

Snippet details

ContextCallbackLevelProductionCopy-and-paste statusMarked safe after review.

Categories

  • Forms and Validation

Code

<?php

/*
 * Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
 * Free for personal and internal use. Paid project use requires visible credit
 * to Jeffery L. Paris. Corporate use requires a paid license fee unless a
 * separate written license states otherwise.
 */

/**
 * Checks whether a mission is active.
 *
 * @param array $mission Mission row.
 * @return bool Whether the mission is active.
 */
function ogSnippetNamedCallbackKeepActiveMission(array $mission): bool {
	if (isset($mission['active']) === false) {
		return false;
	}

	if ($mission['active'] === true) {
		return true;
	}

	return false;
}

/**
 * Avoid Arrow Functions with Named Callbacks.
 *
 * Purpose:
 * Filters active rows with a named callback instead of an arrow function.
 *
 * @param array $missions Mission rows.
 * @return array Active mission rows.
 */
function ogSnippetAvoidArrowFunctionsWithNamedCallbacks(array $missions): array {
	$active = array_filter($missions, 'ogSnippetNamedCallbackKeepActiveMission');

	return array_values($active);
}

$active_missions = ogSnippetAvoidArrowFunctionsWithNamedCallbacks(array(
	array('name' => 'SG-1 recon', 'active' => true),
	array('name' => 'Atlantis archive', 'active' => false)
));

echo 'Active missions: '.count($active_missions);