Browse Source

Catch various phpstan things.

pull/840/head
James Cole 4 months ago
parent
commit
4c35391b4c
  1. 9
      .ci/phpstan.neon
  2. 2
      app/Http/Controllers/AutoUploadController.php
  3. 16
      app/Http/Controllers/Controller.php
  4. 57
      app/Http/Controllers/DebugController.php
  5. 13
      app/Http/Controllers/Import/ConfigurationController.php
  6. 7
      config/importer.php

9
.ci/phpstan.neon

@ -4,7 +4,12 @@ parameters:
enabled: false
final:
enabled: false
noParameterWithNullDefaultValue:
enabled: false
noCompact:
enabled: false
scanFiles:
- ../.phpstorm.meta.php
- ../_ide_helper.php
paths:
- ../app
@ -32,6 +37,10 @@ parameters:
- '#expects view-string\|null, string given#'
# phpstan can't handle this so we ignore them.
- '#Call to an undefined method Illuminate\\Http\\Request::get#'
- '#Call to an undefined method Illuminate\\Http\\Request::header#'
- '#Call to an undefined method Illuminate\\Http\\Request::cookie#'
- '#Call to an undefined method App\\Http\\Request\\AutoUploadRequest::#'
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::before#'
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::after#'
- '#Call to an undefined method Illuminate\\Database\\Eloquent\\Relations\\HasMany::withTrashed#'

2
app/Http/Controllers/AutoUploadController.php

