Developers

PHP Coding Style

Hubzero writes PHP to PSR-12 with three deliberate departures: indentation is a tab, the opening brace of a control structure goes on its own line, and a leading underscore on a non-public member is still allowed. Everything else PSR-12 says holds.

The three departures

None of the three is a better idea than what PSR-12 asks for. They are older than PSR-12, they are what the tree is written in, and the cost of changing them is the point: reformatting 2,800 files rewrites every line of every file, which destroys git blame, turns every open pull request into a conflict, and buries the next real change in a diff nobody can review. The style is frozen because unfreezing it is expensive, not because it is right.

The rule that follows from that: match the file you are editing. A patch that fixes one line and reindents the surrounding forty is a patch a reviewer has to read twice.

Indent with tabs

One tab per level. PSR-12 asks for four spaces; core does not use them. Of the 2,870 PHP files under core/components, 2,809 indent with tabs and 32 with spaces.

Newer framework files written from scratch — Hubzero\Base\ClassLoader, for one — use four spaces. Match the file you are editing. Never mix the two in a single file, and never reindent a file you are otherwise only patching: it buries the change in a diff nobody can read.

Braces on their own line

The opening brace goes on the line below, for classes, methods, functions and control structures. PSR-12 puts a control structure's brace on the same line; core does not.

if ($filters['month'] > 12)
{
	$filters['month'] = 0;
}

foreach ($rows as $row)
{
	$row->save();
}

A leading underscore is allowed

PSR-12 says an underscore prefix has no meaning. Core uses one on protected and private members throughout — 887 methods and 718 properties under core/components and core/libraries. Do not add the prefix to new code; do not strip it from existing code either, because the name is part of the class's contract with its subclasses.

That last part is the reason this one is not simply a tidy-up waiting to happen. Renaming _sortNames() to sortNames() in a base class silently breaks every subclass that overrides it — including subclasses in extensions that are not in this repository, on hubs you cannot see. PHP raises nothing; the override just stops being an override and the base implementation runs instead. The prefix stays.

Files

  • Open with <?php. Short tags are never allowed, and <?= appears nowhere in core.
  • Omit the closing ?> in a file that is only PHP; it is not required, and leaving it off keeps trailing whitespace out of the response. A file that ends in markup keeps its final ?> because it has to.
  • End the file with a single newline, and use Unix line endings (LF, 0x0A). Never CR or CRLF.
  • One class per file.

Every PHP file opens with the same four-line docblock:

<?php
/**
 * @package    hubzero-cms
 * @copyright  Copyright (c) 2005-2020 The Regents of the University of California.
 * @license    http://opensource.org/licenses/MIT MIT
 */

@package is hubzero-cms for extensions and framework for files under core/libraries/Hubzero. @license is always MIT. A new file gets

 * @copyright  Copyright © 2026 Purdue University. All Rights Reserved.

and a file modified from core keeps the earliest year already there: Copyright © 2015-2026 …. The long LGPL preamble that older documentation showed is gone; no file in the tree carries it.

The direct-access gate

A file that is reached by including it — a view template, a plugin entry point, a helper the router pulls in — declares the gate immediately after the header:

// No direct access
defined('_HZEXEC_') or die;

3,389 files do. A file that is only ever reached through the autoloader does not need it; the autoloader will not run arbitrary code on request.

Importing global facades

The CMS registers Route, App, User, Lang, Event, Config, Request, Notify and the rest as root-namespace aliases. Inside a namespaced file an unqualified Route::url() resolves to Current\Namespace\Route, not the alias, and fatals when the line runs. Import every facade you call:

namespace Components\Blog\Site\Controllers;

use Hubzero\Component\SiteController;
use Request;
use Route;
use Lang;
use User;

A leading backslash (\Route::url()) works too, but the use list is the house form. This is the one style rule with a linter behind it; see below.

Lines

The soft limit is 120 characters. There is no hard limit: a longer line is a warning, never an error, and readability wins over the count. Split a long call by pulling its arguments into variables first.

No trailing whitespace. One statement per line.

Strings

Single quotes for a literal with no substitution:

$a = 'Example String';

Double quotes when the string contains apostrophes or interpolates a variable. Both interpolation forms are accepted:

$greeting = "Hello $name, welcome back!";
$greeting = "Hello {$name}, welcome back!";

"${name}" is not permitted; PHP 8.2 deprecates it.

Concatenate with ., a space on each side. When a concatenated expression runs long, break it and align the . under the =:

$sql = "SELECT `id`, `name` FROM `users` "
     . "WHERE `name` = 'Jim' "
     . "ORDER BY `name` ASC";

Arrays

array() is the prevailing form in core — 11,377 uses under core/components — and short [] syntax is accepted in new code. Pick one per file.

A single space after each comma. A multi-line array indents its items one level and puts the closing paren on its own line at the level of the declaration:

$filters = array(
	'year'       => Request::getInt('year', 0),
	'month'      => Request::getInt('month', 0),
	'scope'      => $this->config->get('show_from', 'site'),
	'authorized' => false,
	'state'      => 1,
	'access'     => User::getAuthorisedViewLevels()
);

Aligning the => operators is house style, as above. A trailing comma on the last item is allowed but core generally omits it.

Classes

Name classes as PHP Naming Conventions describes. extends and implements stay on the class line:

class Entries extends SiteController
{
}

Declare visibility on every property and method. var is not used. Properties come before methods.

Functions and methods

