🐞 fix: phpcs issue cleanup

This commit is contained in:
Keith Solomon
2026-02-10 07:19:26 -06:00
parent d821a7e684
commit d8d6b34e90
7 changed files with 152 additions and 154 deletions

View File

@@ -47,11 +47,7 @@ if (! isset( $tokenError ) ) {
try { try {
if (! isset( $tokenError ) ) { if (! isset( $tokenError ) ) {
if ( if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['ship_type'] ) && isset( $_POST['waypoint_symbol'] )) {
$_SERVER['REQUEST_METHOD'] === 'POST' &&
isset( $_POST['ship_type'] ) &&
isset( $_POST['waypoint_symbol'] )
) {
$shipType = trim( (string) $_POST['ship_type'] ); $shipType = trim( (string) $_POST['ship_type'] );
$waypointSymbol = trim( (string) $_POST['waypoint_symbol'] ); $waypointSymbol = trim( (string) $_POST['waypoint_symbol'] );

View File

@@ -1,8 +1,15 @@
<?php <?php
/** /**
* Project runtime configuration values. * Spacetraders API Client Library - Project configuration
* *
* @package Spacetraders * Runtime configuration values.
*
* @category Configuration
* @package SpacetradersAPI
* @author Keith Solomon <keith@keithsolomon.net>
* @license MIT License
* @version GIT: <git_id>
* @link https://git.keithsolomon.net/keith/Spacetraders
*/ */
return array( return array(

View File

@@ -1,24 +1,22 @@
<?php <?php
/** /**
* Spacetraders API Exception Class * Spacetraders API Client Library - Exception class
*
* PHP version 7.4
*
* @category Exception
* @package Spacetraders
* @author Keith Solomon <keith@keithsolomon.net>
* @license MIT License
* @link https://github.com/your-repo/spacetraders
*/
/**
* Custom exception class for Spacetraders API related errors.
* *
* This exception is thrown when API calls to the Spacetraders service * This exception is thrown when API calls to the Spacetraders service
* encounter errors such as network issues, invalid responses, authentication * encounter errors such as network issues, invalid responses, authentication
* failures, or other API-specific problems. * failures, or other API-specific problems.
* *
* @category Exception
* @package SpacetradersAPI
* @author Keith Solomon <keith@keithsolomon.net>
* @license MIT License
* @version GIT: <git_id>
* @link https://git.keithsolomon.net/keith/Spacetraders
*/
/**
* Custom exception class for Spacetraders API related errors.
*
* @extends RuntimeException * @extends RuntimeException
*/ */
class SpacetradersApiException extends RuntimeException { class SpacetradersApiException extends RuntimeException {

View File

@@ -7,11 +7,12 @@
* It includes methods for authentication, making API requests, and handling responses, * It includes methods for authentication, making API requests, and handling responses,
* making it easier to integrate Spacetraders into your applications or scripts. * making it easier to integrate Spacetraders into your applications or scripts.
* *
* @package SpacetradersAPI * @category API
* @author Keith Solomon <keith@keithsolomon.net> * @package SpacetradersAPI
* @license MIT License * @author Keith Solomon <keith@keithsolomon.net>
* @version GIT: <git_id> * @license MIT License
* @link https://spacetraders.io * @version GIT: <git_id>
* @link https://git.keithsolomon.net/keith/Spacetraders
*/ */
require_once __DIR__ . '/spacetraders-api-exception.php'; require_once __DIR__ . '/spacetraders-api-exception.php';

View File

@@ -1,18 +1,19 @@
<?php <?php
/** /**
* Spacetraders SQLite storage for configuration and API cache. * Spacetraders API Client Library - Storage
* *
* @category Storage * SQLite storage for configuration and API cache.
* @package SpacetradersAPI *
* @author Keith Solomon <keith@keithsolomon.net> * @category Storage
* @license MIT License * @package SpacetradersAPI
* @link https://git.keithsolomon.net/keith/Spacetraders * @author Keith Solomon <keith@keithsolomon.net>
* @license MIT License
* @version GIT: <git_id>
* @link https://git.keithsolomon.net/keith/Spacetraders
*/ */
/** /**
* Spacetraders SQLite storage for configuration and API cache. * Storage for configuration and API cache.
*
* @package SpacetradersAPI
*/ */
class SpacetradersStorage { class SpacetradersStorage {
/** /**
@@ -45,7 +46,7 @@ class SpacetradersStorage {
* *
* @return void * @return void
*/ */
private function _initializeSchema(): void { private function _initializeSchema(): void { // phpcs:ignore
$this->db->exec( $this->db->exec(
'CREATE TABLE IF NOT EXISTS settings ( 'CREATE TABLE IF NOT EXISTS settings (
setting_key TEXT PRIMARY KEY, setting_key TEXT PRIMARY KEY,

View File

@@ -18,7 +18,7 @@ require_once __DIR__ . '/lib/spacetraders-storage.php';
* *
* @return array<int,array<string,mixed>> * @return array<int,array<string,mixed>>
*/ */
function spacetraders_filter_mining_ships( array $ships ): array { function filterMiningShips( array $ships ): array {
return array_values( return array_values(
array_filter( array_filter(
$ships, $ships,
@@ -76,7 +76,7 @@ try {
$agent = $agentResponse['data'] ?? $agentResponse; $agent = $agentResponse['data'] ?? $agentResponse;
$ships = $shipsResponse['data'] ?? $shipsResponse; $ships = $shipsResponse['data'] ?? $shipsResponse;
$contracts = $contractsResponse['data'] ?? $contractsResponse; $contracts = $contractsResponse['data'] ?? $contractsResponse;
$miningShips = spacetraders_filter_mining_ships( $ships ); $miningShips = filterMiningShips( $ships );
$activeContracts = array_values( $activeContracts = array_values(
array_filter( array_filter(
(array) $contracts, (array) $contracts,
@@ -291,7 +291,7 @@ try {
$contractsResponse = $client->listMyContracts(); $contractsResponse = $client->listMyContracts();
$ships = $shipsResponse['data'] ?? $shipsResponse; $ships = $shipsResponse['data'] ?? $shipsResponse;
$contracts = $contractsResponse['data'] ?? $contractsResponse; $contracts = $contractsResponse['data'] ?? $contractsResponse;
$miningShips = spacetraders_filter_mining_ships( $ships ); $miningShips = filterMiningShips( $ships );
$activeContracts = array_values( $activeContracts = array_values(
array_filter( array_filter(
(array) $contracts, (array) $contracts,
@@ -302,12 +302,7 @@ try {
); );
} }
if ( if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['ship_action'] ) && in_array( (string) $_POST['ship_action'], array( 'sell_ship_cargo', 'jettison_ship_cargo' ), true ) && isset( $_POST['ship_symbol'] )) {
$_SERVER['REQUEST_METHOD'] === 'POST' &&
isset( $_POST['ship_action'] ) &&
in_array( (string) $_POST['ship_action'], array( 'sell_ship_cargo', 'jettison_ship_cargo' ), true ) &&
isset( $_POST['ship_symbol'] )
) {
$shipAction = (string) $_POST['ship_action']; $shipAction = (string) $_POST['ship_action'];
$sellShipSymbol = trim( (string) $_POST['ship_symbol'] ); $sellShipSymbol = trim( (string) $_POST['ship_symbol'] );
@@ -355,7 +350,7 @@ try {
$shipsResponse = $client->listMyShips(); $shipsResponse = $client->listMyShips();
$ships = $shipsResponse['data'] ?? $shipsResponse; $ships = $shipsResponse['data'] ?? $shipsResponse;
$miningShips = spacetraders_filter_mining_ships( $ships ); $miningShips = filterMiningShips( $ships );
} catch (SpacetradersApiException $e) { } catch (SpacetradersApiException $e) {
$errorMessage = $sellShipSymbol . ': ' . $e->getMessage(); $errorMessage = $sellShipSymbol . ': ' . $e->getMessage();
} }

View File

@@ -57,129 +57,129 @@ try {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['ship_action'] ) ) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset( $_POST['ship_action'] ) ) {
$action = (string) $_POST['ship_action']; $action = (string) $_POST['ship_action'];
switch ($action) { switch ($action) {
case 'orbit': case 'orbit':
$client->orbitShip( $shipSymbol ); $client->orbitShip( $shipSymbol );
$statusMessage = 'Ship set to orbit.'; $statusMessage = 'Ship set to orbit.';
break; break;
case 'dock': case 'dock':
$client->dockShip( $shipSymbol );
$statusMessage = 'Ship docked.';
break;
case 'extract':
$client->extractResources( $shipSymbol );
$statusMessage = 'Resource extraction started.';
break;
case 'survey':
$client->surveyWaypoint( $shipSymbol );
$statusMessage = 'Survey completed.';
break;
case 'siphon':
$client->siphonResources( $shipSymbol );
$statusMessage = 'Siphon action completed.';
break;
case 'refuel':
$client->refuelShip( $shipSymbol );
$statusMessage = 'Ship refueled.';
break;
case 'sell_all_cargo':
$shipResponse = $client->getShip( $shipSymbol );
$shipData = $shipResponse['data'] ?? array();
$shipStatus = (string) ( $shipData['nav']['status'] ?? '' );
if ($shipStatus === 'IN_TRANSIT' ) {
throw new SpacetradersApiException( 'Ship is in transit and cannot sell cargo right now.' );
}
if ($shipStatus !== 'DOCKED' ) {
$client->dockShip( $shipSymbol ); $client->dockShip( $shipSymbol );
$statusMessage = 'Ship docked.'; }
break;
case 'extract':
$client->extractResources( $shipSymbol );
$statusMessage = 'Resource extraction started.';
break;
case 'survey':
$client->surveyWaypoint( $shipSymbol );
$statusMessage = 'Survey completed.';
break;
case 'siphon':
$client->siphonResources( $shipSymbol );
$statusMessage = 'Siphon action completed.';
break;
case 'refuel':
$client->refuelShip( $shipSymbol );
$statusMessage = 'Ship refueled.';
break;
case 'sell_all_cargo':
$shipResponse = $client->getShip( $shipSymbol );
$shipData = $shipResponse['data'] ?? array();
$shipStatus = (string) ( $shipData['nav']['status'] ?? '' );
if ($shipStatus === 'IN_TRANSIT' ) {
throw new SpacetradersApiException( 'Ship is in transit and cannot sell cargo right now.' );
}
if ($shipStatus !== 'DOCKED' ) {
$client->dockShip( $shipSymbol );
}
$inventory = (array) ( $shipData['cargo']['inventory'] ?? array() );
$soldItems = 0;
foreach ( $inventory as $item ) {
$tradeSymbol = (string) ( $item['symbol'] ?? '' );
$units = (int) ( $item['units'] ?? 0 );
if ($tradeSymbol === '' || $units <= 0 ) {
continue;
}
try {
$client->sellCargo( $shipSymbol, $tradeSymbol, $units );
$soldItems++;
} catch (SpacetradersApiException $e) {
// Continue selling other items.
}
}
$statusMessage = 'Attempted to sell all cargo item types. Sold: ' . $soldItems . '.';
break;
case 'sell_cargo_item':
$tradeSymbol = trim( (string) ( $_POST['trade_symbol'] ?? '' ) );
$units = (int) ( $_POST['units'] ?? 0 );
$inventory = (array) ( $shipData['cargo']['inventory'] ?? array() );
$soldItems = 0;
foreach ( $inventory as $item ) {
$tradeSymbol = (string) ( $item['symbol'] ?? '' );
$units = (int) ( $item['units'] ?? 0 );
if ($tradeSymbol === '' || $units <= 0 ) { if ($tradeSymbol === '' || $units <= 0 ) {
throw new SpacetradersApiException( 'Trade symbol and units are required to sell cargo.' ); continue;
} }
$shipResponse = $client->getShip( $shipSymbol ); try {
$shipData = $shipResponse['data'] ?? array(); $client->sellCargo( $shipSymbol, $tradeSymbol, $units );
$shipStatus = (string) ( $shipData['nav']['status'] ?? '' ); $soldItems++;
} catch (SpacetradersApiException $e) {
if ($shipStatus === 'IN_TRANSIT' ) { // Continue selling other items.
throw new SpacetradersApiException( 'Ship is in transit and cannot sell cargo right now.' );
} }
}
if ($shipStatus !== 'DOCKED' ) { $statusMessage = 'Attempted to sell all cargo item types. Sold: ' . $soldItems . '.';
$client->dockShip( $shipSymbol ); break;
} case 'sell_cargo_item':
$tradeSymbol = trim( (string) ( $_POST['trade_symbol'] ?? '' ) );
$units = (int) ( $_POST['units'] ?? 0 );
$client->sellCargo( $shipSymbol, $tradeSymbol, $units ); if ($tradeSymbol === '' || $units <= 0 ) {
$statusMessage = 'Sold ' . number_format( $units ) . ' units of ' . $tradeSymbol . '.'; throw new SpacetradersApiException( 'Trade symbol and units are required to sell cargo.' );
break; }
case 'jettison_all_cargo':
$shipResponse = $client->getShip( $shipSymbol );
$shipData = $shipResponse['data'] ?? array();
$shipStatus = (string) ( $shipData['nav']['status'] ?? '' );
if ($shipStatus === 'IN_TRANSIT' ) {
throw new SpacetradersApiException( 'Ship is in transit and cannot jettison cargo right now.' );
}
$inventory = (array) ( $shipData['cargo']['inventory'] ?? array() ); $shipResponse = $client->getShip( $shipSymbol );
$jettisonedItems = 0; $shipData = $shipResponse['data'] ?? array();
foreach ( $inventory as $item ) { $shipStatus = (string) ( $shipData['nav']['status'] ?? '' );
$tradeSymbol = (string) ( $item['symbol'] ?? '' );
$units = (int) ( $item['units'] ?? 0 );
if ($tradeSymbol === '' || $units <= 0 ) {
continue;
}
try { if ($shipStatus === 'IN_TRANSIT' ) {
$client->jettisonCargo( $shipSymbol, $tradeSymbol, $units ); throw new SpacetradersApiException( 'Ship is in transit and cannot sell cargo right now.' );
$jettisonedItems++; }
} catch (SpacetradersApiException $e) {
// Continue attempting the rest.
}
}
$statusMessage = 'Attempted to jettison all cargo item types. Jettisoned: ' . $jettisonedItems . '.'; if ($shipStatus !== 'DOCKED' ) {
break; $client->dockShip( $shipSymbol );
case 'jettison_cargo_item': }
$tradeSymbol = trim( (string) ( $_POST['trade_symbol'] ?? '' ) );
$units = (int) ( $_POST['units'] ?? 0 ); $client->sellCargo( $shipSymbol, $tradeSymbol, $units );
$statusMessage = 'Sold ' . number_format( $units ) . ' units of ' . $tradeSymbol . '.';
break;
case 'jettison_all_cargo':
$shipResponse = $client->getShip( $shipSymbol );
$shipData = $shipResponse['data'] ?? array();
$shipStatus = (string) ( $shipData['nav']['status'] ?? '' );
if ($shipStatus === 'IN_TRANSIT' ) {
throw new SpacetradersApiException( 'Ship is in transit and cannot jettison cargo right now.' );
}
$inventory = (array) ( $shipData['cargo']['inventory'] ?? array() );
$jettisonedItems = 0;
foreach ( $inventory as $item ) {
$tradeSymbol = (string) ( $item['symbol'] ?? '' );
$units = (int) ( $item['units'] ?? 0 );
if ($tradeSymbol === '' || $units <= 0 ) { if ($tradeSymbol === '' || $units <= 0 ) {
throw new SpacetradersApiException( 'Trade symbol and units are required to jettison cargo.' ); continue;
} }
$shipResponse = $client->getShip( $shipSymbol ); try {
$shipData = $shipResponse['data'] ?? array(); $client->jettisonCargo( $shipSymbol, $tradeSymbol, $units );
$shipStatus = (string) ( $shipData['nav']['status'] ?? '' ); $jettisonedItems++;
if ($shipStatus === 'IN_TRANSIT' ) { } catch (SpacetradersApiException $e) {
throw new SpacetradersApiException( 'Ship is in transit and cannot jettison cargo right now.' ); // Continue attempting the rest.
} }
}
$client->jettisonCargo( $shipSymbol, $tradeSymbol, $units ); $statusMessage = 'Attempted to jettison all cargo item types. Jettisoned: ' . $jettisonedItems . '.';
$statusMessage = 'Jettisoned ' . number_format( $units ) . ' units of ' . $tradeSymbol . '.'; break;
break; case 'jettison_cargo_item':
$tradeSymbol = trim( (string) ( $_POST['trade_symbol'] ?? '' ) );
$units = (int) ( $_POST['units'] ?? 0 );
if ($tradeSymbol === '' || $units <= 0 ) {
throw new SpacetradersApiException( 'Trade symbol and units are required to jettison cargo.' );
}
$shipResponse = $client->getShip( $shipSymbol );
$shipData = $shipResponse['data'] ?? array();
$shipStatus = (string) ( $shipData['nav']['status'] ?? '' );
if ($shipStatus === 'IN_TRANSIT' ) {
throw new SpacetradersApiException( 'Ship is in transit and cannot jettison cargo right now.' );
}
$client->jettisonCargo( $shipSymbol, $tradeSymbol, $units );
$statusMessage = 'Jettisoned ' . number_format( $units ) . ' units of ' . $tradeSymbol . '.';
break;
} }
if ($statusMessage !== '' ) { if ($statusMessage !== '' ) {
@@ -284,7 +284,7 @@ try {
<button type="submit" name="ship_action" value="refuel" class="px-3 py-1 bg-amber-700 rounded hover:bg-amber-600">Refuel</button> <button type="submit" name="ship_action" value="refuel" class="px-3 py-1 bg-amber-700 rounded hover:bg-amber-600">Refuel</button>
<?php endif; ?> <?php endif; ?>
<?php if ( (int) ( $ship['cargo']['units'] ?? 0 ) > 0 ) : ?> <?php if ((int) ( $ship['cargo']['units'] ?? 0 ) > 0) : ?>
<button type="submit" name="ship_action" value="sell_all_cargo" class="px-3 py-1 bg-rose-700 rounded hover:bg-rose-600">Sell All Cargo</button> <button type="submit" name="ship_action" value="sell_all_cargo" class="px-3 py-1 bg-rose-700 rounded hover:bg-rose-600">Sell All Cargo</button>
<button type="submit" name="ship_action" value="jettison_all_cargo" class="px-3 py-1 bg-red-800 rounded hover:bg-red-700">Jettison All Cargo</button> <button type="submit" name="ship_action" value="jettison_all_cargo" class="px-3 py-1 bg-red-800 rounded hover:bg-red-700">Jettison All Cargo</button>
<?php endif; ?> <?php endif; ?>