-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor for PHP 8.x and Symfony 5.x
Replaced PHPDoc comments with typed parameters and return types. Removed superfluous comments. Upgraded to PHPUnit 9.5 Added required PHP extensions to composer.json. Fixed all static analysis issues to PHPStan level 9. Formatted code to PSR-12 using Easy Coding Standard. Generally brought the package up to modern PHP standards.
- Loading branch information
Showing
31 changed files
with
513 additions
and
695 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,114 +2,91 @@ | |
|
||
namespace Lexik\Bundle\CurrencyBundle\Adapter; | ||
|
||
use ArrayIterator; | ||
use Lexik\Bundle\CurrencyBundle\Entity\Currency; | ||
|
||
/** | ||
* | ||
* @author Cédric Girard <[email protected]> | ||
* @author Yoann Aparici <[email protected]> | ||
* @extends ArrayIterator<string, Currency> | ||
*/ | ||
abstract class AbstractCurrencyAdapter extends \ArrayIterator | ||
abstract class AbstractCurrencyAdapter extends ArrayIterator | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $defaultCurrency; | ||
protected string $defaultCurrency; | ||
|
||
/** | ||
* @var array | ||
* @var array<string> | ||
*/ | ||
protected $managedCurrencies = array(); | ||
protected array $managedCurrencies = []; | ||
|
||
/** | ||
* @var string | ||
* @var class-string | ||
*/ | ||
protected $currencyClass; | ||
protected string $currencyClass; | ||
|
||
/** | ||
* Set default currency | ||
* | ||
* @param string $defaultCurrency | ||
*/ | ||
public function setDefaultCurrency($defaultCurrency) | ||
public function setDefaultCurrency(string $defaultCurrency): void | ||
{ | ||
$this->defaultCurrency = $defaultCurrency; | ||
} | ||
|
||
/** | ||
* Get default currency | ||
* | ||
* @return string | ||
*/ | ||
public function getDefaultCurrency() | ||
public function getDefaultCurrency(): string | ||
{ | ||
return $this->defaultCurrency; | ||
} | ||
|
||
/** | ||
* Get managedCurrencies | ||
* | ||
* @param array $currencies | ||
* @param array<string> $currencies | ||
*/ | ||
public function setManagedCurrencies($currencies) | ||
public function setManagedCurrencies(array $currencies): void | ||
{ | ||
$this->managedCurrencies = $currencies; | ||
} | ||
|
||
/** | ||
* Get managedCurrencies | ||
* | ||
* @return array | ||
* @return array<string> | ||
*/ | ||
public function getManagedCurrencies() | ||
public function getManagedCurrencies(): array | ||
{ | ||
return $this->managedCurrencies; | ||
} | ||
|
||
/** | ||
* Get managedCurrencies | ||
* | ||
* @return array | ||
* @param class-string $currencyClass | ||
*/ | ||
public function setCurrencyClass($currencyClass) | ||
public function setCurrencyClass(string $currencyClass): void | ||
{ | ||
return $this->currencyClass = $currencyClass; | ||
$this->currencyClass = $currencyClass; | ||
} | ||
|
||
/** | ||
* Set object | ||
* | ||
* @param mixed $index | ||
* @param Currency $newval | ||
* @param string $key | ||
* @param Currency $value | ||
*/ | ||
public function offsetSet($index, $newval) | ||
public function offsetSet($key, $value): void | ||
{ | ||
if (!$newval instanceof $this->currencyClass) { | ||
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', get_class($newval))); | ||
if (!$value instanceof $this->currencyClass) { | ||
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', $value::class)); | ||
} | ||
|
||
parent::offsetSet($index, $newval); | ||
parent::offsetSet($key, $value); | ||
} | ||
|
||
/** | ||
* Append a value | ||
* | ||
* @param Currency $value | ||
*/ | ||
public function append($value) | ||
public function append($value): void | ||
{ | ||
if (!$value instanceof $this->currencyClass) { | ||
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', get_class($value))); | ||
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', $value::class)); | ||
} | ||
|
||
parent::append($value); | ||
} | ||
|
||
/** | ||
* Convert all | ||
* | ||
* @param mixed $rate | ||
*/ | ||
protected function convertAll($rate) | ||
protected function convertAll(float $rate): void | ||
{ | ||
/** @var Currency $currency */ | ||
foreach ($this as $currency) { | ||
$currency->convert($rate); | ||
} | ||
|
@@ -119,13 +96,11 @@ protected function convertAll($rate) | |
* This method is used by the constructor | ||
* to attach all currencies. | ||
*/ | ||
abstract public function attachAll(); | ||
abstract public function attachAll(): void; | ||
|
||
/** | ||
* Get identier value for the adapter must be unique | ||
* Get identifier value for the adapter must be unique | ||
* for all the project | ||
* | ||
* @return string | ||
*/ | ||
abstract protected function getIdentifier(); | ||
} | ||
abstract public function getIdentifier(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,55 +2,44 @@ | |
|
||
namespace Lexik\Bundle\CurrencyBundle\Adapter; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\Bundle\DoctrineBundle\Registry; | ||
use Doctrine\ORM\EntityManager; | ||
|
||
/** | ||
* This class is used to create DoctrineCurrencyAdapter | ||
* | ||
* @author Yoann Aparici <[email protected]> | ||
* @author Cédric Girard <[email protected]> | ||
*/ | ||
class AdapterFactory | ||
{ | ||
/** | ||
* @var EntityManager | ||
*/ | ||
protected $doctrine; | ||
|
||
/** | ||
* @var array | ||
* @var array{default: string, managed: array<string>} | ||
*/ | ||
private $currencies; | ||
private array $currencies; | ||
|
||
/** | ||
* @var string | ||
* @param Registry $doctrine | ||
* @param string $defaultCurrency | ||
* @param array<string> $availableCurrencies | ||
* @param class-string $currencyClass | ||
*/ | ||
private $currencyClass; | ||
|
||
/** | ||
* __construct | ||
* | ||
* @param EntityManager $em | ||
*/ | ||
public function __construct(Registry $doctrine, $defaultCurrency, $availableCurrencies, $currencyClass) | ||
{ | ||
$this->doctrine = $doctrine; | ||
|
||
$this->currencies = array(); | ||
$this->currencies['default'] = $defaultCurrency; | ||
$this->currencies['managed'] = $availableCurrencies; | ||
$this->currencyClass = $currencyClass; | ||
public function __construct( | ||
protected Registry $doctrine, | ||
string $defaultCurrency, | ||
array $availableCurrencies, | ||
private string $currencyClass | ||
) { | ||
$this->currencies = [ | ||
'default' => $defaultCurrency, | ||
'managed' => $availableCurrencies | ||
]; | ||
} | ||
|
||
/** | ||
* Create an adaper from the given class. | ||
* | ||
* @param string $adapterClass | ||
* @return Lexik\Bundle\CurrencyBundle\Adapter\AbstractCurrencyAdapter | ||
* @param class-string $adapterClass | ||
*/ | ||
public function create($adapterClass) | ||
public function create(string $adapterClass): AbstractCurrencyAdapter | ||
{ | ||
/** @var AbstractCurrencyAdapter $adapter */ | ||
$adapter = new $adapterClass(); | ||
$adapter->setDefaultCurrency($this->currencies['default']); | ||
$adapter->setManagedCurrencies($this->currencies['managed']); | ||
|
@@ -60,62 +49,56 @@ public function create($adapterClass) | |
} | ||
|
||
/** | ||
* Create a DoctrineCurrencyAdapter. | ||
* | ||
* @return Lexik\Bundle\CurrencyBundle\Adapter\DoctrineCurrencyAdapter | ||
* @param ?class-string $adapterClass | ||
*/ | ||
public function createDoctrineAdapter($adapterClass = null, $entityManagerName = null) | ||
public function createDoctrineAdapter(string $adapterClass = null, string $entityManagerName = null): AbstractCurrencyAdapter | ||
{ | ||
if (null == $adapterClass) { | ||
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\DoctrineCurrencyAdapter'; | ||
$adapterClass = DoctrineCurrencyAdapter::class; | ||
} | ||
/** @var DoctrineCurrencyAdapter $adapter */ | ||
$adapter = $this->create($adapterClass); | ||
|
||
/** @var EntityManager $em */ | ||
$em = $this->doctrine->getManager($entityManagerName); | ||
$adapter->setManager($em); | ||
|
||
return $adapter; | ||
} | ||
|
||
/** | ||
* Create an EcbCurrencyAdapter. | ||
* | ||
* @return Lexik\Bundle\CurrencyBundle\Adapter\EcbCurrencyAdapter | ||
* @param ?class-string $adapterClass | ||
*/ | ||
public function createEcbAdapter($adapterClass = null) | ||
public function createEcbAdapter(string $adapterClass = null): AbstractCurrencyAdapter | ||
{ | ||
if (null == $adapterClass) { | ||
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\EcbCurrencyAdapter'; | ||
$adapterClass = EcbCurrencyAdapter::class; | ||
} | ||
|
||
return $this->create($adapterClass); | ||
} | ||
|
||
/** | ||
* Create an OerCurrencyAdapter. | ||
* | ||
* @return Lexik\Bundle\CurrencyBundle\Adapter\OerCurrencyAdapter | ||
* @param ?class-string $adapterClass | ||
*/ | ||
public function createOerAdapter($adapterClass = null) | ||
public function createOerAdapter(string $adapterClass = null): AbstractCurrencyAdapter | ||
{ | ||
if (null == $adapterClass) { | ||
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\OerCurrencyAdapter'; | ||
$adapterClass = OerCurrencyAdapter::class; | ||
} | ||
|
||
return $this->create($adapterClass); | ||
} | ||
|
||
/** | ||
* Create an YahooCurrencyAdapter. | ||
* | ||
* @return Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter | ||
* @param ?class-string $adapterClass | ||
*/ | ||
public function createYahooAdapter($adapterClass = null) | ||
public function createYahooAdapter(string $adapterClass = null): AbstractCurrencyAdapter | ||
{ | ||
if (null == $adapterClass) { | ||
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter'; | ||
$adapterClass = YahooCurrencyAdapter::class; | ||
} | ||
|
||
return $this->create($adapterClass); | ||
} | ||
} | ||
} |
Oops, something went wrong.