Skip to content
← Back to Snippets
Code

Shared Procedural Helper Pattern Without Traits

Shows how small prefixed procedural helpers can share formatting behavior without traits or classes.

Purpose

Shows how small prefixed procedural helpers can share formatting behavior without traits or classes.

Snippet details

ContextUtilityLevelProductionCopy-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.
 */

/**
 * Formats a procedural callsign value.
 *
 * @param string $callsign Raw callsign.
 * @return string Normalized callsign.
 */
function ogSnippetFormatProceduralCallsign(string $callsign): string {
	$callsign = strtoupper(trim($callsign));
	$callsign = preg_replace('/[^A-Z0-9\-]+/', '-', $callsign);

	if (is_string($callsign) === false) {
		$callsign = '';
	}

	$callsign = trim($callsign, '-');

	return $callsign;
}

/**
 * Shared Procedural Helper Pattern Without Traits.
 *
 * Purpose:
 * Uses a small shared helper function to keep repeated procedural formatting
 * in one place without traits.
 *
 * @param array $ships Ship rows with callsign and status values.
 * @return array Formatted ship labels.
 */
function ogSnippetSharedProceduralHelperPatternWithoutTraits(array $ships): array {
	$labels = array();

	foreach ($ships as $ship) {
		if (is_array($ship) === false) {
			continue;
		}

		$callsign = '';
		$status = 'unknown';

		if (isset($ship['callsign']) === true) {
			$callsign = ogSnippetFormatProceduralCallsign((string) $ship['callsign']);
		}

		if (isset($ship['status']) === true) {
			$status = strtolower(trim((string) $ship['status']));
		}

		if ($callsign === '') {
			continue;
		}

		$labels[] = $callsign.' ['.$status.']';
	}

	return $labels;
}

$ship_labels = ogSnippetSharedProceduralHelperPatternWithoutTraits(array(
	array('callsign' => 'red-five', 'status' => 'ready'),
	array('callsign' => 'millennium-falcon', 'status' => 'standby')
));

echo 'Star Wars helper label: '.$ship_labels[0];