@ -40,7 +40,7 @@ class AutoUploadController extends Controller
/**
* @throws ImporterErrorException
*/
public function index(AutoUploadRequest $request)
public function index(AutoUploadRequest $request): string
{
if (false === config('importer.can_post_files')) {
throw new ImporterErrorException('Disabled, not allowed to import.');

16
app/Http/Controllers/Controller.php

@ -48,10 +48,18 @@ class Controller extends BaseController
public function __construct()
{
// validate some env vars (skip over config)
$accessToken = (string) env('FIREFLY_III_ACCESS_TOKEN', '');
$clientId = (string) env('FIREFLY_III_CLIENT_ID', '');
$baseUrl = (string) env('FIREFLY_III_URL', '');
$vanityUrl = (string) env('VANITY_URL', '');
// $accessToken = (string) env('FIREFLY_III_ACCESS_TOKEN', '');
// $clientId = (string) env('FIREFLY_III_CLIENT_ID', '');
// $baseUrl = (string) env('FIREFLY_III_URL', '');
// $vanityUrl = (string) env('VANITY_URL', '');
// experimental. Use config instead
$accessToken = (string) config('importer.access_token','');
$clientId = (string) config('importer.client_id', '');
$baseUrl = (string) config('importer.url', '');
$vanityUrl = (string) config('importer.vanity_url', '');
// access token AND client ID cannot be set together
if ('' !== $accessToken && '' !== $clientId) {

57
app/Http/Controllers/DebugController.php

@ -28,8 +28,10 @@ namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
class DebugController extends Controller
{
@ -40,9 +42,10 @@ class DebugController extends Controller
*/
public function index(Request $request)
{
$now = Carbon::now()->format('Y-m-d H:i:s e');
$table = $this->getTable();
$logger = app('log')->driver();
$now = Carbon::now()->format('Y-m-d H:i:s e');
$table = $this->getTable();
/** @var Logger $logger */
$logger = Log::driver();
$handlers = $logger->getHandlers();
$logContent = '';
foreach ($handlers as $handler) {
@ -50,8 +53,8 @@ class DebugController extends Controller
$logFile = $handler->getUrl();
if (null !== $logFile) {
try {
$logContent = file_get_contents($logFile);
} catch (\Exception $e) { // @phpstan-ignore-line
$logContent = (string)file_get_contents($logFile);
} catch (\Exception $e) {
// @ignoreException
}
}
@ -59,20 +62,20 @@ class DebugController extends Controller
}
if ('' !== $logContent) {
// last few lines
$logContent = 'Truncated from this point <----|'.substr($logContent, -8192);
$logContent = 'Truncated from this point <----|' . substr($logContent, -8192);
}
if (true === config('importer.is_external')) {
$logContent = 'No logs, external installation.';
}
app('log')->emergency('I am a EMERGENCY message.');
app('log')->alert('I am a ALERT message.');
app('log')->critical('I am a CRITICAL message.');
app('log')->error('I am a ERROR message.');
app('log')->warning('I am a WARNING message.');
app('log')->notice('I am a NOTICE message.');
app('log')->info('I am a INFO message.');
app('log')->debug('I am a DEBUG message.');
Log::emergency('I am a EMERGENCY message.');
Log::alert('I am a ALERT message.');
Log::critical('I am a CRITICAL message.');
Log::error('I am a ERROR message.');
Log::warning('I am a WARNING message.');
Log::notice('I am a NOTICE message.');
Log::info('I am a INFO message.');
Log::debug('I am a DEBUG message.');
return view(
'debug',
@ -98,23 +101,23 @@ class DebugController extends Controller
{
$build = null;
$baseBuild = null;
$isDocker = env('IS_DOCKER', false);
$isDocker = config('importer.docker.is_docker', false);
if (true === $isDocker) {
try {
if (file_exists('/var/www/counter-main.txt')) {
$build = trim(file_get_contents('/var/www/counter-main.txt'));
$build = trim((string) file_get_contents('/var/www/counter-main.txt'));
}
} catch (\Exception $e) {
app('log')->debug('Could not check build counter, but that\'s ok.');
app('log')->warning($e->getMessage());
Log::debug('Could not check build counter, but that\'s ok.');
Log::warning($e->getMessage());
}
if ('' !== (string) env('BASE_IMAGE_BUILD')) {
$baseBuild = env('BASE_IMAGE_BUILD');
if ('' !== (string)config('importer.docker.base_build')) {
$baseBuild = (string)config('importer.docker.base_build');
}
}
$search = ['~', '#'];
$replace = ['\~', '# '];
$search = ['~', '#'];
$replace = ['\~', '# '];
return [
'is_docker' => $isDocker,
@ -131,7 +134,7 @@ class DebugController extends Controller
return [
'debug' => var_export(config('app.debug'), true),
'display_errors' => ini_get('display_errors'),
'reporting' => $this->errorReporting((int) ini_get('error_reporting')),
'reporting' => $this->errorReporting((int)ini_get('error_reporting')),
'bcscale' => bcscale(),
];
}
@ -143,15 +146,15 @@ class DebugController extends Controller
{
$array = [
-1 => 'ALL errors',
E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED => 'E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED',
E_ALL & ~E_NOTICE & ~E_DEPRECATED => 'E_ALL & ~E_NOTICE & ~E_DEPRECATED',
E_ALL => 'E_ALL',
E_ALL & ~E_DEPRECATED & ~E_STRICT => 'E_ALL & ~E_DEPRECATED & ~E_STRICT',
E_ALL & ~E_DEPRECATED => 'E_ALL & ~E_DEPRECATED',
E_ALL & ~E_NOTICE => 'E_ALL & ~E_NOTICE',
E_ALL & ~E_NOTICE & ~E_STRICT => 'E_ALL & ~E_NOTICE & ~E_STRICT',
E_ALL & ~E_NOTICE => 'E_ALL & ~E_NOTICE',
E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR => 'E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR',
];
return $array[$value] ?? (string) $value;
return $array[$value] ?? sprintf('flags: %s', (string)$value);
}
private function getUserInfo(): array

13
app/Http/Controllers/Import/ConfigurationController.php

@ -114,7 +114,7 @@ class ConfigurationController extends Controller
// save configuration in session and on disk:
session()->put(Constants::CONFIGURATION, $configuration->toSessionArray());
$configFileName = StorageService::storeContent(json_encode($configuration->toArray(), JSON_PRETTY_PRINT));
$configFileName = StorageService::storeContent((string)json_encode($configuration->toArray(), JSON_PRETTY_PRINT));
session()->put(Constants::UPLOAD_CONFIG_FILE, $configFileName);
// redirect to selection.
@ -160,22 +160,22 @@ class ConfigurationController extends Controller
foreach ($accountsData ?? [] as $account) {
// Ensure the account has required SimpleFIN protocol fields
if (!isset($account['id']) || '' === (string)$account['id']) {
if (!array_key_exists('id', $account) || '' === (string)$account['id']) {
Log::warning('SimpleFIN account data is missing a valid ID, skipping.', ['account_data' => $account]);
continue;
}
if (!isset($account['name'])) {
if (!array_key_exists('name', $account)) {
Log::warning('SimpleFIN account data is missing name field, adding default.', ['account_id' => $account['id']]);
$account['name'] = 'Unknown Account (ID: '.$account['id'].')';
}
if (!isset($account['currency'])) {
if (!array_key_exists('currency', $account)) {
Log::warning('SimpleFIN account data is missing currency field, this may cause issues.', ['account_id' => $account['id']]);
}
if (!isset($account['balance'])) {
if (!array_key_exists('balance', $account)) {
Log::warning('SimpleFIN account data is missing balance field, this may cause issues.', ['account_id' => $account['id']]);
}
@ -219,7 +219,7 @@ class ConfigurationController extends Controller
$return[] = ['import_account' => $importAccountRepresentation, // The DTO-like object for the component
'name' => $sfinAccountData['name'], // SimpleFIN account name
'id' => $sfinAccountData['id'], // ID for form fields (do_import[ID], accounts[ID])
'mapped_to' => $this->getMappedTo((object)['identifier' => $importAccountRepresentation->id, 'name' => $importAccountRepresentation->name], $fireflyAccounts), // getMappedTo needs 'identifier'
'mapped_to' => $this->getMappedTo(['identifier' => $importAccountRepresentation->id, 'name' => $importAccountRepresentation->name], $fireflyAccounts), // getMappedTo needs 'identifier'
'type' => 'source', // Indicates it's an account from the import source
'firefly_iii_accounts' => $fireflyAccounts, // Required by x-importer-account component
];
@ -232,6 +232,7 @@ class ConfigurationController extends Controller
/**
* Stub for determining if an imported account is mapped to a Firefly III account.
* TODO: Implement actual mapping logic.
* TODO get rid of object casting.
*
* @param object $importAccount An object representing the account from the import source.
* Expected to have at least 'identifier' and 'name' properties.

7
config/importer.php

@ -44,6 +44,13 @@ return [
'bridge_url' => env('SIMPLEFIN_BRIDGE_URL'),
'timeout' => (int) env('SIMPLEFIN_TIMEOUT', 30),
],
// docker build info.
'docker' => [
'is_docker' => env('IS_DOCKER', false),
'base_build' => env('BASE_IMAGE_BUILD','(unknown)'),
],
'fallback_in_dir' => env('FALLBACK_IN_DIR', false),
'fallback_configuration' => '_fallback.json',
'import_dir_allowlist' => explode(',', env('IMPORT_DIR_ALLOWLIST', '')),

Loading…
Cancel
Save