Reformat various code.

This commit is contained in:
James Cole
2022-03-29 15:00:29 +02:00
parent d1a09ff33b
commit d04efb8325
81 changed files with 662 additions and 662 deletions

View File

@@ -93,6 +93,14 @@ class RemoteUserGuard implements Guard
$this->user = $retrievedUser;
}
/**
* @inheritDoc
*/
public function guest(): bool
{
return !$this->check();
}
/**
* @inheritDoc
*/
@@ -104,9 +112,17 @@ class RemoteUserGuard implements Guard
/**
* @inheritDoc
*/
public function guest(): bool
public function user(): ?User
{
return !$this->check();
return $this->user;
}
/**
* @inheritDoc
*/
public function hasUser()
{
// TODO: Implement hasUser() method.
}
/**
@@ -125,14 +141,6 @@ class RemoteUserGuard implements Guard
$this->user = $user;
}
/**
* @inheritDoc
*/
public function user(): ?User
{
return $this->user;
}
/**
* @inheritDoc
*/
@@ -140,12 +148,4 @@ class RemoteUserGuard implements Guard
{
throw new FireflyException('Did not implement RemoteUserGuard::validate()');
}
/**
* @inheritDoc
*/
public function hasUser()
{
// TODO: Implement hasUser() method.
}
}

View File

@@ -30,6 +30,15 @@ use Log;
*/
trait AppendsLocationData
{
/**
* Abstract method.
*
* @param $key
*
* @return bool
*/
abstract public function has($key);
/**
* Read the submitted Request data and add new or updated Location data to the array.
*
@@ -148,25 +157,6 @@ trait AppendsLocationData
return false;
}
/**
* Abstract method stolen from "InteractsWithInput".
*
* @param null $key
* @param bool $default
*
* @return mixed
*/
abstract public function boolean($key = null, $default = false);
/**
* Abstract method.
*
* @param $key
*
* @return bool
*/
abstract public function has($key);
/**
* Abstract method.
*
@@ -183,6 +173,16 @@ trait AppendsLocationData
*/
abstract public function routeIs(...$patterns);
/**
* Abstract method stolen from "InteractsWithInput".
*
* @param null $key
* @param bool $default
*
* @return mixed
*/
abstract public function boolean($key = null, $default = false);
/**
* @param string|null $prefix
*

View File

@@ -45,18 +45,6 @@ class AmountFormat extends AbstractExtension
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
$this->formatAmountBySymbol(),
$this->formatAmountByCurrency(),
];
}
/**
* @return TwigFilter
*/
@@ -89,6 +77,18 @@ class AmountFormat extends AbstractExtension
);
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->formatAmountByAccount(),
$this->formatAmountBySymbol(),
$this->formatAmountByCurrency(),
];
}
/**
* Will format the amount by the currency related to the given account.
*

View File

@@ -53,25 +53,6 @@ class General extends AbstractExtension
];
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->phpdate(),
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialObjectType(),
$this->menuOpenRoutePartial(),
$this->formatDate(),
$this->getMetaField(),
$this->hasRole(),
$this->getRootSearchOperator(),
$this->carbonize()
];
}
/**
* Show account balance. Only used on the front page of Firefly III.
*
@@ -93,24 +74,6 @@ class General extends AbstractExtension
);
}
/**
* Show URL host name
*
* @return TwigFilter
*/
protected function phpHostName(): TwigFilter
{
return new TwigFilter(
'phphost',
static function (string $string): string {
$proto = (string)parse_url($string, PHP_URL_SCHEME);
$host = (string)parse_url($string, PHP_URL_HOST);
return e(sprintf('%s://%s', $proto, $host));
}
);
}
/**
* Used to convert 1024 to 1kb etc.
*
@@ -250,6 +213,43 @@ class General extends AbstractExtension
);
}
/**
* Show URL host name
*
* @return TwigFilter
*/
protected function phpHostName(): TwigFilter
{
return new TwigFilter(
'phphost',
static function (string $string): string {
$proto = (string) parse_url($string, PHP_URL_SCHEME);
$host = (string) parse_url($string, PHP_URL_HOST);
return e(sprintf('%s://%s', $proto, $host));
}
);
}
/**
* {@inheritdoc}
*/
public function getFunctions(): array
{
return [
$this->phpdate(),
$this->activeRouteStrict(),
$this->activeRoutePartial(),
$this->activeRoutePartialObjectType(),
$this->menuOpenRoutePartial(),
$this->formatDate(),
$this->getMetaField(),
$this->hasRole(),
$this->getRootSearchOperator(),
$this->carbonize(),
];
}
/**
* Basic example thing for some views.
*
@@ -396,19 +396,6 @@ class General extends AbstractExtension
);
}
/**
* @return TwigFunction
*/
protected function carbonize(): TwigFunction
{
return new TwigFunction(
'carbonize',
static function (string $date): Carbon {
return new Carbon($date);
}
);
}
/**
* Will return true if the user is of role X.
*
@@ -438,4 +425,17 @@ class General extends AbstractExtension
}
);
}
/**
* @return TwigFunction
*/
protected function carbonize(): TwigFunction
{
return new TwigFunction(
'carbonize',
static function (string $date): Carbon {
return new Carbon($date);
}
);
}
}