No space between the name and the opening parenthesis. Arguments separated by , . Arguments with defaults go last, and the default gets a space on each side of the = — $limit = 25, not $limit=25. Both forms are in core; the spaced one is more common and is what PSR-12 asks for. Do not wrap a return value in parentheses — return $this->bar;, not return($this->bar);.

Call-time pass-by-reference (foo(&$bar) at the call site) is a PHP fatal and is never used. Declare the reference in the signature instead:

public function onContentPrepare($context, &$article, &$params)
{
}

Control structures

One space after the keyword, none inside the parentheses. Braces are always required, even for a single statement. elseif, not else if — both appear in core, and elseif is the rule for new code.

Break a long condition before the operator, and indent the continuation so the operator hangs one column left of the first clause:

		if (!User::authorise('core.edit', $this->_option)
		 && !User::authorise('core.create', $this->_option))
		{
			App::abort(403, Lang::txt('JERROR_ALERTNOAUTHOR'));
		}

Put the operator at the start of the continuation line, not the end of the one before, and do not mix the two in one condition.

A switch indents its case one level and the case body one more. Give it a default. Where a non-empty case falls through deliberately, say so with a // no break comment, so the next reader does not read it as a bug:

switch ($state)
{
	case 'archived':
		$this->archive();
		// no break

	case 'published':
		$this->publish();
		break;

	default:
		break;
}

Documentation blocks

Docblocks are phpDocumentor format. Every class gets a short description; every method gets a description, its parameters, and its return type. Columns are aligned with spaces, two after each tag:

	/**
	 * Generate an alias from the data being saved
	 *
	 * @param   array   $data  the data being saved
	 * @return  string
	 */
	public function automaticAlias($data)

Add @throws for every exception class a method can raise. Do not write @access; the visibility modifier already says it. Do not write @version or @package_version@; nothing substitutes them.

SQL

Keywords uppercase, identifiers lowercase and backquoted. Write the table prefix as the #__ placeholder, never a literal jos_ — see Database Schema.

$this->db->setQuery("SELECT `id` FROM `#__usergroups` WHERE `title` = " . $this->db->quote($title));

Quote every value through $db->quote() or bind it. Prefer the query builder over hand-written SQL in new code; see Database.

Checking your work

The tools live under core/vendor/bin, installed by php bin/composer install run from the core directory. The lint and test commands expect that working directory too:

cd core
vendor/bin/parallel-lint --exclude vendor .
vendor/bin/phpunit -c phpunit.xml.dist

The facade check runs from the repository root:

php core/bin/lint/missing-facade-imports.php

It takes --fix to insert the missing use statements, and any path to narrow the scan; with no path it scans core/components, core/plugins, core/modules and core/libraries/Hubzero.

phpcs is installed as a development dependency but the repository commits no ruleset, so running plain PSR-12 against core reports mostly the three departures above: one ordinary controller draws 772 errors, nearly all of them tab indentation and control-structure braces. Excluding the sniffs that see only those departures makes the result readable — the same file drops to a few dozen findings worth reading:

house_style=Generic.WhiteSpace.DisallowTabIndent
house_style=$house_style,Generic.WhiteSpace.ScopeIndent
house_style=$house_style,Squiz.WhiteSpace.ScopeClosingBrace
house_style=$house_style,Squiz.ControlStructures.ControlSignature
house_style=$house_style,PSR12.ControlStructures.ControlStructureSpacing
house_style=$house_style,PSR2.Methods.MethodDeclaration
house_style=$house_style,Squiz.Functions.MultiLineFunctionDeclaration

core/vendor/bin/phpcs --standard=PSR12 --exclude=$house_style path/to/changed/file.php

Run it on the files you changed, not on the tree.

When the linter and the house style disagree

phpcs has no committed ruleset here, so there is nothing in the repository that encodes the house style. --standard=PSR12 is the closest thing available and it is wrong about three rules, which is why the exclude list above exists. When a finding and this page disagree:

  1. The three departures win. Tabs, braces on their own line, and a leading underscore on an existing non-public member are the house style. phpcs is not configured to know that. Exclude the sniff, do not change the code.
  2. Everything else, phpcs wins. Trailing whitespace, a missing visibility modifier, a space before a comma, else if for elseif — fix those. They are PSR-12 and they are also what this page asks for.
  3. The file you are in wins over both, for anything cosmetic. A file already written with four-space indentation or [] arrays stays that way. Consistency inside one file beats consistency across the tree, because the reader is looking at one file.
  4. When it is still not clear, leave it. A style question that needs a decision is not worth holding a fix for. Say in the pull request that you left it and why; a reviewer can settle it in one comment.

Do not add a ruleset to the repository as part of an unrelated change. A committed phpcs.xml would decide the house style for everyone, and that is its own pull request with its own discussion.

What the build checks

Three GitHub Actions workflows run on a pull request, each on the paths it cares about.

Workflow Checks
php-lint.yml php -l over every *.php under core and app outside vendor, then core/bin/lint/missing-facade-imports.php, then core/bin/lint/undefined-language-keys.php against a ceiling of 444
tests.yml The PHPUnit suite, for a change under core
pages.yml Builds the documentation, regenerates docs/reference, checks every internal link, and fails if the committed gh-pages/public is stale

None of them runs phpcs. A style problem is caught in review, not by the build. A syntax error, a missing facade import, a language key nothing defines, and a broken test are caught by the build.

Rewritten and checked against 2.4-main @ 348f0057c2 on 2026-09-10.