Completed
Push — work-fleets ( d40084...46e295 )
by SuperNova.WS
05:22
created

FleetTest.php ➔ testFleet()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 44
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 16
nc 6
nop 13
dl 0
loc 44
rs 8.439
c 1
b 0
f 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
use Vector\Vector;
4
5
define('SN_IN_FLEET', true);
6
define('SN_RENDER_NAVBAR_PLANET', true);
7
8
include('../common.' . substr(strrchr(__FILE__, '.'), 1));
9
10
// TODO - Переместить это куда-нибудь
11
$fleet_page = sys_get_param_int('fleet_page', sys_get_param_int('mode'));
12
13
global $template_result, $user, $planetrow;
14
$template_result = !empty($template_result) && is_array($template_result) ? $template_result : array();
15
16
17
require_once('../includes/includes/flt_functions.php');
18
19
lng_include('fleet');
20
21
$targetVector = new Vector(Vector::READ_PARAMS_FLEET, $planetrow);
22
$target_mission = sys_get_param_int('target_mission', MT_NONE);
23
$ships = sys_get_param_array('ships');
24
$fleet_group_mr = sys_get_param_id('fleet_group');
25
$speed_percent = sys_get_param_int('speed', 10);
26
27
$captainId = sys_get_param_id('captain_id');
28
// TODO - Missile - targeted unit ID
29
30
$resources = array(
31
  RES_METAL     => max(0, floor(sys_get_param_float('resource0'))),
32
  RES_CRYSTAL   => max(0, floor(sys_get_param_float('resource1'))),
33
  RES_DEUTERIUM => max(0, floor(sys_get_param_float('resource2'))),
34
);
35
36
37
require_once '_tests.php';
38
39
$testUser = $user;
40
$testPlanetRow = $planetrow;
41
42
$testData = array(
43
//array($fleet_page,        $user,     $planetrow,    $targetVector,                $target_mission, $ships, $fleet_group_mr, $speed_percent, $missileTarget, $captainId, $resources, $exceptionCode, $resourcesOnPlanet)
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
  // checkSpeedPercentOld - Wrong speed_percent (1..10)
45
  array(FLIGHT_FLEET_SPEED_WRONG, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(1, 1, 1, PT_NONE), MT_NONE, array(), 0, 11, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
46
  // checkTargetInUniverse
47
  array(FLIGHT_VECTOR_BEYOND_UNIVERSE, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(-PHP_INT_MAX, 1, 1, PT_NONE), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
48
  array(FLIGHT_VECTOR_BEYOND_UNIVERSE, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(PHP_INT_MAX, 1, 1, PT_NONE), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
49
  array(FLIGHT_VECTOR_BEYOND_UNIVERSE, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(1, -PHP_INT_MAX, 1, PT_NONE), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
50
  array(FLIGHT_VECTOR_BEYOND_UNIVERSE, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(1, PHP_INT_MAX, 1, PT_NONE), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
51
  // checkTargetNotSource
52
  array(FLIGHT_VECTOR_SAME_SOURCE, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector($testPlanetRow['galaxy'], $testPlanetRow['system'], $testPlanetRow['planet'], $testPlanetRow['planet_type']), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
53
  // TODO 'checkSenderNoVacation'      => FLIGHT_PLAYER_VACATION_OWN,
54
  // checkTargetNoVacation
55
  array(FLIGHT_PLAYER_VACATION, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(1, 1, 12, PT_PLANET), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
56
  // checkFleetNotEmpty
57
  array(FLIGHT_SHIPS_NO_SHIPS, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(1, 1, 1, PT_NONE), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
58
59
//  // 'checkUnitsPositive'
60
    array(FLIGHT_SHIPS_NEGATIVE, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(1, 1, 11, PT_PLANET), MT_NONE, array(SHIP_CARGO_SMALL => -1), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
61
//  // 'checkOnlyFleetUnits'        => FLIGHT_SHIPS_UNIT_WRONG,
0 ignored issues
show
Unused Code Comprehensibility introduced by
51% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
62
//  // 'checkOnlyFlyingUnits'       => FLIGHT_SHIPS_UNMOVABLE,
63
//  // 'checkResourcesPositive'     => FLIGHT_RESOURCES_NEGATIVE,
64
//  // 'checkNotTooFar'             => FLIGHT_FLEET_TOO_FAR,
65
//  // 'checkEnoughCapacity'        => FLIGHT_FLEET_OVERLOAD,
66
//  array(FLIGHT_FLEET_SPEED_WRONG, FLEET_PAGE_MISSION, $testUser, $testPlanetRow, new Vector(1, 1, 1, PT_NONE), MT_NONE, array(), 0, 10, 0, 0, array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0), array(RES_METAL => 0, RES_CRYSTAL => 0, RES_DEUTERIUM => 0)),
67
);
68
69
function testFleet($exceptionCode, $fleet_page, $user, $planetrow, $targetVector, $target_mission, $ships, $fleet_group_mr, $speed_percent, $missileTarget, $captainId, $resources, $resourcesOnPlanet) {
0 ignored issues
show
Unused Code introduced by
The parameter $missileTarget is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
// Инициализируем объекты значениями по умолчанию
71
  $objFleet5 = new Fleet();
72
  try {
73
    $objFleet5->initDefaults($user, $planetrow, $targetVector, $target_mission, $ships, $fleet_group_mr, $speed_percent, 0, $captainId, $resources);
74
75
    switch ($fleet_page) {
76
//  case FLEET_PAGE_DESTINATION:
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
77
//    $objFleet5->fleetPage1();
78
//  break;
79
//
80
      case FLEET_PAGE_MISSION:
81
        $objFleet5->fleetPage2Prepare($resourcesOnPlanet);
82
      break;
83
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
84
//  case FLEET_PAGE_SEND:
85
//    $objFleet5->fleetPage3();
86
//  break;
87
//
88
//  case 4:
89
//    require('../includes/includes/flt_page4.inc');
90
//  break;
91
//
92
//  case 5:
93
//    require('../includes/includes/flt_page5.inc');
94
//  break;
95
//
96
//  default:
97
//    $objFleet5->fleetPage0();
98
//  break;
99
    }
100
101
    print('Passed');
102
  } catch (Exception $e) {
103
    if ($exceptionCode !== null && $e->getCode() === $exceptionCode) {
104
      print('<font color="green">Exception [' . $exceptionCode . ']: "' . classLocale::$lang['fl_attack_error'][$e->getCode()] . '" - passed</font><br />');
105
    } else {
106
      print('<font color="red">');
107
      throw $e;
108
    }
109
  }
110
111
  unset($objFleet5);
112
}
113
114
_testProcess('testFleet', $testData);
115