View File

@@ -86,84 +86,6 @@ class SearchRuleEngine implements RuleEngineInterface
return $collection->unique();
}
/**
* @inheritDoc
* @throws FireflyException
*/
public function fire(): void
{
$this->resultCount = [];
Log::debug('SearchRuleEngine::fire()!');
// if rules and no rule groups, file each rule separately.
if (0 !== $this->rules->count()) {
Log::debug(sprintf('SearchRuleEngine:: found %d rule(s) to fire.', $this->rules->count()));
foreach ($this->rules as $rule) {
$this->fireRule($rule);
}
Log::debug('SearchRuleEngine:: done processing all rules!');
return;
}
if (0 !== $this->groups->count()) {
Log::debug(sprintf('SearchRuleEngine:: found %d rule group(s) to fire.', $this->groups->count()));
// fire each group:
/** @var RuleGroup $group */
foreach ($this->groups as $group) {
$this->fireGroup($group);
}
}
Log::debug('SearchRuleEngine:: done processing all rules!');
}
/**
* Return the number of changed transactions from the previous "fire" action.
*
* @return int
*/
public function getResults(): int
{
return count($this->resultCount);
}
/**
* @inheritDoc
*/
public function setRuleGroups(Collection $ruleGroups): void
{
Log::debug(__METHOD__);
foreach ($ruleGroups as $group) {
if ($group instanceof RuleGroup) {
Log::debug(sprintf('Adding a rule group to the SearchRuleEngine: #%d ("%s")', $group->id, $group->title));
$this->groups->push($group);
}
}
}
/**
* @inheritDoc
*/
public function setRules(Collection $rules): void
{
Log::debug(__METHOD__);
foreach ($rules as $rule) {
if ($rule instanceof Rule) {
Log::debug(sprintf('Adding a rule to the SearchRuleEngine: #%d ("%s")', $rule->id, $rule->title));
$this->rules->push($rule);
}
}
}
/**
* @inheritDoc
*/
public function setUser(User $user): void
{
$this->user = $user;
$this->operators = [];
}
/**
* Finds the transactions a strict rule will execute on.
*
@@ -365,6 +287,36 @@ class SearchRuleEngine implements RuleEngineInterface
return $unique;
}
/**
* @inheritDoc
* @throws FireflyException
*/
public function fire(): void
{
$this->resultCount = [];
Log::debug('SearchRuleEngine::fire()!');
// if rules and no rule groups, file each rule separately.
if (0 !== $this->rules->count()) {
Log::debug(sprintf('SearchRuleEngine:: found %d rule(s) to fire.', $this->rules->count()));
foreach ($this->rules as $rule) {
$this->fireRule($rule);
}
Log::debug('SearchRuleEngine:: done processing all rules!');
return;
}
if (0 !== $this->groups->count()) {
Log::debug(sprintf('SearchRuleEngine:: found %d rule group(s) to fire.', $this->groups->count()));
// fire each group:
/** @var RuleGroup $group */
foreach ($this->groups as $group) {
$this->fireGroup($group);
}
}
Log::debug('SearchRuleEngine:: done processing all rules!');
}
/**
* Returns true if the rule has been triggered.
*
@@ -553,4 +505,52 @@ class SearchRuleEngine implements RuleEngineInterface
return $all;
}
/**
* Return the number of changed transactions from the previous "fire" action.
*
* @return int
*/
public function getResults(): int
{
return count($this->resultCount);
}
/**
* @inheritDoc
*/
public function setRuleGroups(Collection $ruleGroups): void
{
Log::debug(__METHOD__);
foreach ($ruleGroups as $group) {
if ($group instanceof RuleGroup) {
Log::debug(sprintf('Adding a rule group to the SearchRuleEngine: #%d ("%s")', $group->id, $group->title));
$this->groups->push($group);
}
}
}
/**
* @inheritDoc
*/
public function setRules(Collection $rules): void
{
Log::debug(__METHOD__);
foreach ($rules as $rule) {
if ($rule instanceof Rule) {
Log::debug(sprintf('Adding a rule to the SearchRuleEngine: #%d ("%s")', $rule->id, $rule->title));
$this->rules->push($rule);
}
}
}
/**
* @inheritDoc
*/
public function setUser(User $user): void
{
$this->user = $user;
$this->operators = [];
}
}

View File

