Skip to content

Add support for returning NULL-s from service methods #4

@maciej-sz

Description

@maciej-sz

Service methods that return null would allow using PHP 8 nullsafe operator, without the need to providing correct dates.

Example (current behavior)

$currencyAverages = CurrencyAverageRatesService::create();
// throws exeption, because there is no trading data available for this day:
$currencyAverages->formDay('2023-01-01');

Example (new behavior)

$currencyAverages = CurrencyAverageRatesService::create();
// returns null instead of throwing exception, so can be safely chained:
$currencyAverages->formDay('2023-01-01')?->fromTable('A')->getRate('USD');

Todo

  • Decide whether or not to implement this in old API (causes BC-break), or add new API

Possible aproaches for the new API solution

  1. Introduce methods in existing services (for example tryFromDay('2023-01-01')
  2. Introduce new api classes corresponding to existing ones (something like CurrencyAverageRatesService -> CurrencyAverageRatesNullableService)
  3. Introduce some kind of switch (config variable) in existing services to change the behavior of existing methods. No BC-break, but breaks SOLID.

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions