Skip to content

Commit 5ffe05e

Browse files
committed
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.
1 parent 3058642 commit 5ffe05e

31 files changed

+514
-697
lines changed

Adapter/AbstractCurrencyAdapter.php

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,114 +2,91 @@
22

33
namespace Lexik\Bundle\CurrencyBundle\Adapter;
44

5+
use ArrayIterator;
6+
use Lexik\Bundle\CurrencyBundle\Entity\Currency;
7+
58
/**
69
*
710
* @author Cédric Girard <[email protected]>
811
* @author Yoann Aparici <[email protected]>
12+
* @extends ArrayIterator<string, Currency>
913
*/
10-
abstract class AbstractCurrencyAdapter extends \ArrayIterator
14+
abstract class AbstractCurrencyAdapter extends ArrayIterator
1115
{
12-
/**
13-
* @var string
14-
*/
15-
protected $defaultCurrency;
16+
protected string $defaultCurrency;
1617

1718
/**
18-
* @var array
19+
* @var array<string>
1920
*/
20-
protected $managedCurrencies = array();
21+
protected array $managedCurrencies = [];
2122

2223
/**
23-
* @var string
24+
* @var class-string
2425
*/
25-
protected $currencyClass;
26+
protected string $currencyClass;
2627

27-
/**
28-
* Set default currency
29-
*
30-
* @param string $defaultCurrency
31-
*/
32-
public function setDefaultCurrency($defaultCurrency)
28+
public function setDefaultCurrency(string $defaultCurrency): void
3329
{
3430
$this->defaultCurrency = $defaultCurrency;
3531
}
3632

37-
/**
38-
* Get default currency
39-
*
40-
* @return string
41-
*/
42-
public function getDefaultCurrency()
33+
public function getDefaultCurrency(): string
4334
{
4435
return $this->defaultCurrency;
4536
}
4637

4738
/**
48-
* Get managedCurrencies
49-
*
50-
* @param array $currencies
39+
* @param array<string> $currencies
5140
*/
52-
public function setManagedCurrencies($currencies)
41+
public function setManagedCurrencies(array $currencies): void
5342
{
5443
$this->managedCurrencies = $currencies;
5544
}
5645

5746
/**
58-
* Get managedCurrencies
59-
*
60-
* @return array
47+
* @return array<string>
6148
*/
62-
public function getManagedCurrencies()
49+
public function getManagedCurrencies(): array
6350
{
6451
return $this->managedCurrencies;
6552
}
6653

6754
/**
68-
* Get managedCurrencies
69-
*
70-
* @return array
55+
* @param class-string $currencyClass
7156
*/
72-
public function setCurrencyClass($currencyClass)
57+
public function setCurrencyClass(string $currencyClass): void
7358
{
74-
return $this->currencyClass = $currencyClass;
59+
$this->currencyClass = $currencyClass;
7560
}
7661

7762
/**
78-
* Set object
79-
*
80-
* @param mixed $index
81-
* @param Currency $newval
63+
* @param string $key
64+
* @param Currency $value
8265
*/
83-
public function offsetSet($index, $newval)
66+
public function offsetSet($key, $value): void
8467
{
85-
if (!$newval instanceof $this->currencyClass) {
86-
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', get_class($newval)));
68+
if (!$value instanceof $this->currencyClass) {
69+
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', $value::class));
8770
}
8871

89-
parent::offsetSet($index, $newval);
72+
parent::offsetSet($key, $value);
9073
}
9174

9275
/**
93-
* Append a value
94-
*
9576
* @param Currency $value
9677
*/
97-
public function append($value)
78+
public function append($value): void
9879
{
9980
if (!$value instanceof $this->currencyClass) {
100-
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', get_class($value)));
81+
throw new \InvalidArgumentException(sprintf('$newval must be an instance of Currency, instance of "%s" given', $value::class));
10182
}
10283

10384
parent::append($value);
10485
}
10586

106-
/**
107-
* Convert all
108-
*
109-
* @param mixed $rate
110-
*/
111-
protected function convertAll($rate)
87+
protected function convertAll(float $rate): void
11288
{
89+
/** @var Currency $currency */
11390
foreach ($this as $currency) {
11491
$currency->convert($rate);
11592
}
@@ -119,13 +96,11 @@ protected function convertAll($rate)
11996
* This method is used by the constructor
12097
* to attach all currencies.
12198
*/
122-
abstract public function attachAll();
99+
abstract public function attachAll(): void;
123100

124101
/**
125-
* Get identier value for the adapter must be unique
102+
* Get identifier value for the adapter must be unique
126103
* for all the project
127-
*
128-
* @return string
129104
*/
130-
abstract protected function getIdentifier();
131-
}
105+
abstract public function getIdentifier(): string;
106+
}

Adapter/AdapterCollector.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,22 @@
99
*/
1010
final class AdapterCollector
1111
{
12-
private $elements = array();
13-
1412
/**
15-
* Add an adapter
16-
*
17-
* @param mixed $key
18-
* @param AbstractCurrencyAdapter $adapter
13+
* @var array<string, AbstractCurrencyAdapter>
1914
*/
20-
public function add(AbstractCurrencyAdapter $adapter)
15+
private array $elements = [];
16+
17+
public function add(AbstractCurrencyAdapter $adapter): void
2118
{
2219
$this->elements[$adapter->getIdentifier()] = $adapter;
2320
}
2421

25-
/**
26-
* Get adapter
27-
*
28-
* @param mixed $key
29-
* @return AbstractCurrencyAdapter
30-
*/
31-
public function get($key)
22+
public function get(string $key): AbstractCurrencyAdapter
3223
{
3324
if (!isset($this->elements[$key])) {
3425
throw new InvalidArgumentException('Adapter does not exist');
3526
}
3627

3728
return $this->elements[$key];
3829
}
39-
40-
}
30+
}