@@ -250,16 +250,6 @@ class TransactionGroupTransformer extends AbstractTransformer
return $this->groupRepos->getLocation($journalId);
}
/**
* @param int $journalId
*
* @return bool
*/
private function hasAttachments(int $journalId): bool
{
return $this->groupRepos->countAttachments($journalId) > 0;
}
/**
* @param array $array
* @param string $key
@@ -290,6 +280,16 @@ class TransactionGroupTransformer extends AbstractTransformer
return $object[$key]->toAtomString();
}
/**
* @param int $journalId
*
* @return bool
*/
private function hasAttachments(int $journalId): bool
{
return $this->groupRepos->countAttachments($journalId) > 0;
}
/**
* @param TransactionGroup $group
*

View File

@@ -35,6 +35,7 @@ trait ReconciliationValidation
{
public ?Account $destination;
public ?Account $source;
/**
* @param array $array
*

View File

@@ -198,6 +198,21 @@ class AccountValidator
return false;
}
/**
* @param string $accountType
*
* @return bool
*/
protected function canCreateType(string $accountType): bool
{
$canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT];
if (in_array($accountType, $canCreate, true)) {
return true;
}
return false;
}
/**
* @param array $validTypes
* @param array $data
@@ -244,19 +259,4 @@ class AccountValidator
return null;
}
/**
* @param string $accountType
*
* @return bool
*/
protected function canCreateType(string $accountType): bool
{
$canCreate = [AccountType::EXPENSE, AccountType::REVENUE, AccountType::INITIAL_BALANCE, AccountType::LIABILITY_CREDIT];
if (in_array($accountType, $canCreate, true)) {
return true;
}
return false;
}
}

View File

@@ -54,47 +54,6 @@ use function is_string;
class FireflyValidator extends Validator
{
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencyName($attribute, $value): bool
{
return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value);
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencyCode($attribute, $value): bool
{
return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value);
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencySymbol($attribute, $value): bool
{
return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value);
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrency(string $field, string $attribute, string $value): bool
{
return 0 === DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count();
}
/**
* @param mixed $attribute
* @param mixed $value
@@ -648,6 +607,46 @@ class FireflyValidator extends Validator
return true;
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencyCode($attribute, $value): bool
{
return $this->validateUniqueCurrency('code', (string) $attribute, (string) $value);
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencyName($attribute, $value): bool
{
return $this->validateUniqueCurrency('name', (string) $attribute, (string) $value);
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrency(string $field, string $attribute, string $value): bool
{
return 0 === DB::table('transaction_currencies')->where($field, $value)->whereNull('deleted_at')->count();
}
/**
* @param $attribute
* @param $value
* @return bool
*/
public function validateUniqueCurrencySymbol($attribute, $value): bool
{
return $this->validateUniqueCurrency('symbol', (string) $attribute, (string) $value);
}
/**
* @param mixed $value
* @param mixed $parameters

View File

@@ -43,6 +43,36 @@ trait GroupValidation
*/
abstract protected function getTransactionsArray(Validator $validator): array;
/**
* @param Validator $validator
*/
protected function preventNoAccountInfo(Validator $validator): void
{
$transactions = $this->getTransactionsArray($validator);
$keys = ['source_id', 'destination_id', 'source_name', 'destination_name', 'source_iban', 'destination_iban', 'source_number', 'destination_number'];
/** @var array $transaction */
foreach ($transactions as $index => $transaction) {
$hasAccountInfo = false;
$hasJournalId = array_key_exists('transaction_journal_id', $transaction);
foreach ($keys as $key) {
if (array_key_exists($key, $transaction) && '' !== (string) $transaction[$key]) {
$hasAccountInfo = true;
}
}
// set errors:
if (false === $hasAccountInfo && !$hasJournalId) {
$validator->errors()->add(
sprintf('transactions.%d.source_id', $index), (string) trans('validation.generic_no_source')
);
$validator->errors()->add(
sprintf('transactions.%d.destination_id', $index), (string) trans('validation.generic_no_destination')
);
}
}
// only an issue if there is no transaction_journal_id
}
/**
* Adds an error to the "description" field when the user has submitted no descriptions and no
* journal description.
@@ -83,36 +113,6 @@ trait GroupValidation
}
}
/**
* @param Validator $validator
*/
protected function preventNoAccountInfo(Validator $validator): void
{
$transactions = $this->getTransactionsArray($validator);
$keys = ['source_id', 'destination_id', 'source_name', 'destination_name', 'source_iban', 'destination_iban', 'source_number', 'destination_number'];
/** @var array $transaction */
foreach ($transactions as $index => $transaction) {
$hasAccountInfo = false;
$hasJournalId = array_key_exists('transaction_journal_id', $transaction);
foreach ($keys as $key) {
if (array_key_exists($key, $transaction) && '' !== (string) $transaction[$key]) {
$hasAccountInfo = true;
}
}
// set errors:
if (false === $hasAccountInfo && !$hasJournalId) {
$validator->errors()->add(
sprintf('transactions.%d.source_id', $index), (string) trans('validation.generic_no_source')
);
$validator->errors()->add(
sprintf('transactions.%d.destination_id', $index), (string) trans('validation.generic_no_destination')
);
}
}
// only an issue if there is no transaction_journal_id
}
/**
* This method validates if the user has submitted transaction journal ID's for each array they submit, if they've submitted more than 1 transaction
* journal. This check is necessary because Firefly III isn't able to distinguish between journals without the ID.