Various code cleanup.

This commit is contained in:
James Cole
2018-07-27 05:03:37 +02:00
parent 0312ba8ad7
commit e3e0e12fef
43 changed files with 167 additions and 145 deletions

View File

@@ -58,7 +58,7 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
$config = $this->repository->getConfiguration($this->importJob);
$mapping = $config['mapping'] ?? [];
$complete = \count($mapping) > 0;
if ($complete === true) {
if (true === $complete) {
// move job to correct stage to download transactions
$this->repository->setStage($this->importJob, 'go-for-import');
}
@@ -79,12 +79,12 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
$config = $this->repository->getConfiguration($this->importJob);
$accounts = $config['accounts'] ?? [];
$mapping = $data['account_mapping'] ?? [];
$applyRules = (int)$data['apply_rules'] === 1;
$applyRules = 1 === (int)$data['apply_rules'];
$final = [];
if (\count($accounts) === 0) {
if (0 === \count($accounts)) {
throw new FireflyException('No bunq accounts found. Import cannot continue.'); // @codeCoverageIgnore
}
if (\count($mapping) === 0) {
if (0 === \count($mapping)) {
$messages = new MessageBag;
$messages->add('nomap', (string)trans('import.bunq_no_mapping'));
@@ -116,7 +116,7 @@ class ChooseAccountsHandler implements BunqJobConfigurationInterface
{
$config = $this->repository->getConfiguration($this->importJob);
$accounts = $config['accounts'] ?? [];
if (\count($accounts) === 0) {
if (0 === \count($accounts)) {
throw new FireflyException('No bunq accounts found. Import cannot continue.'); // @codeCoverageIgnore
}
// list the users accounts:

View File

@@ -183,7 +183,7 @@ class ConfigureMappingHandler implements FileConfigurationInterface
{
$canBeMapped = config('csv.import_roles.' . $name . '.mappable');
return $canBeMapped === true && $requested === true;
return true === $canBeMapped && true === $requested;
}
/**
@@ -247,7 +247,7 @@ class ConfigureMappingHandler implements FileConfigurationInterface
$collection = $this->repository->getAttachments($this->importJob);
/** @var Attachment $attachment */
foreach ($collection as $attachment) {
if ($attachment->filename === 'import_file') {
if ('import_file' === $attachment->filename) {
$content = $this->attachments->getAttachmentContent($attachment);
break;
}
@@ -274,7 +274,7 @@ class ConfigureMappingHandler implements FileConfigurationInterface
*/
public function getValuesForMapping(Reader $reader, array $config, array $columnConfig): array
{
$offset = isset($config['has-headers']) && $config['has-headers'] === true ? 1 : 0;
$offset = isset($config['has-headers']) && true === $config['has-headers'] ? 1 : 0;
try {
$stmt = (new Statement)->offset($offset);
// @codeCoverageIgnoreStart
@@ -295,7 +295,7 @@ class ConfigureMappingHandler implements FileConfigurationInterface
continue;
}
$value = trim($line[$columnIndex]);
if (\strlen($value) === 0) {
if ('' === $value) {
// value is empty, ignore it.
continue;
}
@@ -309,7 +309,7 @@ class ConfigureMappingHandler implements FileConfigurationInterface
$columnConfig[$columnIndex]['values'] = array_unique($columnConfig[$columnIndex]['values']);
asort($columnConfig[$columnIndex]['values']);
// if the count of this array is zero, there is nothing to map.
if (\count($columnConfig[$columnIndex]['values']) === 0) {
if (0 === \count($columnConfig[$columnIndex]['values'])) {
unset($columnConfig[$columnIndex]);
}
}

View File

@@ -76,10 +76,10 @@ class ConfigureRolesHandler implements FileConfigurationInterface
if (\in_array($role, ['amount', 'amount_credit', 'amount_debit'])) {
$hasAmount = true;
}
if ($role === 'foreign-currency-code') {
if ('foreign-currency-code' === $role) {
$hasForeignCode = true;
}
if ($role === 'amount_foreign') {
if ('amount_foreign' === $role) {
$hasForeignAmount = true;
}
}
@@ -122,7 +122,7 @@ class ConfigureRolesHandler implements FileConfigurationInterface
$count = $config['column-count'];
for ($i = 0; $i < $count; ++$i) {
$role = $data['role'][$i] ?? '_ignore';
$mapping = (isset($data['map'][$i]) && $data['map'][$i] === '1');
$mapping = (isset($data['map'][$i]) && '1' === $data['map'][$i]);
$config['column-roles'][$i] = $role;
$config['column-do-mapping'][$i] = $mapping;
Log::debug(sprintf('Column %d has been given role %s (mapping: %s)', $i, $role, var_export($mapping, true)));
@@ -130,7 +130,7 @@ class ConfigureRolesHandler implements FileConfigurationInterface
$config = $this->ignoreUnmappableColumns($config);
$messages = $this->configurationComplete($config);
if ($messages->count() === 0) {
if (0 === $messages->count()) {
$this->repository->setStage($this->importJob, 'ready_to_run');
if ($this->isMappingNecessary($config)) {
$this->repository->setStage($this->importJob, 'map');
@@ -175,7 +175,7 @@ class ConfigureRolesHandler implements FileConfigurationInterface
public function getExamplesFromFile(Reader $reader, array $config): void
{
$limit = (int)config('csv.example_rows', 5);
$offset = isset($config['has-headers']) && $config['has-headers'] === true ? 1 : 0;
$offset = isset($config['has-headers']) && true === $config['has-headers'] ? 1 : 0;
// make statement.
try {
@@ -214,7 +214,7 @@ class ConfigureRolesHandler implements FileConfigurationInterface
public function getHeaders(Reader $reader, array $config): array
{
$headers = [];
if (isset($config['has-headers']) && $config['has-headers'] === true) {
if (isset($config['has-headers']) && true === $config['has-headers']) {
try {
$stmt = (new Statement)->limit(1)->offset(0);
$records = $stmt->process($reader);
@@ -273,7 +273,7 @@ class ConfigureRolesHandler implements FileConfigurationInterface
$collection = $this->repository->getAttachments($this->importJob);
/** @var Attachment $attachment */
foreach ($collection as $attachment) {
if ($attachment->filename === 'import_file') {
if ('import_file' === $attachment->filename) {
$content = $this->attachments->getAttachmentContent($attachment);
break;
}

View File

@@ -57,10 +57,10 @@ class ConfigureUploadHandler implements FileConfigurationInterface
// collect values:
$importId = isset($data['csv_import_account']) ? (int)$data['csv_import_account'] : 0;
$delimiter = (string)$data['csv_delimiter'];
$config['has-headers'] = (int)($data['has_headers'] ?? 0.0) === 1;
$config['has-headers'] = 1 === (int)($data['has_headers'] ?? 0.0);
$config['date-format'] = (string)$data['date_format'];
$config['delimiter'] = 'tab' === $delimiter ? "\t" : $delimiter;
$config['apply-rules'] = (int)($data['apply_rules'] ?? 0.0) === 1;
$config['apply-rules'] = 1 === (int)($data['apply_rules'] ?? 0.0);
$config['specifics'] = $this->getSpecifics($data);
// validate values:
$account = $this->accountRepos->findNull($importId);

View File

@@ -123,7 +123,7 @@ class NewFileJobHandler implements FileConfigurationInterface
/** @var Attachment $attachment */
foreach ($attachments as $attachment) {
// if file is configuration file, store it into the job.
if ($attachment->filename === 'configuration_file') {
if ('configuration_file' === $attachment->filename) {
$this->storeConfig($attachment);
}
}
@@ -162,7 +162,7 @@ class NewFileJobHandler implements FileConfigurationInterface
}
// if file is configuration file, store it into the job.
if ($attachment->filename === 'configuration_file') {
if ('configuration_file' === $attachment->filename) {
$this->storeConfig($attachment);
}
}
@@ -179,10 +179,10 @@ class NewFileJobHandler implements FileConfigurationInterface
{
$content = $this->attachments->getAttachmentContent($attachment);
$result = mb_detect_encoding($content, 'UTF-8', true);
if ($result === false) {
if (false === $result) {
return false;
}
if ($result !== 'ASCII' && $result !== 'UTF-8') {
if ('ASCII' !== $result && 'UTF-8' !== $result) {
return false; // @codeCoverageIgnore
}
@@ -194,7 +194,6 @@ class NewFileJobHandler implements FileConfigurationInterface
*
* @param Attachment $attachment
*
* @throws FireflyException
*/
private function storeConfig(Attachment $attachment): void
{

View File

@@ -98,7 +98,7 @@ class ChooseAccountsHandler implements SpectreJobConfigurationInterface
$config['account_mapping'] = $final;
$config['apply-rules'] = $applyRules;
$this->repository->setConfiguration($this->importJob, $config);
if ($final === [0 => 0] || \count($final) === 0) {
if ($final === [0 => 0] || 0 === \count($final)) {
$messages->add('count', (string)trans('import.spectre_no_mapping'));
}
@@ -116,7 +116,7 @@ class ChooseAccountsHandler implements SpectreJobConfigurationInterface
Log::debug('Now in ChooseAccountsHandler::getnextData()');
$config = $this->importJob->configuration;
$accounts = $config['accounts'] ?? [];
if (\count($accounts) === 0) {
if (0 === \count($accounts)) {
throw new FireflyException('It seems you have no accounts with this bank. The import cannot continue.'); // @codeCoverageIgnore
}
$converted = [];
@@ -128,15 +128,15 @@ class ChooseAccountsHandler implements SpectreJobConfigurationInterface
$login = null;
$logins = $config['all-logins'] ?? [];
$selected = $config['selected-login'] ?? 0;
if (\count($logins) === 0) {
if (0 === \count($logins)) {
throw new FireflyException('It seems you have no configured logins in this import job. The import cannot continue.'); // @codeCoverageIgnore
}
Log::debug(sprintf('Selected login to use is %d', $selected));
if ($selected === 0) {
if (0 === $selected) {
$login = new Login($logins[0]);
Log::debug(sprintf('Will use login %d (%s %s)', $login->getId(), $login->getProviderName(), $login->getCountryCode()));
}
if ($selected !== 0) {
if (0 !== $selected) {
foreach ($logins as $loginArray) {
$loginId = $loginArray['id'] ?? -1;
if ($loginId === $selected) {

View File

@@ -82,7 +82,7 @@ class ChooseLoginHandler implements SpectreJobConfigurationInterface
Log::debug(sprintf('The selected login by the user is #%d', $selectedLogin));
// if selected login is zero, create a new one.
if ($selectedLogin === 0) {
if (0 === $selectedLogin) {
Log::debug('Login is zero, get Spectre customer + token and store it in config.');
$customer = $this->getCustomer($this->importJob);
// get a token for the user and redirect to next stage

View File

@@ -226,7 +226,6 @@ class ImportTransaction
* Calculate the amount of this transaction.
*
* @return string
* @throws FireflyException
*/
public function calculateAmount(): string
{
@@ -258,7 +257,7 @@ class ImportTransaction
if ($conversion === -1) {
$result = app('steam')->negative($result);
}
if ($conversion === 1) {
if (1 === $conversion) {
$result = app('steam')->positive($result);
}
Log::debug(sprintf('convertedAmount after conversion is %s', $result));
@@ -301,7 +300,7 @@ class ImportTransaction
if ($conversion === -1) {
$result = app('steam')->negative($result);
}
if ($conversion === 1) {
if (1 === $conversion) {
$result = app('steam')->positive($result);
}
Log::debug(sprintf('Foreign amount after conversion is %s', $result));

View File

@@ -80,7 +80,7 @@ class StageImportDataHandler
$bunqAccountId = $bunqAccount['id'] ?? 0;
$localId = $mapping[$bunqAccountId] ?? 0;
Log::debug(sprintf('Looping accounts, now at bunq account #%d and local account #%d', $bunqAccountId, $localId));
if ($localId !== 0 && $bunqAccountId !== 0) {
if (0 !== $localId && 0 !== $bunqAccountId) {
$localAccount = $this->getLocalAccount((int)$localId);
$collection[] = $this->getTransactionsFromBunq($bunqAccountId, $localAccount);
}
@@ -122,7 +122,7 @@ class StageImportDataHandler
Log::debug(sprintf('Amount is %s %s', $amount->getCurrency(), $amount->getValue()));
$expected = AccountType::EXPENSE;
if (bccomp($amount->getValue(), '0') === 1) {
if (1 === bccomp($amount->getValue(), '0')) {
// amount + means that its a deposit.
$expected = AccountType::REVENUE;
$type = TransactionType::DEPOSIT;
@@ -131,7 +131,7 @@ class StageImportDataHandler
$destination = $this->convertToAccount($counterParty, $expected);
// switch source and destination if necessary.
if (bccomp($amount->getValue(), '0') === 1) {
if (1 === bccomp($amount->getValue(), '0')) {
Log::debug('Will make it a deposit.');
[$source, $destination] = [$destination, $source];
}
@@ -187,11 +187,12 @@ class StageImportDataHandler
* @param string $expectedType
*
* @return LocalAccount
* @throws FireflyException
*/
private function convertToAccount(LabelMonetaryAccount $party, string $expectedType): LocalAccount
{
Log::debug('in convertToAccount()');
if ($party->getIban() !== null) {
if (null !== $party->getIban()) {
// find opposing party by IBAN first.
$result = $this->accountRepository->findByIbanNull($party->getIban(), [$expectedType]);
if (null !== $result) {

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Support\Import\Routine\Fake;
use FireflyIII\Exceptions\FireflyException;
use Log;
/**
@@ -33,7 +32,6 @@ use Log;
class StageAhoyHandler
{
/**
* @throws FireflyException
*/
public function run(): void
{

View File

@@ -23,7 +23,6 @@ declare(strict_types=1);
namespace FireflyIII\Support\Import\Routine\Fake;
use FireflyIII\Exceptions\FireflyException;
use Log;
/**
@@ -33,7 +32,6 @@ use Log;
class StageNewHandler
{
/**
* @throws FireflyException
*/
public function run(): void
{

View File

@@ -205,7 +205,7 @@ class ImportableConverter
$transactionType = $this->getTransactionType($source->accountType->type, $destination->accountType->type);
$currency = $currency ?? $this->getCurrency($source, $destination);
if ($transactionType === 'unknown') {
if ('unknown' === $transactionType) {
$message = sprintf(
'Cannot determine transaction type. Source account is a %s, destination is a %s', $source->accountType->type, $destination->accountType->type
);
@@ -282,14 +282,14 @@ class ImportableConverter
if ($destination->accountType->type === AccountType::ASSET) {
// destination is asset, might have currency preference:
$destinationCurrencyId = (int)$this->accountRepository->getMetaValue($destination, 'currency_id');
$currency = $destinationCurrencyId === 0 ? $this->defaultCurrency : $this->currencyMapper->map($destinationCurrencyId, []);
$currency = 0 === $destinationCurrencyId ? $this->defaultCurrency : $this->currencyMapper->map($destinationCurrencyId, []);
Log::debug(sprintf('Destination is an asset account, and has currency preference %s', $currency->code));
}
if ($source->accountType->type === AccountType::ASSET) {
// source is asset, might have currency preference:
$sourceCurrencyId = (int)$this->accountRepository->getMetaValue($source, 'currency_id');
$currency = $sourceCurrencyId === 0 ? $this->defaultCurrency : $this->currencyMapper->map($sourceCurrencyId, []);
$currency = 0 === $sourceCurrencyId ? $this->defaultCurrency : $this->currencyMapper->map($sourceCurrencyId, []);
Log::debug(sprintf('Source is an asset account, and has currency preference %s', $currency->code));
}
if (null === $currency) {

View File

@@ -123,7 +123,7 @@ class LineReader
/** @var array $config */
$config = $this->importJob->configuration;
Log::debug('now in getLines()');
$offset = isset($config['has-headers']) && $config['has-headers'] === true ? 1 : 0;
$offset = isset($config['has-headers']) && true === $config['has-headers'] ? 1 : 0;
try {
$stmt = (new Statement)->offset($offset);
// @codeCoverageIgnoreStart
@@ -151,7 +151,7 @@ class LineReader
$collection = $this->repository->getAttachments($this->importJob);
/** @var Attachment $attachment */
foreach ($collection as $attachment) {
if ($attachment->filename === 'import_file') {
if ('import_file' === $attachment->filename) {
$content = $this->attachments->getAttachmentContent($attachment);
break;
}

View File

@@ -195,7 +195,7 @@ class MappingConverger
$value = trim($value);
$originalRole = $this->roles[$columnIndex] ?? '_ignore';
Log::debug(sprintf('Now at column #%d (%s), value "%s"', $columnIndex, $originalRole, $value));
if ($originalRole !== '_ignore' && \strlen($value) > 0) {
if ('_ignore' !== $originalRole && \strlen($value) > 0) {
// is a mapped value present?
$mapped = $this->mapping[$columnIndex][$value] ?? 0;

View File

@@ -53,7 +53,7 @@ class OpposingAccountMapper
$expectedType = AccountType::EXPENSE;
$result = null;
Log::debug(sprintf('Going to search for accounts of type %s', $expectedType));
if (bccomp($amount, '0') === 1) {
if (1 === bccomp($amount, '0')) {
// more than zero.
$expectedType = AccountType::REVENUE;
Log::debug(sprintf('Because amount is %s, will instead search for accounts of type %s', $amount, $expectedType));

View File

@@ -57,7 +57,7 @@ class StageAuthenticatedHandler
$config = $this->importJob->configuration;
$logins = $config['all-logins'] ?? [];
Log::debug(sprintf('%d logins in config', \count($logins)));
if (\count($logins) === 0) {
if (0 === \count($logins)) {
// get logins from Spectre.
$logins = $this->getLogins();
$config['all-logins'] = $logins;

View File

@@ -60,7 +60,7 @@ class StageImportDataHandler
$config = $this->importJob->configuration;
$accounts = $config['accounts'] ?? [];
Log::debug(sprintf('Count of accounts in array is %d', \count($accounts)));
if (\count($accounts) === 0) {
if (0 === \count($accounts)) {
throw new FireflyException('There are no accounts in this import job. Cannot continue.'); // @codeCoverageIgnore
}
$toImport = $config['account_mapping'] ?? [];
@@ -124,14 +124,14 @@ class StageImportDataHandler
$amount = $transaction->getAmount();
$source = $originalSource;
$destination = $this->mapper->map(null, $amount, $destinationData);
$notes = (string)trans('import.imported_from_account', ['account' => $spectreAccount->getName()]) . ' ' . "\n";
$notes = trans('import.imported_from_account', ['account' => $spectreAccount->getName()]) . ' ' . "\n";
$foreignAmount = null;
$foreignCurrencyCode = null;
$currencyCode = $transaction->getCurrencyCode();
$type = 'withdrawal';
// switch source and destination if amount is greater than zero.
if (bccomp($amount, '0') === 1) {
if (1 === bccomp($amount, '0')) {
[$source, $destination] = [$destination, $source];
$type = 'deposit';
}