Adapter/AdapterFactory.php

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,44 @@
22

33
namespace Lexik\Bundle\CurrencyBundle\Adapter;
44

5-
use Doctrine\ORM\EntityManager;
65
use Doctrine\Bundle\DoctrineBundle\Registry;
6+
use Doctrine\ORM\EntityManager;
77

88
/**
9-
* This class is used to create DoctrineCurrencyAdapter
10-
*
119
* @author Yoann Aparici <[email protected]>
1210
* @author Cédric Girard <[email protected]>
1311
*/
1412
class AdapterFactory
1513
{
1614
/**
17-
* @var EntityManager
18-
*/
19-
protected $doctrine;
20-
21-
/**
22-
* @var array
15+
* @var array{default: string, managed: array<string>}
2316
*/
24-
private $currencies;
17+
private array $currencies;
2518

2619
/**
27-
* @var string
20+
* @param Registry $doctrine
21+
* @param string $defaultCurrency
22+
* @param array<string> $availableCurrencies
23+
* @param class-string $currencyClass
2824
*/
29-
private $currencyClass;
30-
31-
/**
32-
* __construct
33-
*
34-
* @param EntityManager $em
35-
*/
36-
public function __construct(Registry $doctrine, $defaultCurrency, $availableCurrencies, $currencyClass)
37-
{
38-
$this->doctrine = $doctrine;
39-
40-
$this->currencies = array();
41-
$this->currencies['default'] = $defaultCurrency;
42-
$this->currencies['managed'] = $availableCurrencies;
43-
$this->currencyClass = $currencyClass;
25+
public function __construct(
26+
protected Registry $doctrine,
27+
string $defaultCurrency,
28+
array $availableCurrencies,
29+
private string $currencyClass
30+
) {
31+
$this->currencies = [
32+
'default' => $defaultCurrency,
33+
'managed' => $availableCurrencies
34+
];
4435
}
4536

4637
/**
47-
* Create an adaper from the given class.
48-
*
49-
* @param string $adapterClass
50-
* @return Lexik\Bundle\CurrencyBundle\Adapter\AbstractCurrencyAdapter
38+
* @param class-string $adapterClass
5139
*/
52-
public function create($adapterClass)
40+
public function create(string $adapterClass): AbstractCurrencyAdapter
5341
{
42+
/** @var AbstractCurrencyAdapter $adapter */
5443
$adapter = new $adapterClass();
5544
$adapter->setDefaultCurrency($this->currencies['default']);
5645
$adapter->setManagedCurrencies($this->currencies['managed']);
@@ -60,62 +49,56 @@ public function create($adapterClass)
6049
}
6150

6251
/**
63-
* Create a DoctrineCurrencyAdapter.
64-
*
65-
* @return Lexik\Bundle\CurrencyBundle\Adapter\DoctrineCurrencyAdapter
52+
* @param ?class-string $adapterClass
6653
*/
67-
public function createDoctrineAdapter($adapterClass = null, $entityManagerName = null)
54+
public function createDoctrineAdapter(string $adapterClass = null, string $entityManagerName = null): AbstractCurrencyAdapter
6855
{
6956
if (null == $adapterClass) {
70-
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\DoctrineCurrencyAdapter';
57+
$adapterClass = DoctrineCurrencyAdapter::class;
7158
}
59+
/** @var DoctrineCurrencyAdapter $adapter */
7260
$adapter = $this->create($adapterClass);
7361

62+
/** @var EntityManager $em */
7463
$em = $this->doctrine->getManager($entityManagerName);
7564
$adapter->setManager($em);
7665

7766
return $adapter;
7867
}
7968

8069
/**
81-
* Create an EcbCurrencyAdapter.
82-
*
83-
* @return Lexik\Bundle\CurrencyBundle\Adapter\EcbCurrencyAdapter
70+
* @param ?class-string $adapterClass
8471
*/
85-
public function createEcbAdapter($adapterClass = null)
72+
public function createEcbAdapter(string $adapterClass = null): AbstractCurrencyAdapter
8673
{
8774
if (null == $adapterClass) {
88-
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\EcbCurrencyAdapter';
75+
$adapterClass = EcbCurrencyAdapter::class;
8976
}
9077

9178
return $this->create($adapterClass);
9279
}
9380

9481
/**
95-
* Create an OerCurrencyAdapter.
96-
*
97-
* @return Lexik\Bundle\CurrencyBundle\Adapter\OerCurrencyAdapter
82+
* @param ?class-string $adapterClass
9883
*/
99-
public function createOerAdapter($adapterClass = null)
84+
public function createOerAdapter(string $adapterClass = null): AbstractCurrencyAdapter
10085
{
10186
if (null == $adapterClass) {
102-
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\OerCurrencyAdapter';
87+
$adapterClass = OerCurrencyAdapter::class;
10388
}
10489

10590
return $this->create($adapterClass);
10691
}
10792

10893
/**
109-
* Create an YahooCurrencyAdapter.
110-
*
111-
* @return Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter
94+
* @param ?class-string $adapterClass
11295
*/
113-
public function createYahooAdapter($adapterClass = null)
96+
public function createYahooAdapter(string $adapterClass = null): AbstractCurrencyAdapter
11497
{
11598
if (null == $adapterClass) {
116-
$adapterClass = 'Lexik\Bundle\CurrencyBundle\Adapter\YahooCurrencyAdapter';
99+
$adapterClass = YahooCurrencyAdapter::class;
117100
}
118101

119102
return $this->create($adapterClass);
120103
}
121-
}
104+
}

0 commit comments

Comments
 (0)