Skip to content

Commit

Permalink
Remove many remaining uses of references
Browse files Browse the repository at this point in the history
See smrealms#317.

This handles three major remaining usages:

* When returning objects or arrays from class methods.
  Objects should almost always be returned "by value" (since they
  are handled internally as references by PHP). Arrays should be
  returned "by value" except, perhaps, if they will be modified.

* In foreach loops. The only time a reference is needed here is
  when you want to loop over an array and modify array elements.

* In template includes. We never want to modify variables within
  templates, so there is no reason to use references here.
  • Loading branch information
hemberger committed Mar 15, 2019
1 parent f76ccdc commit 2051bfe
Show file tree
Hide file tree
Showing 45 changed files with 103 additions and 114 deletions.
2 changes: 1 addition & 1 deletion engine/Default/chess.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$chessGames =& ChessGame::getOngoingAccountGames($player->getAccountID());
$chessGames = ChessGame::getOngoingAccountGames($player->getAccountID());
$template->assign('ChessGames', $chessGames);

$playersChallenged = array($player->getAccountID() => true);
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/chess_move_processing.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$chessGame =& ChessGame::getChessGame($var['ChessGameID']);
$chessGame = ChessGame::getChessGame($var['ChessGameID']);
$template->assign('ChessGame',$chessGame);
if(is_numeric($_REQUEST['x']) && is_numeric($_REQUEST['y']) && is_numeric($_REQUEST['toX']) && is_numeric($_REQUEST['toY'])) {
$x = $_REQUEST['x'];
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/chess_resign_processing.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

$chessGame =& ChessGame::getChessGame($var['ChessGameID']);
$chessGame = ChessGame::getChessGame($var['ChessGameID']);
$result = $chessGame->resign($player->getAccountID());

$container = create_container('skeleton.php', 'current_sector.php');
Expand Down
8 changes: 4 additions & 4 deletions engine/Default/combat_simulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ function runAnAttack($realAttackers,$realDefenders) {
global $template;
$results = array('Attackers' => array('Traders' => array(), 'TotalDamage' => 0),
'Defenders' => array('Traders' => array(), 'TotalDamage' => 0));
foreach($realAttackers as $accountID => &$teamPlayer) {
foreach ($realAttackers as $accountID => $teamPlayer) {
$playerResults =& $teamPlayer->shootPlayers($realDefenders);
$results['Attackers']['Traders'][] =& $playerResults;
$results['Attackers']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($teamPlayer);
foreach($realDefenders as $accountID => &$teamPlayer) {
}
foreach ($realDefenders as $accountID => $teamPlayer) {
$playerResults =& $teamPlayer->shootPlayers($realAttackers);
$results['Defenders']['Traders'][] =& $playerResults;
$results['Defenders']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($teamPlayer);
}
$template->assign('TraderCombatResults',$results);
}
4 changes: 2 additions & 2 deletions engine/Default/edit_dummys.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

$template->assign('EditDummysLink',SmrSession::getNewHREF(create_container('skeleton.php','edit_dummys.php')));

$dummyPlayer =& DummyPlayer::getCachedDummyPlayer($_REQUEST['dummy_name']);
$dummyShip =& $dummyPlayer->getShip();
$dummyPlayer = DummyPlayer::getCachedDummyPlayer($_REQUEST['dummy_name']);
$dummyShip = $dummyPlayer->getShip();

if(isset($_REQUEST['save_dummy'])) {
$dummyPlayer->setPlayerName($_REQUEST['dummy_name']);
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/galactic_post_view_applications.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
else
$PHP_OUTPUT.=('You have no applications to view at the current time.');
while ($db->nextRecord()) {
$appliee =& SmrPlayer::getPlayer($db->getField('account_id'), $player->getGameID());
$appliee = SmrPlayer::getPlayer($db->getField('account_id'), $player->getGameID());

$container = array();
$container['url'] = 'skeleton.php';
Expand All @@ -30,7 +30,7 @@
$db->query('SELECT * FROM galactic_post_applications WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND account_id = '.$db->escapeNumber($var['id']));
$db->nextRecord();
$desc = stripslashes($db->getField('description'));
$applie =& SmrPlayer::getPlayer($var['id'], $player->getGameID());
$applie = SmrPlayer::getPlayer($var['id'], $player->getGameID());
$PHP_OUTPUT.=('Name : '.$applie->getPlayerName().'<br />');
$PHP_OUTPUT.=('Have you written for some kind of newspaper before? ' . $db->getField('written_before'));
$PHP_OUTPUT.=('<br />');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/galactic_post_view_members.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

while ($db->nextRecord()) {

$curr_writter =& SmrPlayer::getPlayer($db->getField('account_id'), $player->getGameID());
$curr_writter = SmrPlayer::getPlayer($db->getField('account_id'), $player->getGameID());
$time = $db->getField('last_wrote');
$PHP_OUTPUT.=('<tr>');
$PHP_OUTPUT.=('<td align="center">'.$curr_writter->getPlayerName().'</td>');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/sector_jump_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
acquire_lock($player->getSectorID());

// get new sector object
$sector =& $player->getSector();
$sector = $player->getSector();

// make current sector visible to him
$sector->markVisited($player);
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/sector_move_processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
$player->update();

// get new sector object
$sector =& $player->getSector();
$sector = $player->getSector();
$sector->markVisited($player);
forward(create_container('skeleton.php', $var['target_page']));
}
Expand Down Expand Up @@ -89,7 +89,7 @@
acquire_lock($var['target_sector']);

// get new sector object
$sector =& $player->getSector();
$sector = $player->getSector();

//add that the player explored here if it hasnt been explored...for HoF
if (!$sector->isVisited($player)) {
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/shop_ship_processing.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$shipID = $var['ship_id'];
$newShip =& AbstractSmrShip::getBaseShip(Globals::getGameType($player->getGameID()),$shipID);
$newShip = AbstractSmrShip::getBaseShip(Globals::getGameType($player->getGameID()),$shipID);
$cost = $ship->getCostToUpgrade($shipID);

// trade master 33
Expand Down
14 changes: 7 additions & 7 deletions lib/Default/ChessGame.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class ChessGame {
WHERE end_time > ' . TIME . ' OR end_time IS NULL;');
$games = array();
while($db->nextRecord()) {
$game =& self::getChessGame($db->getInt('chess_game_id'), $forceUpdate);
$game = self::getChessGame($db->getInt('chess_game_id'), $forceUpdate);
if($game->getCurrentTurnAccount()->isNPC()) {
$games[] =& $game;
$games[] = $game;
}
}
return $games;
Expand All @@ -46,7 +46,7 @@ class ChessGame {
$db->query('SELECT chess_game_id FROM chess_game WHERE (black_id = ' . $db->escapeNumber($accountID) . ' OR white_id = ' . $db->escapeNumber($accountID) . ') AND (end_time > ' . TIME . ' OR end_time IS NULL);');
$games = array();
while($db->nextRecord()) {
$games[] =& self::getChessGame($db->getInt('chess_game_id'));
$games[] = self::getChessGame($db->getInt('chess_game_id'));
}
return $games;
}
Expand All @@ -56,7 +56,7 @@ class ChessGame {
$db->query('SELECT chess_game_id FROM chess_game WHERE black_id = ' . $db->escapeNumber($accountID) . ' OR white_id = ' . $db->escapeNumber($accountID) . ';');
$games = array();
while($db->nextRecord()) {
$games[] =& self::getChessGame($db->getInt('chess_game_id'));
$games[] = self::getChessGame($db->getInt('chess_game_id'));
}
return $games;
}
Expand Down Expand Up @@ -642,7 +642,7 @@ class ChessGame {
if($this->getCurrentTurnAccountID() != $forAccountID) {
return 4;
}
$lastTurnPlayer =& $this->getCurrentTurnPlayer();
$lastTurnPlayer = $this->getCurrentTurnPlayer();
$this->getBoard();
$p = $this->board[$y][$x];
if($p == null || $p->colour != $this->getColourForAccountID($forAccountID)) {
Expand All @@ -653,7 +653,7 @@ class ChessGame {
foreach($moves as $move) {
if($move[0]==$toX && $move[1]==$toY) {
$chessType = $this->isNPCGame() ? 'Chess (NPC)' : 'Chess';
$currentPlayer =& $this->getCurrentTurnPlayer();
$currentPlayer = $this->getCurrentTurnPlayer();

$moveInfo = ChessGame::movePiece($this->board, $this->getHasMoved(), $x, $y, $toX, $toY, $pawnPromotionPiece);

Expand Down Expand Up @@ -689,7 +689,7 @@ class ChessGame {
return 3;
}

$otherPlayer =& $this->getCurrentTurnPlayer();
$otherPlayer = $this->getCurrentTurnPlayer();
if($moveInfo['PawnPromotion'] !== false) {
$piecePromotedSymbol = $p->getPieceSymbol();
$currentPlayer->increaseHOF(1, array($chessType,'Moves','Own Pawns Promoted','Total'), HOF_PUBLIC);
Expand Down
2 changes: 1 addition & 1 deletion lib/Default/DummyPlayer.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DummyPlayer extends AbstractSmrPlayer {
}

public function killPlayer($sectorID) {
// $sector =& SmrSector::getSector($this->getGameID(),$sectorID);
// $sector = SmrSector::getSector($this->getGameID(),$sectorID);
//msg taken care of in trader_att_proc.php
// forget plotted course
// $this->deletePlottedCourse();
Expand Down
10 changes: 4 additions & 6 deletions lib/Default/RouteGenerator.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,14 @@ class RouteGenerator {
if ($goods[GOOD_NOTHING]===true)
$rl[] = new OneWayRoute($currentSectorId, $targetSectorId, $races[$raceID], $sectors[$targetSectorId]->getPort()->getRaceID(), 0, 0, $distance, GOOD_NOTHING);

$gameGoods = Globals::getGoods();
foreach($gameGoods as $goodId => &$value) {
foreach (Globals::getGoods() as $goodId => $value) {
if ($goods[$goodId]===true) {
if ($sectors[$currentSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_SELLS &&
$sectors[$targetSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_BUYS) {
$rl[] = new OneWayRoute($currentSectorId, $targetSectorId, $races[$raceID], $sectors[$targetSectorId]->getPort()->getRaceID(), $sectors[$currentSectorId]->getPort()->getGoodDistance($goodId), $sectors[$targetSectorId]->getPort()->getGoodDistance($goodId), $distance, $goodId);
}
}
} unset($value);
}
} unset($distance);
$routes[$sectors[$currentSectorId]->getSectorID()] = $rl;
} unset($d);
Expand All @@ -128,8 +127,7 @@ class RouteGenerator {
if($routesForPort!==-1 && $currentSectorId !== $routesForPort && $targetSectorId !== $routesForPort)
continue;

$gameGoods =& Globals::getGoods();
foreach($gameGoods as $goodId => &$value) {
foreach (Globals::getGoods() as $goodId => $value) {
if ($goods[$goodId]===true) {
if ($sectors[$currentSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_SELLS &&
$sectors[$targetSectorId]->getPort()->getGoodTransaction($goodId) === self::GOOD_BUYS) {
Expand All @@ -140,7 +138,7 @@ class RouteGenerator {
self::addMoneyRoute($mpr);
}
}
} unset($value);
}
} unset($distance);
} unset($d);
$allRoutes = array();
Expand Down
2 changes: 1 addition & 1 deletion lib/Default/smr.inc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function smrBBCode($bbParser, $action, $tagName, $default, $tagParams, $tagConte
break;
case 'chess':
$chessGameID = $default;
$chessGame =& ChessGame::getChessGame($chessGameID);
$chessGame = ChessGame::getChessGame($chessGameID);
if ($action == \Nbbc\BBCode::BBCODE_CHECK) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<td>
<form method="POST" action="<?php echo $JumpGalaxyHREF; ?>">
<select name="gal_on" onchange="this.form.submit()"><?php
foreach($Galaxies as &$CurrentGalaxy) { ?>
foreach($Galaxies as $CurrentGalaxy) { ?>
<option value="<?php echo $CurrentGalaxy->getGalaxyID(); ?>"<?php if($CurrentGalaxy->equals($Galaxy)) { ?> selected="SELECTED"<?php } ?>><?php
echo $CurrentGalaxy->getName(); ?>
</option><?php
Expand Down
4 changes: 2 additions & 2 deletions templates/Default/admin/Default/account_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@
if(count($EditingPlayers)) { ?>
<a onclick="$('#accountPlayers').fadeToggle(600);">Show/Hide</a>
<table id="accountPlayers" style="display:none"><?php
foreach($EditingPlayers as &$CurrentPlayer) {
$CurrentShip =& $CurrentPlayer->getShip(); ?>
foreach ($EditingPlayers as $CurrentPlayer) {
$CurrentShip = $CurrentPlayer->getShip(); ?>
<tr>
<td align="right">Game ID:</td>
<td><?php echo $CurrentPlayer->getGameID(); ?></td>
Expand Down
2 changes: 1 addition & 1 deletion templates/Default/admin/Default/log_console.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<th>Notes</th>
</tr><?php

foreach($LoggedAccounts as &$LoggedAccount) { ?>
foreach($LoggedAccounts as $LoggedAccount) { ?>
<tr>
<td valign="top"><?php echo $LoggedAccount['Login']; ?></td>
<td valign="top" align="center"><?php echo $LoggedAccount['TotalEntries']; ?></td>
Expand Down
6 changes: 3 additions & 3 deletions templates/Default/engine/Default/alliance_pick.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<th>Members</th>
<th>Pick</th>
</tr><?php
foreach ($Teams as &$Team) {
foreach ($Teams as $Team) {
// boldface this row if it is the current player's alliance
$Class = ($Team['Leader']->getPlayerID() == $PlayerID) ? "bold" : ""; ?>
<tr class="<?php echo $Class; ?>">
Expand Down Expand Up @@ -43,7 +43,7 @@
<th>HoF Name</th>
<th>User Score</th>
</tr><?php
foreach($PickPlayers as &$PickPlayer) { ?>
foreach($PickPlayers as $PickPlayer) { ?>
<tr>
<td class="center"><?php
if ($CanPick) { ?>
Expand Down Expand Up @@ -84,7 +84,7 @@
<th>HoF Name</th>
<th>User Score</th>
</tr><?php
foreach(array_reverse($History, true) as $i => &$Pick) { ?>
foreach(array_reverse($History, true) as $i => $Pick) { ?>
<tr>
<td class="center"><?php echo $i+1; ?></td>
<td><?php echo $Pick['Leader']->getPlayerName(); ?></td>
Expand Down
8 changes: 4 additions & 4 deletions templates/Default/engine/Default/alliance_roles.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<h2>Current Roles</h2><br /><?php
foreach($AllianceRoles as $RoleID => &$Role) {
$this->includeTemplate('includes/AllianceRole.inc',array('Role' => &$Role));
foreach($AllianceRoles as $RoleID => $Role) {
$this->includeTemplate('includes/AllianceRole.inc',array('Role' => $Role));
} ?><br />
<h2>Create Role</h2><br /><?php
$this->includeTemplate('includes/AllianceRole.inc',array('Role' => &$CreateRole)); ?>
$this->includeTemplate('includes/AllianceRole.inc',array('Role' => $CreateRole)); ?>
<b>Usage:</b><br />
To add a new entry input the name of the role in the name field and press 'Create'.<br />
To delete an entry clear the box and click 'Edit'.
To delete an entry clear the box and click 'Edit'.
2 changes: 1 addition & 1 deletion templates/Default/engine/Default/bank_alliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
?><th class="shrink noWrap">Make Exempt</th><?php
} ?>
</tr><?php
foreach($BankTransactions as $TransactionID => &$BankTransaction) { ?>
foreach($BankTransactions as $TransactionID => $BankTransaction) { ?>
<tr>
<td class="center"><?php echo number_format($TransactionID); ?></td>
<td class="center noWrap"><?php echo date(DATE_FULL_SHORT_SPLIT, $BankTransaction['Time']); ?></td>
Expand Down
6 changes: 3 additions & 3 deletions templates/Default/engine/Default/chess.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
foreach($ChessGames as $ChessGame) { ?>
<tr>
<td><?php
$WhitePlayer =& $ChessGame->getWhitePlayer();
$BlackPlayer =& $ChessGame->getBlackPlayer();
$WhitePlayer = $ChessGame->getWhitePlayer();
$BlackPlayer = $ChessGame->getBlackPlayer();
if($WhitePlayer == null) {
?>Unknown<?php
}
Expand All @@ -27,7 +27,7 @@
} ?>
</td>
<td><?php
$CurrentTurnPlayer =& $ChessGame->getCurrentTurnPlayer();
$CurrentTurnPlayer = $ChessGame->getCurrentTurnPlayer();
if($CurrentTurnPlayer == null) {
?>Unknown<?php
}
Expand Down
2 changes: 1 addition & 1 deletion templates/Default/engine/Default/chess_play.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p>It is currently <span id="turn"><?php
$CurrentTurnPlayer =& $ChessGame->getCurrentTurnPlayer();
$CurrentTurnPlayer = $ChessGame->getCurrentTurnPlayer();
if($CurrentTurnPlayer == null) {
?>Unknown<?php
}
Expand Down
5 changes: 2 additions & 3 deletions templates/Default/engine/Default/council_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</thead>
<tbody class="list"><?php
foreach($CouncilMembers as $Ranking => $AccountID) {
$CouncilPlayer =& SmrPlayer::getPlayer($AccountID, $ThisPlayer->getGameID()); ?>
$CouncilPlayer = SmrPlayer::getPlayer($AccountID, $ThisPlayer->getGameID()); ?>
<tr id="player-<?php echo $CouncilPlayer->getPlayerID(); ?>" class="ajax<?php if ($ThisPlayer->equals($CouncilPlayer)) { ?> bold<?php } ?>">
<td class="right"><?php echo $Ranking; ?></td>
<td class="name"><?php echo $CouncilPlayer->getLevelName(); ?> <?php echo $CouncilPlayer->getLinkedDisplayName(false); ?></td>
Expand All @@ -64,8 +64,7 @@
<br /><br />

<b>View Council For:</b><br /><?php
$Races =& Globals::getRaces();
foreach($Races as $RaceID => $RaceInfo) {
foreach (Globals::getRaces() as $RaceID => $RaceInfo) {
if($RaceID != RACE_NEUTRAL) { ?>
<span class="smallFont"><?php
echo $ThisPlayer->getColouredRaceName($RaceID, true); ?>
Expand Down
2 changes: 1 addition & 1 deletion templates/Default/engine/Default/current_sector.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
$this->includeTemplate('includes/SectorPlanet.inc');
$this->includeTemplate('includes/SectorPort.inc');
$this->includeTemplate('includes/SectorLocations.inc');
$this->includeTemplate('includes/SectorPlayers.inc',array('PlayersContainer'=>&$ThisSector));
$this->includeTemplate('includes/SectorPlayers.inc',array('PlayersContainer'=>$ThisSector));
$this->includeTemplate('includes/SectorForces.inc'); ?>
<br />
6 changes: 3 additions & 3 deletions templates/Default/engine/Default/edit_dummys.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</select><br />
<input type="submit" value="Select Dummy" />
</form>
<?php $DummyShip =& $DummyPlayer->getShip(); ?>
<?php $DummyShip = $DummyPlayer->getShip(); ?>
<table>
<tr>
<td class="top">
Expand All @@ -33,7 +33,7 @@
Weapon: <?php echo $OrderID+1; ?>
<select name="weapons[]">
<option value="0">None</option><?php
foreach($Weapons as &$Weapon) {
foreach($Weapons as $Weapon) {
?><option value="<?php echo $Weapon->getWeaponTypeID(); ?>"<?php if($Weapon->getWeaponTypeID()==$ShipWeapon->getWeaponTypeID()){ ?> selected="selected"<?php } ?>><?php echo $Weapon->getName(); ?> (dmg: <?php echo $Weapon->getShieldDamage(); ?>/<?php echo $Weapon->getArmourDamage(); ?> acc: <?php echo $Weapon->getBaseAccuracy(); ?>% lvl:<?php echo $Weapon->getPowerLevel(); ?>)</option><?php
} ?>
</select><br /><?php
Expand All @@ -42,7 +42,7 @@
Weapon: <?php echo $OrderID+1; ?>
<select name="weapons[]">
<option value="0">None</option><?php
foreach($Weapons as &$Weapon) {
foreach($Weapons as $Weapon) {
?><option value="<?php echo $Weapon->getWeaponTypeID(); ?>"><?php echo $Weapon->getName(); ?> (dmg: <?php echo $Weapon->getShieldDamage(); ?>/<?php echo $Weapon->getArmourDamage(); ?> acc: <?php echo $Weapon->getBaseAccuracy(); ?>% lvl:<?php echo $Weapon->getPowerLevel(); ?>)</option><?php
} ?>
</select><br /><?php
Expand Down
Loading

0 comments on commit 2051bfe

Please sign in to comment.