fix(php8): silence warnings raised by the attack/report flow [#203]

PHP 8.x turned several long-standing implicit accesses into runtime
warnings that flooded the error log during attacks and report viewing:

- Battle.report (Templates/Notice/1.tpl): the prisoner check ran
  array_sum() over slots that may hold non-numeric text (the
  "Information" line can shift indices), raising
  "Addition is not supported on type string". Sum int-cast values.
- Database::getUnitsNumber(): movement/reinforcement arrays can be null
  or miss tribe-specific unit keys, raising "Undefined array key" and
  "Trying to access array offset on null". Null-coalesce to 0.
- Automation (combat resolver): $scout was only defined for scouting
  attacks (type 1) yet always passed to buildCombatReport(), raising
  "Undefined variable $scout". Initialize it for every attack type.
- Automation: the destroy-village check read $to['natar'] which is not
  always set, raising "Undefined array key". Use empty().

Pure null-safety / behaviour-preserving changes; no gameplay logic
altered.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Ferywir
2026-06-12 14:20:01 +02:00
committed by Catalin Novgorodschi
parent 868ec257b2
commit 6030727a0d
3 changed files with 10 additions and 8 deletions
+3 -3
View File
@@ -1842,7 +1842,7 @@ class Automation {
//type of attack
$type = $dataarray[$data_num]['attack_type'];
if($type == 1) $scout = 1;
$scout = ($type == 1) ? 1 : 0;
$ud = ($def_tribe - 1) * 10;
$att_ab = $database->getABTech($data['from']); // Blacksmith level
@@ -1941,7 +1941,7 @@ class Automation {
//type of attack
$type = $dataarray[$data_num]['attack_type'];
if($type == 1) $scout = 1;
$scout = ($type == 1) ? 1 : 0;
$att_ab1 = $att_ab2 = $att_ab3 = $att_ab4 = $att_ab5 = $att_ab6 = $att_ab7 = $att_ab8 = 0;
$def_ab[31] = $def_ab[32] = $def_ab[33] = $def_ab[34] = $def_ab[35] = $def_ab[36] = $def_ab[37] = $def_ab[38] = 0;
@@ -2082,7 +2082,7 @@ class Automation {
$info_cat = $info_chief = $info_ram = $info_hero = ",";
//check to see if can destroy village
if (count($varray) > 1 && !$database->villageHasArtefact($DefenderWref) && !$to['natar']) {
if (count($varray) > 1 && !$database->villageHasArtefact($DefenderWref) && empty($to['natar'])) {
$can_destroy = 1;
}
else $can_destroy = 0;
+4 -4
View File
@@ -6130,12 +6130,12 @@ $q = "INSERT INTO ".TB_PREFIX."demolition VALUES (
$end = ( $ownertribe * 10 );
for ( $i = $start; $i <= $end; $i ++ ) {
$totalunits += $movingunits[ 'u' . $i ];
$totalunits += $reinforcingunits[ 'u' . $i ];
$totalunits += $movingunits[ 'u' . $i ] ?? 0;
$totalunits += $reinforcingunits[ 'u' . $i ] ?? 0;
}
$totalunits += $movingunits['hero'];
$totalunits += $reinforcingunits['hero'];
$totalunits += $movingunits['hero'] ?? 0;
$totalunits += $reinforcingunits['hero'] ?? 0;
return $totalunits;
}
+3 -1
View File
@@ -144,7 +144,9 @@ if ($hasHero) {
}
// PRISONERS (unchanged logic but safer sum)
if (!$spy && array_sum(array_slice($dataarray, 182, 11)) > 0) {
// Cast to int: some reports carry non-numeric text in these slots (e.g. the
// "Information" line shifts indices), which made array_sum() warn under PHP 8.
if (!$spy && array_sum(array_map('intval', array_slice($dataarray, 182, 11))) > 0) {
echo "</tr><tr><th>".PRISONERS."</th>";
for ($i = 182; $i <= 191; $i++) {